Skip to content

Commit

Permalink
HPCC4J-578 Migrate existing jirabot logic to new Jira environment
Browse files Browse the repository at this point in the history
- Updated Jirabot to work with current Jira and new Cloud Jira

Signed-off-by: James McMullan [email protected]
  • Loading branch information
jpmcmu committed Mar 13, 2024
1 parent c531036 commit 29473e7
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions .github/workflows/Jirabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 29473e7

Please sign in to comment.