Skip to content

Commit 23da7c2

Browse files
ShimShteinares
authored andcommitted
Add support for Jira and update BZ actions
1 parent f23d34c commit 23da7c2

File tree

5 files changed

+187
-10
lines changed

5 files changed

+187
-10
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ gem 'faraday'
44
gem 'awesome_print'
55
gem 'activesupport'
66
gem 'pry'
7+
gem 'jira-ruby'
8+
gem 'jsonrpc-client'

bugzilla.rb

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
51119
end

jira.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'jira-ruby'
2+
3+
class Jira
4+
def self.set_config(config)
5+
@config = config
6+
end
7+
8+
def self.issue(id)
9+
api.Issue.find(id)
10+
end
11+
12+
def self.issue_url(issue_id)
13+
"#{config.site}/browse/#{issue_id}"
14+
end
15+
16+
def self.update_summary(issue, summary)
17+
issue.save({'fields' => { 'summary' => summary}})
18+
end
19+
20+
def self.add_remote_link(issue, label, url)
21+
return if issue.remotelink.all.find { |link| link.attrs.dig('object', 'title') }
22+
link = issue.remotelink.build
23+
24+
link.save(
25+
object: {
26+
url: url,
27+
title: label
28+
}
29+
)
30+
end
31+
32+
def self.active_sprints
33+
api.Agile.get_sprints(config.board, state: 'active')
34+
end
35+
36+
def self.find_sprint(id)
37+
api.Sprint.find(id)
38+
end
39+
40+
private
41+
42+
def self.api
43+
@client ||= JIRA::Client.new({
44+
username: config.user,
45+
password: config.password,
46+
site: config.site,
47+
context_path: '',
48+
auth_type: :basic,
49+
})
50+
end
51+
52+
def self.config
53+
@config
54+
end
55+
56+
def self.project(id)
57+
api.Project.find(id)
58+
end
59+
end

kansync.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,19 @@ def self.prepare_kanboard_connection(connection_options)
5757
@kanboard_connection = RequestFactory.new(connection_options)
5858
end
5959

60-
def self.prepare_bz_connection(connection_options)
61-
Bugzilla.set_options(connection_options)
60+
def self.prepare_bz_connection(profile)
61+
Bugzilla.set_options(profile.bugzilla_options, profile.bugzilla_config)
62+
end
63+
64+
def self.prepare_jira_connection(profile)
65+
Jira.set_config(profile.jira_config)
6266
end
6367

6468
def self.setup(profile)
6569
prepare_logger(profile.logger_level)
6670
prepare_kanboard_connection(profile.kanboard_options)
67-
prepare_bz_connection(profile.bugzilla_options)
71+
prepare_bz_connection(profile)
72+
prepare_jira_connection(profile)
6873
end
6974
end
7075

@@ -108,5 +113,3 @@ def execute
108113
# TODO need a separate script to help with new iteration setup
109114
# TODO scripts may need custom configuration per profile, e.g. email mapping
110115
# TODO README with links to APIs, docker image, sql converting trick
111-
112-

profile.rb

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Profile
22
attr_accessor :project_id, :kanboard_options, :logger_level, :task_configuration,
33
:whitelist, :blacklist, :github_options, :bugzilla_options,
4-
:backlog_swimlane_name
4+
:backlog_swimlane_name, :bugzilla_config, :jira_config
55

66
def initialize(profile_file)
77
@profile_file = profile_file
@@ -25,6 +25,51 @@ def initialize_vars
2525
@blacklist = @data['blacklist']
2626
@github_options = @data['github']
2727
@bugzilla_options = @data['bugzilla']
28+
@bugzilla_config = Bugzilla.new(@data['bugzilla'])
29+
@jira_config = Jira.new(@data['jira'])
30+
end
31+
32+
class Bugzilla
33+
def initialize(yaml)
34+
@yaml = yaml
35+
end
36+
37+
def api_key
38+
@yaml['api_key']
39+
end
40+
41+
def username
42+
@yaml['user']
43+
end
44+
45+
def password
46+
@yaml['password']
47+
end
48+
49+
def url
50+
@yaml['url'] || 'https://bugzilla.redhat.com'
51+
end
2852
end
29-
end
3053

54+
class Jira
55+
def initialize(yaml)
56+
@yaml = yaml
57+
end
58+
59+
def user
60+
@yaml['user']
61+
end
62+
63+
def password
64+
@yaml['password']
65+
end
66+
67+
def site
68+
@yaml['site'] || 'https://projects.engineering.redhat.com'
69+
end
70+
71+
def board
72+
@yaml['board']
73+
end
74+
end
75+
end

0 commit comments

Comments
 (0)