From 255f73079bd74308742ed7cfbac64e6649da7811 Mon Sep 17 00:00:00 2001 From: James McMullan Date: Tue, 22 Aug 2023 16:17:22 -0400 Subject: [PATCH] HPCC4J-532 Jirabot python script fixes - Fixed status mapping - Improved code flow - Improved issue commenting - Moved issue update to separate function Signed-off-by: James McMullan James.McMullan@lexisnexis.com --- .github/workflows/Jirabot.yml | 98 +++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/.github/workflows/Jirabot.yml b/.github/workflows/Jirabot.yml index 0f28a2876..7cd77f693 100644 --- a/.github/workflows/Jirabot.yml +++ b/.github/workflows/Jirabot.yml @@ -41,62 +41,82 @@ jobs: import re from jira.client import JIRA + def updateIssue(jira, issue, user: str, pull_url: str) -> str: + result = '' + + statusName = str(issue.fields.status) + if statusName == 'Open': + transition = 'Start Progress' + elif statusName == 'In Progress': + transition = '' + elif statusName == 'Resolved': + transition = 'Reopen Issue' + elif statusName == 'Closed': + transition = 'Reopen Issue' + else: + transition = '' + + if transition != '': + try: + jira.transition_issue(issue, transition) + result += 'Workflow Transition: ' + transition + '\n' + except: + transitions = jira.transitions(issue) + result += 'Error: Transition: "' + transition + '" failed. Valid transitions=' + transitions + '\n' + + if issue.fields.customfield_10010 is None: + issue.update(fields={'customfield_10010': pull_url}) + result += 'Updated PR\n' + elif issue.fields.customfield_10010 is not None and issue.fields.customfield_10010 != 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) + + return result + jirabot_user = os.environ['JIRABOT_USERNAME'] jirabot_pass = os.environ['JIRABOT_PASSWORD'] 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'] - comments_url = os.environ['COMMENTS_URL'] pull_url = os.environ['PULL_URL'] github_token = os.environ['GITHUB_TOKEN'] print("%s %s %s" % (title, user, comments_url)) - status = '' + result = '' issuem = re.search("(HPCC4J|JAPI)-[0-9]+", title) if issuem: nameCorrectionPattern = re.compile("hpcc4j", re.IGNORECASE) issue_name = nameCorrectionPattern.sub("JAPI",issuem.group()) - if user == 'kunalaswani': - user = 'kunal.aswani' - if user == 'timothyklemm': - user = 'klemti01' - if user == 'jpmcmu': - user = 'mcmuja01' - if user == 'asselitx': - user = 'terrenceasselin' - if user == 'jeclrsg': - user = 'clemje01' - if user == 'jackdelv': - user = 'delvecja' + + userDict = { + 'kunalaswani': 'kunal.aswani', + 'timothyklemm': 'klemti01', + 'jpmcmu': 'mcmuja01', + 'asselitx': 'terrenceasselin', + 'jeclrsg': 'clemje01', + 'jackdelv': 'delvecja', + } + user = userDict.get(user, user) + options = { - 'server': jira_url + 'server': jira_url } + jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass)) issue = jira.issue(issue_name) - status = jira_url + '/browse/' + issue_name + '\\n' - if False and issue.fields.status.name != 'Active' and issue.fields.status.name != 'Open' and issue.fields.status.name != 'New' and issue.fields.status.name != 'Discussing' and issue.fields.status.name != 'Awaiting Information': - status += 'Jira not updated (state was not active or new)' - elif issue.fields.customfield_10010 != None: - if issue.fields.customfield_10010 != pull_url: - status += 'Jira not updated (pull request "%s" already registered)' % issue.fields.customfield_10010 - else: - status += 'This pull request is already registered' - elif issue.fields.assignee is not None and issue.fields.assignee.name.lower() != user.lower(): - status += 'Jira not updated (user does not match)' - else: - if issue.fields.assignee is None: - jira.assign_issue(issue, user) - issue.update(fields={'customfield_10010': pull_url}) - issue = jira.issue(issue_name) - try: - transitions = jira.transitions(issue) - jira.transition_issue(issue, '291') # Attach Pull Request - except: - status += 'Failed to set to merge pending: transitions=%s' % transitions - status += 'Jira updated' - print('curl -X POST %s -H "Content-Type: application/json" -H "Authorization: token %s" --data \'{ "body": "%s" }\'' % ( comments_url, github_token, status )) - os.system('curl -X POST %s -H "Content-Type: application/json" -H "Authorization: token %s" --data \'{ "body": "%s" }\'' % ( comments_url, github_token, status )) + result = 'Jirabot Action Result:\n' + + result += updateIssue(jira, issue, user, pull_url) + jira.add_comment(issue, result) + else: + print('Unable to find Jira issue name in title') - print(status) + print(result) shell: python