-
Notifications
You must be signed in to change notification settings - Fork 8
/
bugzilla.rb
122 lines (99 loc) · 2.87 KB
/
bugzilla.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
require 'rest-client'
require 'jsonrpc-client'
class Bugzilla
def initialize(attrs)
@attrs = attrs
end
def id
@attrs['id'].to_i
end
def summary
@attrs['summary']
end
def pm_score
@attrs['cf_pm_score'].to_i
end
def url
"https://bugzilla.redhat.com/show_bug.cgi?id=#{id}"
end
def self.set_options(options, config)
@options = options
@config = config
end
def self.options
@options
end
def self.config
@config
end
def self.load(url)
attrs = bz_query(id: url.gsub(/.*?(\d+)$/, '\1')).first
self.new(attrs)
end
def self.search(filters = {})
bz_query(filters).map { |bz| self.new(bz) }
end
def self.bz_query(filters = {})
filters = filters.merge('api_key' => config.api_key) if config.api_key
conn = Faraday.new(:url => config.url) do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.adapter Faraday.default_adapter
faraday.options.params_encoder = Faraday::FlatParamsEncoder
end
response = conn.get('/rest/bug', filters)
JSON.parse(response.body)['bugs']
end
def self.get_issue(id)
jsonrpc.invoke('Bug.get',[{api_key: config.api_key, ids: [id.to_i], include_fields: [:external_bugs, :summary, :comments],}])
end
# tracker url is the full url e.g. https://projects.engineering.redhat.com/browse/TFMRHCLOUD-165
# tracker id is the short id as it appears in BZ. e.g. TFMRHCLOUD-165
def self.put_tracker(id, tracker_url, tracker_id)
return unless get_issue(id)['bugs'].first['external_bugs'].select { |link| link['ext_bz_bug_id'] == tracker_id }.empty?
jsonrpc.invoke(
'ExternalBugs.add_external_bug',
[{
api_key: config.api_key,
bug_ids: [id.to_i],
external_bugs: [
{
ext_bz_bug_url: tracker_url,
},
],
}]
)
end
# known fields:
# cf_fixed_in: version,
# status: 'POST'
def self.set_fields(id, fields)
all_fields = fields.merge(
api_key: config.api_key,
)
bz_api['bug']["#{id}"].put(all_fields)
end
def self.bugzilla_url(bz_id)
"#{config.url}/show_bug.cgi?id=#{bz_id}"
end
private
def self.bz_api
RestClient::Resource.new(config.url + '/rest', params: {api_key: config.api_key})
end
def self.jsonrpc
@jsonrpc ||= begin
conn = Faraday.new(url: config.url + '/jsonrpc.cgi') do |faraday|
faraday.response :logger, nil, { headers: true, bodies: true }
end
JsonRpcClient.new(config.url + '/jsonrpc.cgi', { connection: conn})
end
end
# Need to monkey-patch valid_response? because it has a strict JSONRPC version check
# https://github.com/fxposter/jsonrpc-client/blob/287f6d2418f9a67064ebff2417c281db2c3a17c8/lib/jsonrpc/client.rb#L194
class JsonRpcClient < JSONRPC::Client
private
def valid_response?(data)
return true
end
end
end