@@ -23,81 +23,146 @@ jobs:
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
+ python -m pip --version
27
28
- name : " Run"
28
- env :
29
+ env :
29
30
JIRABOT_USERNAME : ${{ secrets.JIRABOT_USERNAME }}
30
31
JIRABOT_PASSWORD : ${{ secrets.JIRABOT_PASSWORD }}
31
- JIRA_URL : ${{ secrets .JIRA_URL }}
32
+ JIRA_URL : ${{ vars .JIRA_URL }}
32
33
PULL_REQUEST_NUMBER : ${{ github.event.pull_request.number }}
33
34
PULL_REQUEST_TITLE : ${{ github.event.pull_request.title }}
34
35
PULL_REQUEST_AUTHOR_NAME : ${{ github.event.pull_request.user.login }}
35
36
PULL_URL : ${{ github.event.pull_request.html_url }}
36
37
COMMENTS_URL : ${{ github.event.pull_request.comments_url }}
37
38
GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
38
-
39
+ GHUB_JIRA_USER_MAP : ${{ vars.GHUB_JIRA_USER_MAP }}
40
+ JIRA_ISSUE_PROPERTY_MAP : ${{ vars.JIRA_ISSUE_PROPERTY_MAP }}
41
+ JIRA_ISSUE_TRANSITION_MAP : ${{ vars.JIRA_ISSUE_TRANSITION_MAP }}
39
42
run : |
40
43
import os
41
44
import re
42
- from jira.client import JIRA
43
-
45
+ import time
46
+ import sys
47
+ import json
48
+ from atlassian.jira import Jira
49
+
50
+ def updateIssue(jira, issue, prAuthor : str, transitionMap: dict, propertyMap: dict, pull_url: str) -> str:
51
+ result = ''
52
+
53
+ issueName = issue['key']
54
+ issueFields = issue['fields']
55
+
56
+ statusName = str(issueFields['status']['name'])
57
+ transition = transitionMap.get(statusName, None)
58
+
59
+ if transition == None:
60
+ print('Error: Unable to find transition for status: ' + statusName)
61
+ elif transition != '':
62
+ try:
63
+ jira.issue_transition(issueName, transition)
64
+ result += 'Workflow Transition: ' + transition + '\n'
65
+ except Exception as error:
66
+ transitions = jira.get_issue_transitions(issueName)
67
+ result += 'Error: Transition: "' + transition + '" failed with: "' + str(error) + '" Valid transitions=' + str(transitions) + '\n'
68
+
69
+ prFieldName = propertyMap.get('pullRequestFieldName', 'customfield_10010')
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)
75
+ currentPR = None
76
+
77
+ if currentPR is None:
78
+ jira.update_issue_field(issueName, {prFieldName: pull_url})
79
+ result += 'Updated PR\n'
80
+ elif currentPR is not None and currentPR != pull_url:
81
+ result += 'Additional PR: ' + pull_url + '\n'
82
+
83
+ if prAuthor:
84
+ assignee = issueFields['assignee']
85
+ if assignee is None:
86
+ assigneeId = ''
87
+ assigneeEmail = ''
88
+ else:
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)
100
+
101
+ return result
102
+
44
103
jirabot_user = os.environ['JIRABOT_USERNAME']
45
104
jirabot_pass = os.environ['JIRABOT_PASSWORD']
46
105
jira_url = os.environ['JIRA_URL']
47
106
pr = os.environ['PULL_REQUEST_NUMBER']
48
107
title = os.environ['PULL_REQUEST_TITLE']
49
- user = os.environ['PULL_REQUEST_AUTHOR_NAME']
50
- comments_url = os.environ['COMMENTS_URL']
108
+ prAuthor = os.environ['PULL_REQUEST_AUTHOR_NAME']
51
109
pull_url = os.environ['PULL_URL']
52
110
github_token = os.environ['GITHUB_TOKEN']
53
-
54
- print("%s %s %s" % (title, user, comments_url))
55
- status = ''
56
- issuem = re.search("(HPCC|HH|IDE|EPE|ML|JAPI)-[0-9]+", title)
111
+ comments_url = os.environ['COMMENTS_URL']
112
+
113
+ print("%s %s %s" % (title, prAuthor, comments_url))
114
+ result = ''
115
+ issuem = re.search("(HPCC|HH|IDE|EPE|ML|HPCC4J|JAPI)-[0-9]+", title)
57
116
if issuem:
58
117
issue_name = issuem.group()
59
- if user == 'dehilsterlexis':
60
- user = 'dehilster'
61
- if user == 'kunalaswani':
62
- user = 'kunal.aswani'
63
- if user == 'timothyklemm':
64
- user = 'klemti01'
65
- if user == 'jpmcmu':
66
- user = 'mcmuja01'
67
- if user == 'asselitx':
68
- user = 'terrenceasselin'
69
- if user == 'jeclrsg':
70
- user = 'clemje01'
71
- if user == 'jackdelv':
72
- user = 'delvecja'
73
- options = {
74
- 'server': jira_url
75
- }
76
- jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass))
77
- issue = jira.issue(issue_name)
78
- status = jira_url + '/browse/' + issue_name + '\\n'
79
- if False and issue.fields.status.name != 'Active' and issue.fields.status.name != 'Open' and issue.fields.status.name != 'New' and issue.fields.status.name != 'Discussing' and issue.fields.status.name != 'Awaiting Information':
80
- status += 'Jira not updated (state was not active or new)'
81
- elif issue.fields.customfield_10010 != None:
82
- if issue.fields.customfield_10010 != pull_url:
83
- status += 'Jira not updated (pull request "%s" already registered)' % issue.fields.customfield_10010
84
- else:
85
- status += 'This pull request is already registered'
86
- elif issue.fields.assignee is not None and issue.fields.assignee.name.lower() != user.lower():
87
- status += 'Jira not updated (user does not match)'
118
+
119
+ userDict = json.loads(os.environ['GHUB_JIRA_USER_MAP'])
120
+ if not isinstance(userDict, dict):
121
+ userDict = {}
122
+
123
+ if prAuthor in userDict:
124
+ prAuthor = userDict.get(prAuthor)
125
+ print('Mapped Github user to Jira user: ' + prAuthor)
126
+
127
+ jira = Jira(url=jira_url, username= jirabot_user, password= jirabot_pass, cloud=True)
128
+
129
+ jiraUser = None
130
+ userSearchResults = jira.user_find_by_user_string(query=prAuthor)
131
+ if userSearchResults and len(userSearchResults) > 0:
132
+ jiraUser = userSearchResults[0]
133
+ else:
134
+ print('Error: Unable to find Jira user: ' + prAuthor + ' continuing without assigning')
135
+
136
+ if not jira.issue_exists(issue_name):
137
+ sys.exit('Error: Unable to find Jira issue: ' + issue_name)
88
138
else:
89
- if issue.fields.assignee is None:
90
- jira.assign_issue(issue, user)
91
- issue.update(fields={'customfield_10010': pull_url})
92
139
issue = jira.issue(issue_name)
93
- try:
94
- transitions = jira.transitions(issue)
95
- jira.transition_issue(issue, '291') # Attach Pull Request
96
- except:
97
- status += 'Failed to set to merge pending: transitions=%s' % transitions
98
- status += 'Jira updated'
99
- print('curl -X POST %s -H "Content-Type: application/json" -H "Authorization: token %s" --data \'{ "body": "%s" }\'' % ( comments_url, github_token, status ))
100
- os.system('curl -X POST %s -H "Content-Type: application/json" -H "Authorization: token %s" --data \'{ "body": "%s" }\'' % ( comments_url, github_token, status ))
101
-
102
- print(status)
103
- shell : python
140
+
141
+ result = 'Jirabot Action Result:\n'
142
+
143
+ transitionMap = json.loads(os.environ['JIRA_ISSUE_TRANSITION_MAP'])
144
+ if not isinstance(transitionMap, dict):
145
+ print('Error: JIRA_ISSUE_TRANSITION_MAP is not a valid JSON object, ignoring.')
146
+ transitionMap = {}
147
+
148
+ jiraIssuePropertyMap = json.loads(os.environ['JIRA_ISSUE_PROPERTY_MAP'])
149
+ if not isinstance(jiraIssuePropertyMap, dict):
150
+ print('Error: JIRA_ISSUE_PROPERTY_MAP is not a valid JSON object, ignoring.')
151
+ jiraIssuePropertyMap = {}
152
+
153
+ result += updateIssue(jira, issue, jiraUser, transitionMap, jiraIssuePropertyMap, pull_url)
154
+ jira.issue_add_comment(issue_name, result)
155
+
156
+ result = 'Jira Issue: ' + jira_url + '/browse/' + issue_name + '\n\n' + result
157
+
158
+ # Escape the result for JSON
159
+ result = json.dumps(result)
160
+
161
+ curlCommand = 'curl -X POST %s -H "Content-Type: application/json" -H "Authorization: token %s" --data \'{ "body": %s }\'' % ( comments_url, github_token, result )
162
+ print(curlCommand)
163
+ os.system(curlCommand)
164
+ else:
165
+ print('Unable to find Jira issue name in title')
166
+
167
+ print(result)
168
+ shell : python
0 commit comments