Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC4J-574 Jirabot improvements #683

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 deletions .github/workflows/Jirabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ jobs:
PULL_URL: ${{ github.event.pull_request.html_url }}
COMMENTS_URL: ${{ github.event.pull_request.comments_url }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

GHUB_JIRA_USER_MAP: ${{ vars.GHUB_JIRA_USER_MAP }}
JIRA_ISSUE_PROPERTY_MAP: ${{ vars.JIRA_ISSUE_PROPERTY_MAP }}
run: |
import os
import re
import json
from jira.client import JIRA

def updateIssue(jira, issue, user: str, pull_url: str) -> str:
def updateIssue(jira, issue, prAuthor: str, propertyMap: dict, pull_url: str) -> str:
result = ''

statusName = str(issue.fields.status)
Expand All @@ -66,18 +68,26 @@ jobs:
transitions = jira.transitions(issue)
result += 'Error: Transition: "' + transition + '" failed with: "' + str(error) + '" Valid transitions=' + str(transitions) + '\n'

if issue.fields.customfield_10010 is None:
issue.update(fields={'customfield_10010': pull_url})
prFieldName = propertyMap.get('pullRequestFieldName', 'customfield_10010')
try:
currentPR = getattr(issue.fields, prFieldName)
except:
currentPR = None
print('Error: Unable to get current pull request with field name: ' + prFieldName)

if currentPR is None:
issue.update(fields={prFieldName: pull_url})
result += 'Updated PR\n'
elif issue.fields.customfield_10010 is not None and issue.fields.customfield_10010 != pull_url:
elif currentPR is not None and currentPR != pull_url:
result += 'Additional PR: ' + pull_url + '\n'

if issue.fields.assignee is None:
jira.assign_issue(issue, user)
result += 'Assigning user: ' + user + '\n'
elif issue.fields.assignee is not None and issue.fields.assignee.name.lower() != user.lower():
result += 'Changing assignee from: ' + issue.fields.assignee.name + ' to: ' + user + '\n'
jira.assign_issue(issue, user)
if prAuthor:
if issue.fields.assignee is None:
jira.assign_issue(issue, prAuthor)
result += 'Assigning user: ' + prAuthor + '\n'
elif issue.fields.assignee is not None and issue.fields.assignee.name.lower() != prAuthor.lower():
result += 'Changing assignee from: ' + issue.fields.assignee.name + ' to: ' + prAuthor + '\n'
jira.assign_issue(issue, prAuthor)

return result

Expand All @@ -86,40 +96,53 @@ jobs:
jira_url = os.environ['JIRA_URL']
pr = os.environ['PULL_REQUEST_NUMBER']
title = os.environ['PULL_REQUEST_TITLE']
user = os.environ['PULL_REQUEST_AUTHOR_NAME']
prAuthor = os.environ['PULL_REQUEST_AUTHOR_NAME']
pull_url = os.environ['PULL_URL']
github_token = os.environ['GITHUB_TOKEN']
comments_url = os.environ['COMMENTS_URL']

print("%s %s %s" % (title, user, comments_url))
print("%s %s %s" % (title, prAuthor, comments_url))
result = ''
issuem = re.search("(HPCC4J|JAPI)-[0-9]+", title)
if issuem:
nameCorrectionPattern = re.compile("hpcc4j", re.IGNORECASE)
issue_name = nameCorrectionPattern.sub("JAPI",issuem.group())

userDict = {
'kunalaswani': 'kunal.aswani',
'timothyklemm': 'klemti01',
'jpmcmu': 'mcmuja01',
'asselitx': 'terrenceasselin',
'jeclrsg': 'clemje01',
'jackdelv': 'delvecja',
}
user = userDict.get(user, user)
userDict = json.loads(os.environ['GHUB_JIRA_USER_MAP'])
if not isinstance(userDict, dict):
userDict = {}

if prAuthor in userDict:
prAuthor = userDict.get(prAuthor)
print('Mapped Github user to Jira user: ' + prAuthor)

options = {
'server': jira_url
}

jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass))

# Check if prAuthor exists in Jira
try:
jiraUser = jira.user(prAuthor)
if jiraUser is None:
prAuthor = None
print('Error: Unable to find Jira user: ' + prAuthor + ' continuing without assigning')
except Exception as error:
prAuthor = None
print('Error: Unable to find Jira user: ' + prAuthor + ' with error: ' + str(error) + ' continuing without assigning')

issue = jira.issue(issue_name)
result = 'Jirabot Action Result:\n'

result += updateIssue(jira, issue, user, pull_url)
jiraIssuePropertyMap = json.loads(os.environ['JIRA_ISSUE_PROPERTY_MAP'])
if not isinstance(jiraIssuePropertyMap, dict):
jiraIssuePropertyMap = {}

result += updateIssue(jira, issue, prAuthor, jiraIssuePropertyMap, pull_url)
jira.add_comment(issue, result)
else:
print('Unable to find Jira issue name in title')

print(result)
shell: python
shell: python
4 changes: 0 additions & 4 deletions .github/workflows/JirabotMerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,6 @@ jobs:
jirabot_pass = os.environ['JIRABOT_PASSWORD']
jira_url = os.environ['JIRA_URL']

if not jira_url:
jira_url = 'https://track.hpccsystems.com'
print('Jira URL us empty defaulting to: ' + jira_url)

pr = os.environ['PULL_REQUEST_NUMBER']
title = os.environ['PULL_REQUEST_TITLE']
user = os.environ['PULL_REQUEST_AUTHOR_NAME']
Expand Down
Loading