@@ -19,14 +19,19 @@ def url
1919 "https://bugzilla.redhat.com/show_bug.cgi?id=#{ id } "
2020 end
2121
22- def self . set_options ( options )
22+ def self . set_options ( options , config )
2323 @options = options
24+ @config = config
2425 end
2526
2627 def self . options
2728 @options
2829 end
2930
31+ def self . config
32+ @config
33+ end
34+
3035 def self . load ( url )
3136 attrs = bz_query ( id : url . gsub ( /.*?(\d +)$/ , '\1' ) ) . first
3237 self . new ( attrs )
@@ -37,8 +42,8 @@ def self.search(filters = {})
3742 end
3843
3944 def self . bz_query ( filters = { } )
40- filters = filters . merge ( 'api_key' => options [ ' api_key' ] ) if options [ ' api_key' ]
41- conn = Faraday . new ( :url => 'https://bugzilla.redhat.com' ) do |faraday |
45+ filters = filters . merge ( 'api_key' => config . api_key ) if config . api_key
46+ conn = Faraday . new ( :url => config . url ) do |faraday |
4247 faraday . request :url_encoded
4348 faraday . response :logger
4449 faraday . adapter Faraday . default_adapter
@@ -48,4 +53,67 @@ def self.bz_query(filters = {})
4853 response = conn . get ( '/rest/bug' , filters )
4954 JSON . parse ( response . body ) [ 'bugs' ]
5055 end
56+
57+ def self . get_issue ( id )
58+ jsonrpc . invoke ( 'Bug.get' , [ { api_key : config . api_key , ids : [ id . to_i ] , include_fields : [ :external_bugs , :summary , :comments ] , } ] )
59+ end
60+
61+ # tracker url is the full url e.g. https://projects.engineering.redhat.com/browse/TFMRHCLOUD-165
62+ # tracker id is the short id as it appears in BZ. e.g. TFMRHCLOUD-165
63+ def self . put_tracker ( id , tracker_url , tracker_id )
64+ return unless get_issue ( id ) [ 'bugs' ] . first [ 'external_bugs' ] . select { |link | link [ 'ext_bz_bug_id' ] == tracker_id } . empty?
65+
66+ jsonrpc . invoke (
67+ 'ExternalBugs.add_external_bug' ,
68+ [ {
69+ api_key : config . api_key ,
70+ bug_ids : [ id . to_i ] ,
71+ external_bugs : [
72+ {
73+ ext_bz_bug_url : tracker_url ,
74+ } ,
75+ ] ,
76+ } ]
77+ )
78+ end
79+
80+ # known fields:
81+ # cf_fixed_in: version,
82+ # status: 'POST'
83+ def self . set_fields ( id , fields )
84+ all_fields = fields . merge (
85+ api_key : config . api_key ,
86+ )
87+ bz_api [ 'bug' ] [ "#{ id } " ] . put ( all_fields )
88+ end
89+
90+ def bugzilla_url ( bz_id )
91+ "#{ config . url } /show_bug.cgi?id=#{ bz_id } "
92+ end
93+
94+ private
95+
96+ def self . bz_api
97+ RestClient ::Resource . new ( config . url + '/rest' , params : { api_key : config . api_key } )
98+ end
99+
100+ def self . jsonrpc
101+ @jsonrpc ||= begin
102+ conn = Faraday . new ( url : config . url + '/jsonrpc.cgi' ) do |faraday |
103+ faraday . response :logger , nil , { headers : true , bodies : true }
104+ end
105+
106+ JsonRpcClient . new ( config . url + '/jsonrpc.cgi' , { connection : conn } )
107+ end
108+ end
109+
110+ # Need to monkey-patch valid_response? because it has a strict JSONRPC version check
111+ # https://github.com/fxposter/jsonrpc-client/blob/287f6d2418f9a67064ebff2417c281db2c3a17c8/lib/jsonrpc/client.rb#L194
112+ class JsonRpcClient < JSONRPC ::Client
113+ private
114+ def valid_response? ( data )
115+ return true
116+ end
117+ end
118+
51119end
0 commit comments