14
14
jirabot :
15
15
runs-on : ubuntu-20.04
16
16
steps :
17
- - uses : " actions/setup-python@v2 "
17
+ - uses : " actions/setup-python@v5 "
18
18
with :
19
19
python-version : " 3.8"
20
20
- name : " Install dependencies"
23
23
python -VV
24
24
python -m site
25
25
python -m pip install --upgrade pip setuptools wheel
26
- python -m pip install --upgrade jira
26
+ python -m pip install --upgrade atlassian-python-api
27
27
python -m pip --version
28
- python -m pip freeze | grep jira
29
28
- name : " Run"
30
29
env :
31
30
JIRABOT_USERNAME : ${{ secrets.JIRABOT_USERNAME }}
@@ -43,49 +42,61 @@ jobs:
43
42
run : |
44
43
import os
45
44
import re
45
+ import time
46
+ import sys
46
47
import json
47
- from jira.client import JIRA
48
+ from atlassian.jira import Jira
48
49
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:
50
51
result = ''
51
52
52
- statusName = str(issue.fields.status)
53
+ issueName = issue['key']
54
+ issueFields = issue['fields']
55
+
56
+ statusName = str(issueFields['status']['name'])
53
57
transition = transitionMap.get(statusName, None)
54
58
55
59
if transition == None:
56
60
print('Error: Unable to find transition for status: ' + statusName)
57
61
elif transition != '':
58
62
try:
59
- jira.transition_issue(issue , transition)
63
+ jira.issue_transition(issueName , transition)
60
64
result += 'Workflow Transition: ' + transition + '\n'
61
65
except Exception as error:
62
- transitions = jira.transitions(issue )
66
+ transitions = jira.get_issue_transitions(issueName )
63
67
result += 'Error: Transition: "' + transition + '" failed with: "' + str(error) + '" Valid transitions=' + str(transitions) + '\n'
64
68
65
69
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)
69
75
currentPR = None
70
- print('Error: Unable to get current pull request with field name: ' + prFieldName)
71
76
72
77
if currentPR is None:
73
- issue.update(fields= {prFieldName: pull_url})
78
+ jira.update_issue_field(issueName, {prFieldName: pull_url})
74
79
result += 'Updated PR\n'
75
80
elif currentPR is not None and currentPR != pull_url:
76
81
result += 'Additional PR: ' + pull_url + '\n'
77
82
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 = ''
82
88
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)
89
100
90
101
return result
91
102
@@ -114,34 +125,20 @@ jobs:
114
125
prAuthor = userDict.get(prAuthor)
115
126
print('Mapped Github user to Jira user: ' + prAuthor)
116
127
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)
122
129
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]
133
134
else:
134
- jiraUser = jira.user(prAuthor)
135
-
136
- jiraUserEmail = None
137
- if jiraUser is None:
138
135
print('Error: Unable to find Jira user: ' + prAuthor + ' continuing without assigning')
139
- else:
140
- jiraUserEmail = jiraUser.emailAddress
141
136
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)
143
141
144
- issue = jira.issue(issue_name)
145
142
result = 'Jirabot Action Result:\n'
146
143
147
144
transitionMap = json.loads(os.environ['JIRA_ISSUE_TRANSITION_MAP'])
@@ -154,10 +151,18 @@ jobs:
154
151
print('Error: JIRA_ISSUE_PROPERTY_MAP is not a valid JSON object, ignoring.')
155
152
jiraIssuePropertyMap = {}
156
153
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)
159
164
else:
160
165
print('Unable to find Jira issue name in title')
161
166
162
167
print(result)
163
- shell : python
168
+ shell : python
0 commit comments