Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC4J-578 Migrate existing jirabot logic to new Jira environment #692

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be logged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a log statement and squashed

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
Loading