-
Notifications
You must be signed in to change notification settings - Fork 3
/
process.rb
414 lines (373 loc) · 12.2 KB
/
process.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
require 'json'
require 'rest-client'
require './config.rb'
require './jiraGitDemLabels.rb'
#returns an array of labels applied to a pull request
def get_labels (pull_request)
labels_url = pull_request["issue_url"] + "/labels"
labels = JSON.parse( RestClient.get( labels_url, {:params => {:access_token => GIT_HUB_TOKEN}, :accept => :json} ) )
return labels
end
#returns an array of jira issues associated with a pull request
#if there are more jira issues in the pull request title than in the branch, return the issues in the title
def get_jira_issues (code, type, is_jitr)
if type == "branch"
if is_jitr == true
jira_issues = code.scan(/(?:|^)(JTQ-[0-9]+|JRQ-[0-9]+|JCEQ-[0-9]+|JITR.+-[0-9]+|TOOLSONE-[0-9]+)(?=|$)/)
else
jira_issues = code.scan(/(?:|^)([A-Za-z]+-[0-9]+)(?=|$)/)
end
elsif type == "pull_request"
if is_jitr == true
issues_in_branch = code["head"]["ref"].scan(/(?:|^)(JTQ-[0-9]+|JRQ-[0-9]+|JCEQ-[0-9]+|JITR.+-[0-9]+|TOOLSONE-[0-9]+)(?=|$)/)
issues_in_pull_request_title = code["title"].scan(/(?:\s|^)(JTQ-[0-9]+|JRQ-[0-9]+|JCEQ-[0-9]+|JITR.+-[0-9]+|TOOLSONE-[0-9]+)(?=\s|$)/)
else
issues_in_branch = code["head"]["ref"].scan(/(?:|^)([A-Za-z]+-[0-9]+)(?=|$)/)
issues_in_pull_request_title = code["title"].scan(/(?:\s|^)([A-Za-z]+-[0-9]+)(?=\s|$)/)
end
if issues_in_branch.length > issues_in_pull_request_title.length
jira_issues = issues_in_branch
else
jira_issues = issues_in_pull_request_title
end
end
return jira_issues
end
#returns message of the latest commit for a pull request
def get_latest_commit_message (pull_request, commits_url)
url = commits_url.split('{')[0] + '/' + pull_request["head"]["sha"]
latest_commit_info = JSON.parse(RestClient.get( url, {:params => {:access_token => GIT_HUB_TOKEN}, :accept => :json} ) )
latest_commit_message = latest_commit_info["commit"]["message"]
return latest_commit_message
end
#returns jira markdown for the user
#if this is a thrillist user, we assume their email is their jira name
#some user have special treatment because of the way they are setup
def get_user (user_object)
user_info = JSON.parse(RestClient.get( user_object["url"], {:params => {:access_token => GIT_HUB_TOKEN}, :accept => :json} ) )
#Example: msolomon@thrillist.com
#user_email_domain = thrillist.com
#user_email_prefix = msolomon
if user_info["email"] != nil
user_email_domain = user_info["email"].split('@')[1]
user_email_prefix = user_info["email"].split('@')[0]
#convert prefix to JIRA markdown or a link to github name if email domain is not thrillist
if user_email_domain == "thrillist.com"
user = user_email_prefix.insert(0, "[~") + "]"
end
else
user = "["+user_object["login"]+"|"+user_object["html_url"]+"]"
end
#overwrite special cases
case user_object["login"]
when "kpeltzer"
user = "[~kpeltzer]"
when "ken"
user = "[~kpeltzer]"
when "gilchenzion"
user = "[~gchenzion]"
when "deanmazurek"
user = "[~dean]"
when "kwadwo"
user = "[~kboateng]"
when "tarasiegel"
user = "[~tsiegel]"
when "samiamorwas"
user = "[~mhaarhaus]"
when "cahalanej"
user = "[~jcahalane]"
when "bdeane"
user = "[~brian-thrillist]"
when "jay"
user = "[~jchinthrajah]"
when "jay-thrillist"
user = "[~jchinthrajah]"
when "patrick"
user = "[~plange]"
when "mvujovic-thrillist"
user = "[~mvujovic]"
when "mattdoyle"
user = "[~matt]"
when "laurenashpole"
user = "[~lashpole]"
when "stefsic"
user = "[~ssicurelli]"
when "mga226"
user = "[~macreman]"
when "lmon"
user = "[~lukemonaco]"
when "schuylerpenny"
user = "[~spenny]"
when "rhinoray29"
user = "[~rshatkin]"
when "khalid-richards"
user = "[~krichards]"
when "THRILL-jacinto"
user = "[~jacinto]"
when "rachel-thrillist"
user = "[~rshatkin]"
when "emchale"
user = "[~emchale]"
end
return user
end
def update_label_jira (jira_issues, current_label, pull_request_labels, user)
i = 0
while (i < jira_issues.length) do
jira_issue = jira_issues[i].join
#if the user labeled the pull request with QAed and the pull request is already labeled with reviewed, move to deploy ready
if current_label == "QAed" && jira_issue != nil
#move to qaed by user
transition_issue jira_issue, QA_PASSED_ID, user
#if this ticket is also reviewed, move to deploy ready
if pull_request_labels.find {|x| x["name"] == "reviewed"} != nil
transition_issue jira_issue, DEPLOY_READY_ID, user
end
elsif current_label == "reviewed" && jira_issue != nil
#move to reveiwed by user
transition_issue jira_issue, QA_READY_ID, user
#if this ticket is also QAed, move to deploy ready
if pull_request_labels.find {|x| x["name"] == "QAed"} != nil
transition_issue jira_issue, DEPLOY_READY_ID, user
end
elsif current_label == "Production verified" && jira_issue != nil
#move to production verified by user
transition_issue jira_issue, PRODUCTION_VERIFIED_ID, user
else
#dont need to do anything for this label
end
i+=1
end
end
def update_message_jira (jira_issues, pull_request, latest_commit_message, user, is_jitr)
if is_jitr == true
qa_transition_id = QA_READY_JITR_ID
else
qa_transition_id = QA_READY_ID
end
#if someone entered a message in their pull request commit with #comment, it will
#already show up in Jira so there is no need to post it with this app
if latest_commit_message.scan(/(?:\s|^)([A-Za-z]+-[0-9]+).+(#comment)(?=\s|$)/).length > 0
apply_comment = false
else
apply_comment = true
end
#loop through all of the tickets associated with the pull request
#update with the comment of latest commit if necessary and then move to QA
i = 0;
while (i < jira_issues.length) do
jira_issue = jira_issues[i].join
if apply_comment == true
transition_issue jira_issue, qa_transition_id, user, pull_request, "updated", latest_commit_message
end
i+=1
end
end
#loops through all of the issues given as a parameter and sends them to the transition function
#need to know if a batch of tickets belongs to JITR team because they use different transition IDs
def start_qa (jira_issues, pull_request, user, is_jitr)
if is_jitr == true
qa_transition_id = QA_READY_JITR_ID
else
qa_transition_id = QA_READY_ID
end
i = 0;
while (i < jira_issues.length) do
jira_issue = jira_issues[i].join
transition_issue jira_issue, qa_transition_id, user, pull_request, "opened"
i+=1
end
end
#loops through all of the issues given as a parameter and sends them to the transition function
#need to know if a batch of tickets belongs to JITR team because they use different transition IDs
def start_code_review (jira_issues, pull_request, user)
code_review_transition_id = CODE_REVIEW_ID
i = 0;
while (i < jira_issues.length) do
jira_issue = jira_issues[i].join
transition_issue jira_issue, code_review_transition_id, user, pull_request, "opened"
i+=1
end
end
def start_qa_for_epic (epic, user)
transition_issue epic, QA_READY_ID, user
end
def start_progress (jira_issues, user, *branch)
i = 0;
if branch[0] != nil
while (i < jira_issues.length) do
jira_issue = jira_issues[i].join
transition_issue jira_issue, START_PROGRESS_JITR_ID, user, branch[0], "created", "jitr"
i+=1
end
else
while (i < jira_issues.length) do
jira_issue = jira_issues[i]
transition_issue jira_issue, START_PROGRESS_ID, user
i+=1
end
end
end
#add branch name or pull request name to jira issues
def update_development_info_jira (jira_issues, name, type)
if type == "branch"
field = JIRA_FIELD_BRANCH
elsif type == "pull_request"
field = JIRA_FIELD_PULL_REQUEST
end
i = 0;
while (i < jira_issues.length) do
jira_issue = jira_issues[i].join
data = {
"fields" => {
field => name
}
}.to_json
headers = {
:"Authorization" => "Basic #{JIRA_TOKEN}",
:"Content-Type" => "application/json"
}
if jira_issue =~ /(?:|^)(JQWE-[0-9]+|PQ-[0-9]+|JTQ-[0-9]+|JRQ-[0-9]+|JCEQ-[0-9]+|JITR.+-[0-9]+|TOOLSONE-[0-9]+)(?=|$)/i
url = JACKTHREADS_JIRA_URL + jira_issue
else
url = THRILLIST_JIRA_URL + jira_issue
end
response = RestClient.put( url, data, headers )
i+=1
end
#"customfield_10000" = branch
#"customfield_10123" = pull_request
end
# Accepts 1 Jira issue at a time
# Transitions the issue to the transition ID "update_to"
# User is the person who made an action to trigger the transition
# code_info is an optional array about the code that triggered this event (branches/pull requests)
def transition_issue (jira_issue, update_to, user, *code_info)
if jira_issue =~ /(?:|^)(JTQ-[0-9]+|JRQ-[0-9]+|JCEQ-[0-9]+|JITR.+-[0-9]+|TOOLSONE-[0-9]+)(?=|$)/i
url = JACKTHREADS_JIRA_URL + jira_issue + "/transitions"
elsif jira_issue =~ /(?:|^)(JQWE-[0-9]+|PQ-[0-9]+)(?=|$)/i
# JackThreads front end does not want these transitions anymore
return false
else
url = THRILLIST_JIRA_URL + jira_issue + "/transitions"
end
case update_to
when START_PROGRESS_JITR_ID
body = "Progress started when #{user} created branch: #{code_info[0]} in GitHub"
when START_PROGRESS_ID
body = "Progress started when #{user} began working on a story in this epic"
when CODE_REVIEW_ID
body = "#{user} opened pull request: [#{code_info[0]["title"]}|#{code_info[0]["html_url"]}]. Ready for Code Review"
when QA_READY_JITR_ID
if code_info[0] == nil
body = "A story for this epic has been submitted to QA by #{user}."
elsif code_info[1] == "opened"
body = "#{user} opened pull request: [#{code_info[0]["title"]}|#{code_info[0]["html_url"]}]. Ready for QA"
elsif code_info[1] == "updated"
body = "#{user} updated pull request: [#{code_info[0]["title"]}|#{code_info[0]["html_url"]}] with comment: \n bq. #{code_info[2]}"
end
when QA_READY_ID
if code_info[1] == "updated"
body = "#{user} updated pull request: [#{code_info[0]["title"]}|#{code_info[0]["html_url"]}] with comment: \n bq. #{code_info[2]}"
else
body = "Code review passed by #{user} #{JIRA_REVIEW_IMAGE}"
end
when QA_PASSED_ID
body = "QA passed by #{user} #{JIRA_QA_IMAGE}"
when REVIEW_PASSED_ID
body = "Code review passed by #{user} #{JIRA_REVIEW_IMAGE}"
when DEPLOY_READY_ID
body = "Deploy ready"
end
data = {
"update" => {
"comment" => [
{
"add" => {
"body" => body
}
}
]
},
"transition" => {
"id" => "#{update_to}"
}
}.to_json
#If this is a JITR ticket that's being transitioned, let's make sure we add PRs and Branch names during transition
#I would love to find a way to quickly append this JSON to the existing data object to clean this up
if update_to == START_PROGRESS_JITR_ID || update_to == QA_READY_JITR_ID
#url.concat("?expand=transitions.fields")
case update_to
when START_PROGRESS_JITR_ID
field = JIRA_FIELD_BRANCH
data = {
"update" => {
"comment" => [
{
"add" => {
"body" => body
}
}
],
field => [
{
"set" => "#{code_info[0]}"
}
]
},
"transition" => {
"id" => "#{update_to}"
}
}.to_json
when QA_READY_JITR_ID
field = JIRA_FIELD_PULL_REQUEST
data = {
"update" => {
"comment" => [
{
"add" => {
"body" => body
}
}
],
field => [
{
"set" => "#{code_info[0]["html_url"]}"
}
]
},
"transition" => {
"id" => "#{update_to}"
}
}.to_json
end
data.to_json
end
headers = {
:"Authorization" => "Basic #{JIRA_TOKEN}",
:"Content-Type" => "application/json"
}
#figure out if this issue is able to be transitioned to where we want it to go
#if we can transition it, post to JIRA, if we can't then don't send anything
available_transitions = JSON.parse( RestClient.get( url, headers ) )
able_to_transition = is_able_to_transition update_to, available_transitions
if able_to_transition == true
response = RestClient.post( url, data, headers )
else
puts "cannot transition this ticket"
end
end
#returns true if the ticket's available transitions includes the transition that we want to update to
def is_able_to_transition(update_to, available_transitions)
able_to_transition = false
i = 0
while (i < available_transitions["transitions"].length ) do
available_transition = available_transitions["transitions"][i]
if available_transition["id"] == update_to
able_to_transition = true
i += available_transitions["transitions"].length
end
i += 1
end
return able_to_transition
end