forked from puppetlabs/ticketmatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticketmatch.rb
465 lines (406 loc) · 13.4 KB
/
ticketmatch.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# This script mangles the output from git log between two git references
# and matches this with a list of tickets from Jira that corresponds to
# a project and a fixVersion
#
# You must cd to the git repo for this to work.
#
require 'rubygems'
require 'highline/import' # see https://github.com/JEG2/highline
require 'json'
require 'optparse'
require 'base64'
# store the basic information of a git log message
class GitEntry
attr_reader :hash, :description
attr_accessor :revert_entry, :has_revert_parent
def initialize(hash, description)
@hash = hash
@description = description
# The GitEntry that is a revert of this commit
@revert_entry = nil
# we set this in the revert entry so we can avoid printing it twice
@has_revert_parent = false
end
def <=>(another_entry)
[self.hash, self.description] <=> [another_entry.hash, another_entry.description]
end
end
# hold all the git commit log messages, but group them based on their ticket number or "type"
class GitCommit
def initialize
@git_entries = Hash.new
end
def add_git_commit_line(line)
# is this a revert?
m = line.match(/^([0-9a-fA-F]+)\s+(Revert ".*")$/)
unless m.nil?
hash = m[1]
description = m[2]
add_git_entry('REVERT', hash, description)
return
end
m = line.match(/^([0-9a-fA-F]+)\s+(?:\(([^\)]*)\))?(.*)$/)
if m[2].nil?
hash = m[1]
description = m[3]
add_git_entry('UNMARKED', hash, description)
else
type = m[2].upcase
hash = m[1]
description = m[3].lstrip
add_git_entry(type, hash, description)
end
end
def add_git_entry(type, hash, description)
if @git_entries[type].nil?
@git_entries[type] = [GitEntry.new(hash, description)]
else
@git_entries[type] << GitEntry.new(hash, description)
end
end
def set_has_revert_parent_for_entry(entry)
@git_entries.each_key do |ticket|
@git_entries[ticket].length.times do |entry_offset|
if @git_entries[ticket][entry_offset] == entry
@git_entries[ticket][entry_offset].has_revert_parent = true
end
end
end
end
def find_commit_that_matches_description(description_pattern)
@git_entries.each_key do |ticket|
@git_entries[ticket].length.times do |entry_offset|
if @git_entries[ticket][entry_offset].description =~ description_pattern
return @git_entries[ticket][entry_offset]
end
end
end
return nil
end
# we have to look 2 ways for reverts to find them all
# 1 - look using the "(TICKET) <description>"
# 2 - look using the "Revert "<description>""
def associate_reverts
@git_entries.each_key do |ticket|
@git_entries[ticket].length.times do |entry_offset|
# look for reverts based on the ticket name and description string
description_pattern = /^Revert "\(#{ticket}\) #{Regexp.escape(@git_entries[ticket][entry_offset].description)}"$/
revert = find_commit_that_matches_description(description_pattern)
unless revert.nil?
@git_entries[ticket][entry_offset].revert_entry = revert
set_has_revert_parent_for_entry(revert)
end
# look for reverts based on the Revert string using just the description
description_pattern = /^Revert "#{Regexp.escape(@git_entries[ticket][entry_offset].description)}"$/
revert = find_commit_that_matches_description(description_pattern)
unless revert.nil?
@git_entries[ticket][entry_offset].revert_entry = revert
set_has_revert_parent_for_entry(revert)
end
end
end
end
def keys
@git_entries.keys
end
def [](key)
@git_entries[key]
end
def empty?
@git_entries.empty?
end
def inspect
string = ""
@git_entries.each_key do |key|
string += "{#{key}: #{@git_entries[key].inspect} }\n"
end
string
end
end
# the basic state of a jira ticket
#
class JiraTicket
attr_accessor :in_git
attr_reader :state, :issuetype, :team, :rn_summary, :key
def initialize(key, state, issuetype, team, rn_summary, in_git=0)
@key = key
@state = state
@issuetype = issuetype
@team = team
@in_git = in_git # This is a count, if its its non-zero then something was still checked in for this ticket
end
def to_s
sprintf("%s\t%-22s %s", key, state, "(#{team || 'Unassigned'})")
end
end
# A grouping of Jira tickets that apply for this matching session
#
class JiraTickets
attr_reader :unresolved, :missing_release_notes
def initialize
@tickets = Hash.new
@unresolved = Array.new
@missing_release_notes = Array.new
end
def [](ticket)
@tickets[ticket]
end
def keys
@tickets.keys
end
def add_ticket(key, state, issuetype, team, rn_summary, in_git=0)
ticket = JiraTicket.new(key, state, issuetype, team, rn_summary, in_git)
@tickets[key] = ticket
unless state =~ /(Closed|Resolved)/
@unresolved << ticket
end
# Epics in Perforce's Jira instance do not have a visible release note
# summary field, so we do not add Epics to the @missing_release_notes array
@missing_release_notes << ticket if rn_summary.nil? && issuetype != 'Epic'
end
def empty?
@tickets.empty?
end
def inspect
string = ""
@tickets.each_key do |key|
string += "{#{key}: #{@tickets[key].inspect} }\n"
end
string
end
end
git_from_rev = nil
git_to_rev = nil
jira_project_name = nil
jira_project_fixed_version = nil
jira_team_name = nil
jira_auth_token = nil
interactive = true
parser = OptionParser.new do |opts|
opts.banner = 'Usage: ruby ticketmatch.rb [options]'
opts.on('-f', '--from from_rev', 'from git revision') do |from_rev|
git_from_rev = from_rev;
end
opts.on('-t', '--to to_rev', 'to git revision') do |to_rev|
git_to_rev = to_rev;
end
opts.on('-p', '--project JIRA_project', 'JIRA project ID') do |project_name|
jira_project_name = project_name;
end
opts.on('-v', '--version version_fixed_in', 'JIRA "fixed-in" version (in quotes for now, please)') do |fixed_version|
jira_project_fixed_version = fixed_version;
end
opts.on('-m', '--team JIRA_team', 'JIRA team assigned tickets within JIRA project') do |team_name|
jira_team_name = team_name;
end
opts.on('-c', '--ci', 'continuous integration mode (no prompting)') do
interactive = false;
end
opts.on('-a', '--jira-auth-token token', 'personal access token for JIRA authentication') do |auth_token|
jira_auth_token = auth_token;
end
opts.on('-h', '--help', 'this message') do
puts opts
exit 1
end
end
parser.parse!
# check if we are in a git tree or not
#
in_repo = %x{git rev-parse --is-inside-work-tree 2>/dev/null}
unless in_repo.chomp == "true"
say('ERROR: Please run ticketmatch from a git repo directory')
exit 1
end
if git_from_rev == nil
if interactive
git_from_rev = ask('Enter Git From Rev: ')
else
abort('ERROR: must specify a Git from revision')
end
end
if git_to_rev == nil
if interactive
git_to_rev = ask('Enter Git To Rev: ') {|q| q.default = 'master'}
else
abort('ERROR: must specify a Git to revision')
end
end
# Get the log from git
# process and store in a hash per user entered ticket reference
#
git_commits = GitCommit.new
git_log = %x{git log --no-merges --oneline #{git_from_rev}..#{git_to_rev}}
git_log.each_line do |line|
git_commits.add_git_commit_line(line)
end
if git_commits.empty?
say("No results found in git log for range #{git_from_rev}..#{git_to_rev}")
exit 0
end
# associate the reverts
#
git_commits.associate_reverts
# collect the Jira information
#
if jira_project_name == nil
if interactive
jira_project_name = ask('Enter JIRA project: ') {|q| q.default = 'PUP'}
else
abort('ERROR: must specify a JIRA project ID')
end
end
if jira_project_fixed_version == nil
if interactive
jira_project_fixed_version = ask('Enter JIRA fix version: ') do |q|
q.default = "#{jira_project_name} #{git_to_rev}"
end
else
abort('ERROR: must specify a JIRA fix version')
end
end
if jira_team_name == nil
if interactive
jira_team_name = ask('(Optional) Enter JIRA team name: ')
end
end
jira_team_name = nil if jira_team_name == ""
query = "project = #{jira_project_name}"
# get the list of tickets from the JIRA project that contain the fixed version
jira_data = {
:jql => query + " AND fixVersion = \"#{jira_project_fixed_version}\" ORDER BY key",
:maxResults => -1,
:fields => ['status', 'issuetype', 'customfield_10067', 'customfield_10064']
}
# Process file with Jira issues
jira_post_data = JSON.fast_generate(jira_data)
jira_auth_header = "-H 'Authorization: Basic #{jira_auth_token}'"
begin
jira_issues = JSON.parse(%x{curl -s -S -X POST -H 'Content-Type: application/json' #{jira_auth_header} --data '#{jira_post_data}' https://perforce.atlassian.net/rest/api/2/search})
rescue
say('Unable to obtain list of issues from JIRA')
exit(status=1)
end
if jira_issues['issues'].nil?
say("JIRA returned no results for project '#{jira_project_name}' and fix version '#{jira_project_fixed_version}'")
say("<%= color(%Q[#{jira_issues['errorMessages'].join}], RED) %>") if jira_issues['errorMessages']
exit 0
end
jira_tickets = JiraTickets.new
jira_issues['issues'].each do |issue|
jira_tickets.add_ticket(issue['key'],
issue['fields']['status']['name'],
issue['fields']['issuetype']['name'],
issue.dig('fields', 'customfield_10067', 'value'),
issue.dig('fields', 'customfield_10064'),
in_git=0)
end
if jira_tickets.empty?
say("JIRA returned no results for project '#{jira_project_name}' and fix version '#{jira_project_fixed_version}'")
exit 0
end
# Print list of issues sorted, for each show sha + comment after reference
#
git_commits.keys.sort.each do |ticket|
if jira_tickets[ticket].nil?
puts "** #{ticket.upcase}"
else
puts "-- #{ticket.upcase} (#{jira_tickets[ticket].state})"
end
git_commits[ticket].each do |git_entry|
next if git_entry.has_revert_parent # skip this entry if its a revert with a parent, (the parent will print it)
# list this entry and any of its
puts " #{git_entry.hash} #{git_entry.description}"
revert_ticket = git_entry.revert_entry
until revert_ticket.nil?
puts " R #{revert_ticket.hash} #{revert_ticket.description}"
revert_ticket = revert_ticket.revert_entry
end
end
end
# figure out what is in git or not based on taking reverts into account
# we add +1 for the initial code and then -1, or +1 depending on the level of the revert
# if the number is non-zero then this ticket had a code change
#
git_commits.keys.each do |ticket|
# if we have no jira ticket for this one, move on
next if jira_tickets[ticket].nil?
git_commits[ticket].each do |git_entry|
# if this git_entry has a parent revert_ticket, let that one do the counting
next if git_entry.has_revert_parent
# add 1 for this ticket adding code
jira_tickets[ticket].in_git += 1
in_git = -1
revert_ticket = git_entry.revert_entry
until revert_ticket.nil?
# now add either -1 or +1 depending on Revert level
jira_tickets[ticket].in_git += in_git
revert_ticket = revert_ticket.revert_entry
in_git = -in_git
end
end
end
puts
puts '----- Git commits in Jira -----'
known_jira_tickets = jira_tickets.keys
unknown_issues = git_commits.keys.reject do |ticket|
known_jira_tickets.include?(ticket) || ["MAINT", "DOC", "DOCS", "TRIVIAL", "PACKAGING", "UNMARKED"].include?(ticket)
end
if !unknown_issues.empty?
say("<%= color('COMMIT TOKENS NOT FOUND IN JIRA (OR NOT WITH FIX VERSION OF #{jira_project_fixed_version})', RED) %>")
unknown_issues.sort.each do |ticket|
if ticket == 'REVERT'
git_commits[ticket].each do |revert_ticket|
say("<%= color('REVERT #{revert_ticket.hash}', RED) %>")
end
else
say("<%= color(%Q[#{ticket}], RED) %>")
end
end
else
say("<%= color('ALL COMMIT TOKENS WERE FOUND IN JIRA', GREEN) %>")
end
puts
puts '----- Unresolved Jira tickets not in git commits -----'
unresolved_tickets = jira_tickets.unresolved
if jira_team_name
unresolved_tickets.reject! do |ticket|
ticket.team != jira_team_name
end
end
unresolved_not_in_git, unresolved_in_git = unresolved_tickets.partition {|ticket| ticket.in_git < 1}
if !unresolved_not_in_git.empty?
say("<%= color('UNRESOLVED ISSUES NOT FOUND IN GIT', RED) %>")
unresolved_not_in_git.each do |ticket|
say("<%= color(%Q[#{ticket}], RED) %>")
end
else
say("<%= color('ALL ISSUES WERE FOUND IN GIT', GREEN) %>")
end
puts
puts '----- Unresolved Jira tickets found in git commits -----'
if !unresolved_in_git.empty?
say("<%= color('UNRESOLVED ISSUES FOUND IN GIT', RED) %>")
unresolved_in_git.each do |ticket|
say("<%= color(%Q[#{ticket}], RED) %>")
end
else
say("<%= color('ALL ISSUES WERE RESOLVED IN JIRA', GREEN) %>")
end
puts
puts '----- Tickets missing release notes -----'
tickets_missing_release_notes = jira_tickets.missing_release_notes
if jira_team_name
tickets_missing_release_notes.reject! do |ticket|
ticket.team != jira_team_name
end
end
if !tickets_missing_release_notes.empty?
say("<%= color('ISSUES MISSING RELEASE NOTES', RED) %>")
tickets_missing_release_notes.each do |ticket|
say("<%= color(%Q[#{ticket}], RED) %>")
end
else
say("<%= color('ALL ISSUES CONTAIN RELEASE NOTES', GREEN) %>")
end
exit 0