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

HPCC-31397 Jirabot updates for CloudJira #18454

Merged
merged 1 commit into from
Mar 26, 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
86 changes: 39 additions & 47 deletions .github/workflows/jirabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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:
Copy link
Member

Choose a reason for hiding this comment

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

Not quite sure about this - failing currently helps catch PRs with the wrong ID. Will merge as-is and revisit after some use.

result += 'Changing assignee from: ' + assigneeEmail + ' to: ' + prAuthorEmail + '\n'
jira.assign_issue(issueName, prAuthorId)

return result

Expand Down Expand Up @@ -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'])
Expand All @@ -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')

Expand Down
Loading