-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatchset-created
More file actions
executable file
·96 lines (76 loc) · 2.26 KB
/
Copy pathpatchset-created
File metadata and controls
executable file
·96 lines (76 loc) · 2.26 KB
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
#!/usr/bin/env ruby
# Copyright (C) 2014 Sarzyniec Luc <contact@olbat.net>
# see the COPYING file for more informations
# This hook enables Gerrit to post comments on a bugzilla 3/4 bug tracker
# depending on the bug# contained in the topic of the Gerrit patchset
#
# By default, the hook is enabled only for the first patch, with a topic
# following the pattern "bug/ID" and it's not working with drafts
require 'optparse'
require 'ostruct'
require 'json'
require 'restclient'
BUGZILLA_API='http://my.bugzilla.tld/jsonrpc.cgi'
BUGZILLA_LOGIN='gerrit_login'
BUGZILLA_PASSWORD='gerrit_password'
TOPIC_PREFIX='bug/'
DISCARD_DRAFTS=true
NOTIFY_ON_FIRST_PATCHSET_ONLY=true
# The tokens <SWITCH_NAME> will be replaced by their respective values
MESSAGE_TEMPLATE=<<-EOS
A new change was submited on Gerrit: <CHANGE-URL>.
Change#: <CHANGE>
Commit#: <COMMIT>
Author: <UPLOADER>
EOS
SWITCHES=[
'change',
'is-draft',
'change-url',
'project',
'branch',
'topic',
'uploader',
'commit',
'patchset',
]
args = {}
opt_parser = OptionParser.new do |opts|
SWITCHES.each {|s| opts.on("--#{s} VALUE") { |v| args[s] = v } }
end
opt_parser.parse!(ARGV)
args['is-draft'] = (args['is-draft'].downcase == 'true') if args['is-draft']
args['patchset'] = args['patchset'].to_i if args['patchset']
if args['topic'] =~ /^#{TOPIC_PREFIX}(\d+)$/
id = $1.to_i
if (DISCARD_DRAFTS and !args['is-draft']) or !DISCARD_DRAFTS
if (NOTIFY_ON_FIRST_PATCHSET_ONLY and args['patchset'] == 1) or !NOTIFY_ON_FIRST_PATCHSET_ONLY
comment = MESSAGE_TEMPLATE.dup
SWITCHES.each {|s| comment.gsub!(/<#{s.upcase}>/,args[s].to_s)}
params = {
'method' => 'Bug.add_comment',
'params' => [{
'id' => id,
'comment' => comment,
'Bugzilla_login' => BUGZILLA_LOGIN,
'Bugzilla_password' => BUGZILLA_PASSWORD,
}],
'id' => id,
}
ret = RestClient.post(BUGZILLA_API, params.to_json,
:content_type => :json, :accept => :json)
begin
ret = JSON.parse(ret)
rescue Exception => e
$stderr.puts "#{e.class.name}: #{e.message}"
exit 1
end
if ret['error'] and !ret['error'].empty?
$stderr.puts ret['error']
exit 1
else
exit 0
end
end
end
end