From 02ecaf0eaec829046d564c84bccf2c38dd1c82b7 Mon Sep 17 00:00:00 2001 From: James McMullan Date: Wed, 13 Mar 2024 09:34:21 -0400 Subject: [PATCH] HPCC4J-578 Migrate existing jirabot logic to new Jira environment (#692) - Updated Jirabot to work with current Jira and new Cloud Jira Signed-off-by: James McMullan James.McMullan@lexisnexis.com Signed-off-by: James McMullan James.McMullan@lexisnexis.com --- .github/workflows/Jirabot.yml | 69 +++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/.github/workflows/Jirabot.yml b/.github/workflows/Jirabot.yml index a5c8bd0c3..fd7f91013 100644 --- a/.github/workflows/Jirabot.yml +++ b/.github/workflows/Jirabot.yml @@ -39,28 +39,22 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GHUB_JIRA_USER_MAP: ${{ vars.GHUB_JIRA_USER_MAP }} JIRA_ISSUE_PROPERTY_MAP: ${{ vars.JIRA_ISSUE_PROPERTY_MAP }} + JIRA_ISSUE_TRANSITION_MAP: ${{ vars.JIRA_ISSUE_TRANSITION_MAP }} run: | import os import re import json from jira.client import JIRA - def updateIssue(jira, issue, prAuthor: str, propertyMap: dict, pull_url: str) -> str: + def updateIssue(jira, issue, prAuthorEmail : str, transitionMap: dict, propertyMap: dict, 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 = '' + transition = transitionMap.get(statusName, None) - if transition != '': + if transition == None: + print('Error: Unable to find transition for status: ' + statusName) + elif transition != '': try: jira.transition_issue(issue, transition) result += 'Workflow Transition: ' + transition + '\n' @@ -81,13 +75,17 @@ jobs: elif currentPR is not None and currentPR != pull_url: result += 'Additional PR: ' + pull_url + '\n' - if prAuthor: + if prAuthorEmail: 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) + jira.assign_issue(issue, prAuthorEmail) + result += 'Assigning user: ' + prAuthorEmail + '\n' + else: + assigneeEmail = None + if issue.fields.assignee: + assigneeEmail = issue.fields.assignee.emailAddress + if assigneeEmail is None or assigneeEmail.lower() != assigneeEmail.lower(): + result += 'Changing assignee from: ' + assigneeEmail + ' to: ' + prAuthorEmail + '\n' + jira.assign_issue(issue, prAuthorEmail) return result @@ -122,24 +120,41 @@ jobs: jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass)) - # Check if prAuthor exists in Jira - try: + # Need to change how we find users for Jira Cloud, unfortunately the API doesn't provide this information. + # At the moment checking if the URL contains atlassian.net appears to be the easiest way to determine if it's Jira Cloud. + isJiraCloud = False + if jira_url.find('atlassian.net') > 0: + isJiraCloud = True + + if isJiraCloud: + res = jira.search_users(query=prAuthor) + if res and len(res) > 0: + jiraUser = res[0] + else: 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') + + jiraUserEmail = None + if jiraUser is None: + print('Error: Unable to find Jira user: ' + prAuthor + ' continuing without assigning') + else: + jiraUserEmail = jiraUser.emailAddress + + print("Jira User Email:" + str(jiraUserEmail)) issue = jira.issue(issue_name) result = 'Jirabot Action Result:\n' + transitionMap = json.loads(os.environ['JIRA_ISSUE_TRANSITION_MAP']) + if not isinstance(transitionMap, dict): + print('Error: JIRA_ISSUE_TRANSITION_MAP is not a valid JSON object, ignoring.') + transitionMap = {} + jiraIssuePropertyMap = json.loads(os.environ['JIRA_ISSUE_PROPERTY_MAP']) if not isinstance(jiraIssuePropertyMap, dict): + print('Error: JIRA_ISSUE_PROPERTY_MAP is not a valid JSON object, ignoring.') jiraIssuePropertyMap = {} - result += updateIssue(jira, issue, prAuthor, jiraIssuePropertyMap, pull_url) + result += updateIssue(jira, issue, jiraUserEmail, transitionMap, jiraIssuePropertyMap, pull_url) jira.add_comment(issue, result) else: print('Unable to find Jira issue name in title')