Class: JiraCommentAddHook
Constant Summary
collapse
- Hook =
RubyGitHooks::Hook
- OPTIONS =
[ "protocol", "host", "username", "password",
"api_path", "github", "issues",
"domain", "from", "subject", "via", "via_options", "intro", "conclusion",
"no_send", "check_status"]
- VALID_ERROR_TYPES =
[:no_jira, :invalid_jira]
RubyGitHooks::Hook::HOOK_INFO, RubyGitHooks::Hook::HOOK_TYPE_SETUP
Instance Attribute Summary collapse
Instance Method Summary
collapse
get_hooks_to_run, initial_setup, register, run, run_as_specific_githook, #setup, shell!
Constructor Details
Returns a new instance of JiraCommentAddHook.
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
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 31
def initialize(options = {})
bad_options = options.keys - OPTIONS
raise "JiraCommentAddHook created with unrecognized options: " +
"#{bad_options.inspect}!" if bad_options.size > 0
if !options.has_key?("username") || !options.has_key?("password")
raise "You must provide Jira server user name and password in options"
end
@options = options
@options["protocol"] ||= "https"
@options["host"] ||= "jira"
@options["api_path"] ||= "rest/api/latest/issue"
@options["github"] ||= "github.com"
@options["check_status"] = true if !@options.has_key? "check_status"
@options["domain"] ||= "mydomain.com"
@options["from"] ||= "Jira Jailer <noreply@#{@options["domain"]}>"
@options["subject"] ||= "Use Jira Ticket Numbers, Please!"
@options["via"] ||= "no_send"
@options["via_options"] ||= {}
@errors_to_report = {}
end
|
Instance Attribute Details
#errors_to_report ⇒ Object
Returns the value of attribute errors_to_report.
29
30
31
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 29
def errors_to_report
@errors_to_report
end
|
Instance Method Details
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 192
def (ticket, )
STDERR.puts "ADDING COMMENT for ticket #{ticket}"
uri = build_uri(ticket, "comment")
data = {"body" => }
STDERR.puts
if !@options["issues"] || @options["issues"].include?(ticket)
resp = RestClient.post(uri, data.to_json, :content_type => :json, :accept=>:json)
end
end
|
#add_error_to_report(commit, msg, error_type = "no_jira") ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 276
def add_error_to_report(commit, msg, error_type = "no_jira")
author_email = Hook.shell!("git log #{commit} -1 --pretty='%aN <%aE>'").chomp rescue "no email"
errors_to_report[author_email] ||= {"no_jira" => [], "invalid_jira" => []}
errors_to_report[author_email][error_type] << "#{build_commit_uri(commit[0..7])}\n#{msg}"
end
|
#build_commit_uri(commit) ⇒ Object
111
112
113
114
115
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 111
def build_commit_uri(commit)
uri = "#{repo_remote_path}/commit/#{commit}"
end
|
#build_message(no_jira = [], invalid_jira = []) ⇒ Object
Build the email message. use the remote repo path for the name of the repo since this is always run as post_receive, there should always be a remote path.
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
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 319
def build_message(no_jira = [], invalid_jira= [])
description = @options["intro"] || ""
description.concat "This notice is to remind you that you need to include valid Jira ticket\nnumbers in all of your Git commits!\n\nWe encountered the following problems in your recent commits.\n\n"
if no_jira.size > 0
description.concat "Commits with no reference to any jira tickets:\n\n\#{no_jira.join(\"\\n--\\n \")}\n-----\n"
end
if invalid_jira.size > 0
description.concat "Commits which reference invalid Jira ticket numbers\nthat don't exist or have already been closed:\n\n\#{invalid_jira.join(\"\\n--\\n \")}\n-----\n"
end
description.concat @options["conclusion"] if @options["conclusion"]
description
end
|
#build_uri(ticket, command = nil) ⇒ Object
59
60
61
62
63
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 59
def build_uri(ticket, command=nil)
uri = "#{@options['protocol']}://#{@options['username']}:#{@options['password']}@#{@options['host']}/#{@options['api_path']}/#{ticket}"
uri = "#{uri}/#{command}" if command
return uri
end
|
#check ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 66
def check
if commits.empty?
STDERR.puts "JiraCommentAddHook - need list of commits to process"
end
success = true
commits.reverse_each do |commit|
commit_message = RubyGitHooks::Hook.shell!("git log #{commit} -1 --pretty=%B").rstrip
success = false unless check_one_commit(commit, commit_message )
end
report_errors
return success
end
|
#check_for_valid_ticket(ticket) ⇒ Object
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
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 207
def check_for_valid_ticket(ticket)
begin
uri = build_uri(ticket)
resp = RestClient.get uri
hash = JSON.parse(resp)
if @options["check_status"]
status = hash["fields"]["status"]["name"] rescue "open"
if status.downcase == "closed"
STDERR.puts "Issue #{ticket} is closed, not allowing."
return false
end
end
return true
rescue SocketError
STDERR.puts "SocketError finding '#{@options["host"]}': #{$!.inspect}"
STDERR.puts "Is '#{@options["host"]}' the right Jira hostname? "
STDERR.puts "I'm allowing this in case you're offline, but make sure"
STDERR.puts "your hostname is right, please!"
return true
rescue RestClient::Exception
if $!.http_code == 401
STDERR.puts "You're not authorized on this server!"
STDERR.puts "Please set your username and password correctly."
return false
elsif $!.http_code == 404
elsif $!.http_code == 407
STDERR.puts "We don't support proxies to Jira yet!"
STDERR.puts "I'll give you the benefit of the doubt."
return true
elsif $!.http_code >= 500
STDERR.puts "Jira got a server error."
STDERR.puts "I'll give you the benefit of the doubt."
return true
else
STDERR.puts "Unexpected HTTP Error: #{$!.http_code}!"
return false
end
rescue
STDERR.puts "Unexpected exception: #{$!.inspect}!"
return false
end
false
end
|
#check_one_commit(commit, commit_message) ⇒ Object
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
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 157
def check_one_commit(commit, commit_message)
STDERR.puts "Checking #{commit[0..6]} #{commit_message.lines.first}"
jira_tickets = commit_message.scan(JiraReferenceCheckHook::JIRA_TICKET_REGEXP).map(&:strip)
if jira_tickets.length == 0
STDERR.puts ">>Commit message must refer to a jira ticket"
add_error_to_report(commit, commit_message, "no_jira")
return false
end
= (commit, commit_message)
success = false
jira_tickets.each do |ticket|
valid_ticket = check_for_valid_ticket(ticket)
if valid_ticket
(ticket, )
success = true
end
end
unless success
STDERR.puts ">>Commit message must refer to a valid jira ticket"
add_error_to_report(commit, commit_message, "invalid_jira")
end
return success
end
|
#commit_list ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 260
def commit_list
return "" if !self.commits || self.commits.empty?
if self.commits.size == 1
"#{self.commits.first[0..6]}"
else
"#{self.commits.last[0..6]}..#{self.commits.first[0..6]}"
end
end
|
#get_change_list(commit) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 117
def get_change_list(commit)
current, base = Hook.shell!("git log #{commit} -2 --pretty=%H").split
if !base
files_with_status = Hook.shell!("git ls-tree --name-status -r #{commit}").split("\n")
files_with_status.map!{|filename| "A\t" + filename}
else
files_with_status = Hook.shell!("git diff --name-status #{base}..#{current}")
end
files_with_status
end
|
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 134
def (commit, commit_message)
github_link = build_commit_uri(commit)
changes = get_change_list(commit)
revision_and_date = Hook.shell!("git log #{commit} -1 --pretty='Revision: %h committed by %cn%nCommit date: %cd'") rescue ""
text = "#{revision_and_date}#{github_link}\n\n#{commit_message}\n{noformat}#{changes}{noformat}"
end
|
#repo_remote_path ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 91
def repo_remote_path
remote_urls = RubyGitHooks::Hook.shell!("git remote -v").split
remote = remote_urls[1]
return "" if !remote
uri = URI.parse(remote) rescue nil
if uri
uri.to_s.sub(/.git\z/, "")
else
path = remote[/:([\w\/.-]*)/,1]
path.sub!(/.git\z/, "") if path
"#{@options['protocol']}://#{@options['github']}/#{path}"
end
end
|
#report_errors ⇒ Object
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 294
def report_errors
require "pony" unless @options["no_send"] || @options["via"] == "no_send"
errors_to_report.each do |email, details|
desc = build_message(details["no_jira"], details["invalid_jira"])
STDERR.puts "Warnings for commit from Jira Add Comment Check:\n--"
STDERR.puts "#{desc}\n--"
unless @options["no_send"] || @options["via"] == "no_send"
STDERR.puts "Sending warning email to #{email}"
ret = Pony.mail :to => email,
:from => @options["from"],
:subject => @options["subject"],
:body => desc,
:via => @options["via"],
:via_options => @options["via_options"]
end
end
end
|
#to_s ⇒ Object
Do not show password when converting to string
86
87
88
|
# File 'lib/ruby_git_hooks/jira_add_comment.rb', line 86
def to_s
"<JiraCommentAddHook:#{object_id} #{@options.merge("password" => :redacted)}>"
end
|