forked from codeclimate/codeclimate-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jira.rb
94 lines (73 loc) · 2.59 KB
/
jira.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
require "base64"
class CC::Service::Jira < CC::Service
class Config < CC::Service::Config
attribute :domain, Axiom::Types::String,
description: "Your JIRA host domain (e.g. yourjira.com:PORT, please exclude https://)"
attribute :username, Axiom::Types::String,
description: "Must exactly match the 'username' that appears on your JIRA profile page."
attribute :password, Axiom::Types::Password,
label: "JIRA password",
description: "Your JIRA password"
attribute :project_id, Axiom::Types::String,
description: "Your JIRA project ID number (located in your JIRA admin panel). Project must contain only the default required fields."
attribute :issue_type, Axiom::Types::String,
description: "The type of Issue to be created.",
default: "Task"
attribute :labels, Axiom::Types::String,
description: "Which labels to add to issues, comma delimited"
validates :domain, presence: true
validates :username, presence: true
validates :password, presence: true
validates :project_id, presence: true
end
self.title = "JIRA"
self.description = "Create tickets in JIRA"
self.issue_tracker = true
def receive_test
result = create_ticket("Test ticket from Code Climate", "Test ticket from Code Climate")
result.merge(
message: "Ticket <a href='#{result[:url]}'>#{result[:id]}</a> created.",
)
end
def receive_quality
create_ticket(quality_title, details_url)
end
def receive_issue
title = %(Fix "#{issue["check_name"]}" issue in #{constant_name})
body = [issue["description"], details_url].join("\n\n")
create_ticket(title, body)
end
def receive_vulnerability
formatter = CC::Formatters::TicketFormatter.new(self)
create_ticket(
formatter.format_vulnerability_title,
formatter.format_vulnerability_body,
)
end
private
def create_ticket(title, ticket_body)
params = {
fields:
{
project: { id: config.project_id },
summary: title,
description: ticket_body,
issuetype: { name: config.issue_type },
},
}
if config.labels.present?
params[:fields][:labels] = config.labels.split(",")
end
http.headers["Content-Type"] = "application/json"
http.basic_auth(config.username, config.password)
url = "https://#{config.domain}/rest/api/2/issue/"
service_post(url, params.to_json) do |response|
body = JSON.parse(response.body)
{
id: body["id"],
key: body["key"],
url: "https://#{config.domain}/browse/#{body["key"]}",
}
end
end
end