diff --git a/.github/workflows/jirabot.yml b/.github/workflows/jirabot.yml index fa402dcc4f9..90f28e35f9c 100644 --- a/.github/workflows/jirabot.yml +++ b/.github/workflows/jirabot.yml @@ -23,9 +23,8 @@ jobs: python -VV python -m site python -m pip install --upgrade pip setuptools wheel - python -m pip install --upgrade jira + python -m pip install --upgrade atlassian-python-api python -m pip --version - python -m pip freeze | grep jira - name: "Run" env: JIRABOT_USERNAME : ${{ secrets.JIRABOT_USERNAME }} @@ -44,49 +43,56 @@ jobs: import os import re import time + import sys import json - from jira.client import JIRA + from atlassian.jira import Jira - def updateIssue(jira, issue, prAuthorEmail : str, transitionMap: dict, propertyMap: dict, pull_url: str) -> str: + def updateIssue(jira, issue, prAuthor : str, transitionMap: dict, propertyMap: dict, pull_url: str) -> str: result = '' - statusName = str(issue.fields.status) + issueName = issue['key'] + issueFields = issue['fields'] + + statusName = str(issueFields['status']['name']) transition = transitionMap.get(statusName, None) if transition == None: print('Error: Unable to find transition for status: ' + statusName) elif transition != '': try: - jira.transition_issue(issue, transition) + jira.issue_transition(issueName, transition) result += 'Workflow Transition: ' + transition + '\n' except Exception as error: - transitions = jira.transitions(issue) + transitions = jira.get_issue_transitions(issueName) result += 'Error: Transition: "' + transition + '" failed with: "' + str(error) + '" Valid transitions=' + str(transitions) + '\n' prFieldName = propertyMap.get('pullRequestFieldName', 'customfield_10010') - try: - currentPR = getattr(issue.fields, prFieldName) - except: + + if prFieldName in issueFields: + currentPR = issueFields[prFieldName] + else: + print('Error: Unable to find pull request field with field name: ' + prFieldName) currentPR = None - print('Error: Unable to get current pull request with field name: ' + prFieldName) if currentPR is None: - issue.update(fields={prFieldName: pull_url}) + jira.update_issue_field(issueName, {prFieldName: pull_url}) result += 'Updated PR\n' elif currentPR is not None and currentPR != pull_url: result += 'Additional PR: ' + pull_url + '\n' - if prAuthorEmail: - if issue.fields.assignee is None: - 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) + if prAuthor: + assignee = issueFields['assignee'] + assigneeId = assignee['accountId'] + assigneeEmail = assignee["emailAddress"] + + prAuthorId = prAuthor["accountId"] + prAuthorEmail = prAuthor["emailAddress"] + if assigneeId is None or assigneeId == '': + jira.assign_issue(issueName, prAuthorId) + result += 'Assigning user: ' + prAuthor + '\n' + elif assigneeId != prAuthorId: + result += 'Changing assignee from: ' + assigneeEmail + ' to: ' + prAuthorEmail + '\n' + jira.assign_issue(issueName, prAuthorId) return result @@ -114,34 +120,20 @@ jobs: 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)) + jira = Jira(url=jira_url, username= jirabot_user, password= jirabot_pass, cloud=True) - # Check if the jira session is valid, if not retry authentication - # This appears to be an issue with the Cloud Jira / Python library - for attempt in range(3): - try: - jira.session() - break - except Exception as error: - print('Service account authentication failed, retrying in 10s.') - time.sleep(10) - jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass)) - - userSearchResults = jira.search_users(query=prAuthor) + jiraUser = None + userSearchResults = jira.find_user_by_user_string(query=prAuthor) if userSearchResults and len(userSearchResults) > 0: jiraUser = userSearchResults[0] - - jiraUserEmail = None - if jiraUser is None: + else: print('Error: Unable to find Jira user: ' + prAuthor + ' continuing without assigning') + + if !jira.issue_exists(issue_name): + sys.exit('Error: Unable to find Jira issue: ' + issue_name) else: - jiraUserEmail = jiraUser.emailAddress + issue = jira.issue(issue_name) - issue = jira.issue(issue_name) result = 'Jirabot Action Result:\n' transitionMap = json.loads(os.environ['JIRA_ISSUE_TRANSITION_MAP']) @@ -154,8 +146,8 @@ jobs: print('Error: JIRA_ISSUE_PROPERTY_MAP is not a valid JSON object, ignoring.') jiraIssuePropertyMap = {} - result += updateIssue(jira, issue, jiraUserEmail, transitionMap, jiraIssuePropertyMap, pull_url) - jira.add_comment(issue, result) + result += updateIssue(jira, issue, jiraUser, transitionMap, jiraIssuePropertyMap, pull_url) + jira.issue_add_comment(issue_name, result) else: print('Unable to find Jira issue name in title')