-
Notifications
You must be signed in to change notification settings - Fork 9
/
github_issues.rb
79 lines (65 loc) · 2.28 KB
/
github_issues.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
class CC::Service::GitHubIssues < CC::Service
class Config < CC::Service::Config
attribute :oauth_token, Axiom::Types::Token,
label: "OAuth Token",
description: "A personal OAuth token with permissions for the repo"
attribute :project, Axiom::Types::String,
label: "Project",
description: "Project name on GitHub (e.g 'thoughtbot/paperclip')"
attribute :labels, Axiom::Types::String,
label: "Labels (comma separated)",
description: "Comma separated list of labels to apply to the issue"
attribute :base_url, Axiom::Types::String,
label: "Github API Base URL",
description: "Base URL for the Github API",
default: "https://api.github.com"
validates :oauth_token, presence: true
end
self.title = "GitHub Issues"
self.description = "Open issues on GitHub"
self.issue_tracker = true
def receive_test
result = create_issue("Test ticket from Code Climate", "")
result.merge(
message: "Issue <a href='#{result[:url]}'>##{result[:number]}</a> created.",
)
rescue CC::Service::HTTPError => e
body = JSON.parse(e.response_body)
e.user_message = body["message"]
raise e
end
def receive_quality
create_issue(quality_title, details_url)
end
def receive_vulnerability
formatter = CC::Formatters::TicketFormatter.new(self)
create_issue(
formatter.format_vulnerability_title,
formatter.format_vulnerability_body,
)
end
def receive_issue
title = %(Fix "#{issue["check_name"]}" issue in #{constant_name})
body = [issue["description"], details_url].join("\n\n")
create_issue(title, body)
end
private
def create_issue(title, issue_body)
params = { title: title, body: issue_body }
if config.labels.present?
params[:labels] = config.labels.split(",").map(&:strip).reject(&:blank?).compact
end
http.headers["Content-Type"] = "application/json"
http.headers["Authorization"] = "token #{config.oauth_token}"
http.headers["User-Agent"] = "Code Climate"
url = "#{config.base_url}/repos/#{config.project}/issues"
service_post_with_redirects(url, params.to_json) do |response|
body = JSON.parse(response.body)
{
id: body["id"],
number: body["number"],
url: body["html_url"],
}
end
end
end