Skip to content

Commit 1e8562f

Browse files
authored
HPCC4J-574 Jirabot Improvements (#700)
- Updated Jirabot to use Atlassian python library - Updated Jirabot to leave a comment on the Github PR Signed-off-by: James McMullan [email protected] Signed-off-by: James McMullan [email protected]
1 parent d1b6e4b commit 1e8562f

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

.github/workflows/Jirabot.yml

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
jirabot:
1515
runs-on: ubuntu-20.04
1616
steps:
17-
- uses: "actions/setup-python@v2"
17+
- uses: "actions/setup-python@v5"
1818
with:
1919
python-version: "3.8"
2020
- name: "Install dependencies"
@@ -23,9 +23,8 @@ jobs:
2323
python -VV
2424
python -m site
2525
python -m pip install --upgrade pip setuptools wheel
26-
python -m pip install --upgrade jira
26+
python -m pip install --upgrade atlassian-python-api
2727
python -m pip --version
28-
python -m pip freeze | grep jira
2928
- name: "Run"
3029
env:
3130
JIRABOT_USERNAME : ${{ secrets.JIRABOT_USERNAME }}
@@ -43,49 +42,61 @@ jobs:
4342
run: |
4443
import os
4544
import re
45+
import time
46+
import sys
4647
import json
47-
from jira.client import JIRA
48+
from atlassian.jira import Jira
4849
49-
def updateIssue(jira, issue, prAuthorEmail : str, transitionMap: dict, propertyMap: dict, pull_url: str) -> str:
50+
def updateIssue(jira, issue, prAuthor : str, transitionMap: dict, propertyMap: dict, pull_url: str) -> str:
5051
result = ''
5152
52-
statusName = str(issue.fields.status)
53+
issueName = issue['key']
54+
issueFields = issue['fields']
55+
56+
statusName = str(issueFields['status']['name'])
5357
transition = transitionMap.get(statusName, None)
5458
5559
if transition == None:
5660
print('Error: Unable to find transition for status: ' + statusName)
5761
elif transition != '':
5862
try:
59-
jira.transition_issue(issue, transition)
63+
jira.issue_transition(issueName, transition)
6064
result += 'Workflow Transition: ' + transition + '\n'
6165
except Exception as error:
62-
transitions = jira.transitions(issue)
66+
transitions = jira.get_issue_transitions(issueName)
6367
result += 'Error: Transition: "' + transition + '" failed with: "' + str(error) + '" Valid transitions=' + str(transitions) + '\n'
6468
6569
prFieldName = propertyMap.get('pullRequestFieldName', 'customfield_10010')
66-
try:
67-
currentPR = getattr(issue.fields, prFieldName)
68-
except:
70+
71+
if prFieldName in issueFields:
72+
currentPR = issueFields[prFieldName]
73+
else:
74+
print('Error: Unable to find pull request field with field name: ' + prFieldName)
6975
currentPR = None
70-
print('Error: Unable to get current pull request with field name: ' + prFieldName)
7176
7277
if currentPR is None:
73-
issue.update(fields={prFieldName: pull_url})
78+
jira.update_issue_field(issueName, {prFieldName: pull_url})
7479
result += 'Updated PR\n'
7580
elif currentPR is not None and currentPR != pull_url:
7681
result += 'Additional PR: ' + pull_url + '\n'
7782
78-
if prAuthorEmail:
79-
if issue.fields.assignee is None:
80-
jira.assign_issue(issue, prAuthorEmail)
81-
result += 'Assigning user: ' + prAuthorEmail + '\n'
83+
if prAuthor:
84+
assignee = issueFields['assignee']
85+
if assignee is None:
86+
assigneeId = ''
87+
assigneeEmail = ''
8288
else:
83-
assigneeEmail = None
84-
if issue.fields.assignee:
85-
assigneeEmail = issue.fields.assignee.emailAddress
86-
if assigneeEmail is None or assigneeEmail.lower() != assigneeEmail.lower():
87-
result += 'Changing assignee from: ' + assigneeEmail + ' to: ' + prAuthorEmail + '\n'
88-
jira.assign_issue(issue, prAuthorEmail)
89+
assigneeId = assignee['accountId']
90+
assigneeEmail = assignee["emailAddress"]
91+
92+
prAuthorId = prAuthor["accountId"]
93+
prAuthorEmail = prAuthor["emailAddress"]
94+
if assigneeId is None or assigneeId == '':
95+
jira.assign_issue(issueName, prAuthorId)
96+
result += 'Assigning user: ' + prAuthorEmail + '\n'
97+
elif assigneeId != prAuthorId:
98+
result += 'Changing assignee from: ' + assigneeEmail + ' to: ' + prAuthorEmail + '\n'
99+
jira.assign_issue(issueName, prAuthorId)
89100
90101
return result
91102
@@ -114,34 +125,20 @@ jobs:
114125
prAuthor = userDict.get(prAuthor)
115126
print('Mapped Github user to Jira user: ' + prAuthor)
116127
117-
options = {
118-
'server': jira_url
119-
}
120-
121-
jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass))
128+
jira = Jira(url=jira_url, username= jirabot_user, password= jirabot_pass, cloud=True)
122129
123-
# Need to change how we find users for Jira Cloud, unfortunately the API doesn't provide this information.
124-
# At the moment checking if the URL contains atlassian.net appears to be the easiest way to determine if it's Jira Cloud.
125-
isJiraCloud = False
126-
if jira_url.find('atlassian.net') > 0:
127-
isJiraCloud = True
128-
129-
if isJiraCloud:
130-
res = jira.search_users(query=prAuthor)
131-
if res and len(res) > 0:
132-
jiraUser = res[0]
130+
jiraUser = None
131+
userSearchResults = jira.user_find_by_user_string(query=prAuthor)
132+
if userSearchResults and len(userSearchResults) > 0:
133+
jiraUser = userSearchResults[0]
133134
else:
134-
jiraUser = jira.user(prAuthor)
135-
136-
jiraUserEmail = None
137-
if jiraUser is None:
138135
print('Error: Unable to find Jira user: ' + prAuthor + ' continuing without assigning')
139-
else:
140-
jiraUserEmail = jiraUser.emailAddress
141136
142-
print("Jira User Email:" + str(jiraUserEmail))
137+
if not jira.issue_exists(issue_name):
138+
sys.exit('Error: Unable to find Jira issue: ' + issue_name)
139+
else:
140+
issue = jira.issue(issue_name)
143141
144-
issue = jira.issue(issue_name)
145142
result = 'Jirabot Action Result:\n'
146143
147144
transitionMap = json.loads(os.environ['JIRA_ISSUE_TRANSITION_MAP'])
@@ -154,10 +151,18 @@ jobs:
154151
print('Error: JIRA_ISSUE_PROPERTY_MAP is not a valid JSON object, ignoring.')
155152
jiraIssuePropertyMap = {}
156153
157-
result += updateIssue(jira, issue, jiraUserEmail, transitionMap, jiraIssuePropertyMap, pull_url)
158-
jira.add_comment(issue, result)
154+
result += updateIssue(jira, issue, jiraUser, transitionMap, jiraIssuePropertyMap, pull_url)
155+
jira.issue_add_comment(issue_name, result)
156+
157+
result = 'Jira Issue: ' + jira_url + '/browse/' + issue_name + '\n\n' + result
158+
159+
# Escape the result for JSON
160+
result = json.dumps(result)
161+
162+
curlCommand = 'curl -X POST %s -H "Content-Type: application/json" -H "Authorization: token %s" --data \'{ "body": %s }\'' % ( comments_url, github_token, result )
163+
os.system(curlCommand)
159164
else:
160165
print('Unable to find Jira issue name in title')
161166
162167
print(result)
163-
shell: python
168+
shell: python

0 commit comments

Comments
 (0)