From 8be3c0b9e56643c7db4e976bb1f157db562fdf13 Mon Sep 17 00:00:00 2001 From: James McMullan Date: Tue, 6 Feb 2024 09:29:16 -0500 Subject: [PATCH] Code review changes --- .github/workflows/Jirabot.yml | 40 +++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/.github/workflows/Jirabot.yml b/.github/workflows/Jirabot.yml index 5ac7d1e9e..4245c40e0 100644 --- a/.github/workflows/Jirabot.yml +++ b/.github/workflows/Jirabot.yml @@ -37,14 +37,14 @@ jobs: PULL_URL: ${{ github.event.pull_request.html_url }} COMMENTS_URL: ${{ github.event.pull_request.comments_url }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - USER_DICT: ${{ vars.USER_DICT }} + GITHUB_JIRA_USER_MAP: ${{ vars.GITHUB_JIRA_USER_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, pull_url: str) -> str: result = '' statusName = str(issue.fields.status) @@ -73,13 +73,13 @@ jobs: elif issue.fields.customfield_10010 is not None and issue.fields.customfield_10010 != pull_url: result += 'Additional PR: ' + pull_url + '\n' - if user: + if prAuthor: 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) + 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 @@ -88,34 +88,46 @@ 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 = json.loads(os.environ['USER_DICT']) + userDict = json.loads(os.environ['GITHUB_JIRA_USER_MAP']) if not isinstance(userDict, dict): userDict = {} - if user in userDict: - user = userDict.get(user, user) + 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) + result += updateIssue(jira, issue, prAuthor, pull_url) jira.add_comment(issue, result) else: print('Unable to find Jira issue name in title')