codeclimate-services 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/cc/presenters/{github_pull_requests_presenter.rb → pull_requests_presenter.rb} +13 -1
- data/lib/cc/services/github_pull_requests.rb +9 -12
- data/lib/cc/services/stash_pull_requests.rb +98 -0
- data/lib/cc/services/version.rb +1 -1
- data/test/github_pull_requests_test.rb +1 -1
- data/test/presenters/{github_pull_requests_presenter_test.rb → pull_requests_presenter_test.rb} +3 -3
- data/test/stash_pull_requests_test.rb +146 -0
- metadata +9 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd43be14f3a358c801335f30ab99aa3e72e65362
|
4
|
+
data.tar.gz: fa21aa050c37330b29af67477d078f5b4aa262c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48410aea9beb76e44066ed178a98a54f58d6cbe308d58fdbbf11b7d8aa4e5c5a1d0d0722661eac46bb7bcade9594f3ce840b3b4fb6341f68be419376182f010e
|
7
|
+
data.tar.gz: 281c5a7829bac3d55eff0c2884a6f9d31eaa801abde6f2fa4c8246277735cc884d89941a526e27debf3c82b4b72c853b34d131abeb8817743f911bc00c7c6720
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module CC
|
2
2
|
class Service
|
3
|
-
class
|
3
|
+
class PullRequestsPresenter
|
4
4
|
include ActiveSupport::NumberHelper
|
5
5
|
|
6
6
|
def initialize(payload)
|
@@ -12,6 +12,18 @@ module CC
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
def error_message
|
16
|
+
"Code Climate encountered an error attempting to analyze this pull request."
|
17
|
+
end
|
18
|
+
|
19
|
+
def pending_message
|
20
|
+
"Code Climate is analyzing this code."
|
21
|
+
end
|
22
|
+
|
23
|
+
def skipped_message
|
24
|
+
"Code Climate has skipped analysis of this commit."
|
25
|
+
end
|
26
|
+
|
15
27
|
def success_message
|
16
28
|
if both_issue_counts_zero?
|
17
29
|
"Code Climate didn't find any new or fixed issues."
|
@@ -1,10 +1,10 @@
|
|
1
|
-
require "cc/presenters/
|
1
|
+
require "cc/presenters/pull_requests_presenter"
|
2
2
|
|
3
3
|
class CC::Service::GitHubPullRequests < CC::Service
|
4
4
|
class Config < CC::Service::Config
|
5
5
|
attribute :oauth_token, String,
|
6
6
|
label: "OAuth Token",
|
7
|
-
description: "A personal OAuth token with permissions for the repo.
|
7
|
+
description: "A personal OAuth token with permissions for the repo."
|
8
8
|
attribute :update_status, Boolean,
|
9
9
|
label: "Update status?",
|
10
10
|
description: "Update the pull request status after analyzing?"
|
@@ -24,9 +24,6 @@ class CC::Service::GitHubPullRequests < CC::Service
|
|
24
24
|
|
25
25
|
BODY_REGEX = %r{<b>Code Climate</b> has <a href=".*">analyzed this pull request</a>}
|
26
26
|
COMMENT_BODY = '<img src="https://codeclimate.com/favicon.png" width="20" height="20" /> <b>Code Climate</b> has <a href="%s">analyzed this pull request</a>.'
|
27
|
-
MESSAGES = [
|
28
|
-
DEFAULT_ERROR = "Code Climate encountered an error attempting to analyze this pull request",
|
29
|
-
]
|
30
27
|
|
31
28
|
# Just make sure we can access GH using the configured token. Without
|
32
29
|
# additional information (github-slug, PR number, etc) we can't test much
|
@@ -70,10 +67,7 @@ private
|
|
70
67
|
end
|
71
68
|
|
72
69
|
def update_status_skipped
|
73
|
-
update_status(
|
74
|
-
"success",
|
75
|
-
"Code Climate has skipped analysis of this commit."
|
76
|
-
)
|
70
|
+
update_status("success", presenter.skipped_message)
|
77
71
|
end
|
78
72
|
|
79
73
|
def update_status_success
|
@@ -87,18 +81,21 @@ private
|
|
87
81
|
end
|
88
82
|
|
89
83
|
def presenter
|
90
|
-
CC::Service::
|
84
|
+
CC::Service::PullRequestsPresenter.new(@payload)
|
91
85
|
end
|
92
86
|
|
93
87
|
def update_status_error
|
94
88
|
update_status(
|
95
89
|
"error",
|
96
|
-
@payload["message"] ||
|
90
|
+
@payload["message"] || presenter.error_message
|
97
91
|
)
|
98
92
|
end
|
99
93
|
|
100
94
|
def update_status_pending
|
101
|
-
update_status(
|
95
|
+
update_status(
|
96
|
+
"pending",
|
97
|
+
@payload["message"] || presenter.pending_message
|
98
|
+
)
|
102
99
|
end
|
103
100
|
|
104
101
|
def update_status(state, description)
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "base64"
|
2
|
+
require "cc/presenters/pull_requests_presenter"
|
3
|
+
|
4
|
+
class CC::Service::StashPullRequests < CC::Service
|
5
|
+
class Config < CC::Service::Config
|
6
|
+
attribute :domain, String,
|
7
|
+
description: "Your Stash host domain (e.g. yourstash.com:PORT, please exclude https://)"
|
8
|
+
attribute :username, String
|
9
|
+
attribute :password, Password
|
10
|
+
|
11
|
+
validates :domain, presence: true
|
12
|
+
validates :username, presence: true
|
13
|
+
validates :password, presence: true
|
14
|
+
end
|
15
|
+
|
16
|
+
self.title = "Stash Pull Requests"
|
17
|
+
self.description = "Update pull requests on Stash"
|
18
|
+
|
19
|
+
STASH_STATES = {
|
20
|
+
"error" => "FAILED",
|
21
|
+
"failure" => "FAILED",
|
22
|
+
"pending" => "INPROGRESS",
|
23
|
+
"skipped" => "SUCCESSFUL",
|
24
|
+
"success" => "SUCCESSFUL",
|
25
|
+
}.freeze
|
26
|
+
|
27
|
+
def receive_test
|
28
|
+
setup_http
|
29
|
+
|
30
|
+
service_get(test_url)
|
31
|
+
|
32
|
+
{ ok: true, message: "Test succeeded" }
|
33
|
+
rescue HTTPError => e
|
34
|
+
{ ok: false, message: e.message }
|
35
|
+
end
|
36
|
+
|
37
|
+
def receive_pull_request
|
38
|
+
setup_http
|
39
|
+
|
40
|
+
params = {
|
41
|
+
description: description,
|
42
|
+
key: "Code Climate",
|
43
|
+
name: "Code Climate",
|
44
|
+
state: state,
|
45
|
+
url: @payload["details_url"],
|
46
|
+
}
|
47
|
+
service_post(url, params.to_json)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def test_url
|
53
|
+
"http://#{config.domain}/rest/api/1.0/users"
|
54
|
+
end
|
55
|
+
|
56
|
+
def url
|
57
|
+
"http://#{config.domain}/rest/build-status/1.0/commits/#{commit_sha}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def commit_sha
|
61
|
+
@payload.fetch("commit_sha")
|
62
|
+
end
|
63
|
+
|
64
|
+
def description
|
65
|
+
return @payload["message"] if @payload["message"]
|
66
|
+
|
67
|
+
case @payload["state"]
|
68
|
+
when "pending"
|
69
|
+
presenter.pending_message
|
70
|
+
when "success", "failure"
|
71
|
+
presenter.success_message
|
72
|
+
when "skipped"
|
73
|
+
presenter.skipped_message
|
74
|
+
when "error"
|
75
|
+
presenter.error_message
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def state
|
80
|
+
STASH_STATES[@payload["state"]]
|
81
|
+
end
|
82
|
+
|
83
|
+
def setup_http
|
84
|
+
http.headers["Content-Type"] = "application/json"
|
85
|
+
http.headers["Authorization"] = "Basic #{auth_token}"
|
86
|
+
http.headers["User-Agent"] = "Code Climate"
|
87
|
+
end
|
88
|
+
|
89
|
+
# Following Basic Auth headers here:
|
90
|
+
# https://developer.atlassian.com/stash/docs/latest/how-tos/example-basic-authentication.html
|
91
|
+
def auth_token
|
92
|
+
Base64.encode64("#{config.username}:#{config.password}")
|
93
|
+
end
|
94
|
+
|
95
|
+
def presenter
|
96
|
+
CC::Service::PullRequestsPresenter.new(@payload)
|
97
|
+
end
|
98
|
+
end
|
data/lib/cc/services/version.rb
CHANGED
@@ -62,7 +62,7 @@ class TestGitHubPullRequests < CC::Service::TestCase
|
|
62
62
|
def test_pull_request_status_error
|
63
63
|
expect_status_update("pbrisbin/foo", "abc123", {
|
64
64
|
"state" => "error",
|
65
|
-
"description" =>
|
65
|
+
"description" => "Code Climate encountered an error attempting to analyze this pull request.",
|
66
66
|
})
|
67
67
|
|
68
68
|
receive_pull_request({ update_status: true }, {
|
data/test/presenters/{github_pull_requests_presenter_test.rb → pull_requests_presenter_test.rb}
RENAMED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "helper"
|
2
|
-
require "cc/presenters/
|
2
|
+
require "cc/presenters/pull_requests_presenter"
|
3
3
|
|
4
|
-
class
|
4
|
+
class TestPullRequestsPresenter < CC::Service::TestCase
|
5
5
|
def test_message_singular
|
6
6
|
assert_equal(
|
7
7
|
"Code Climate found 1 new issue and 1 fixed issue.",
|
@@ -44,6 +44,6 @@ private
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def build_presenter(issue_counts)
|
47
|
-
CC::Service::
|
47
|
+
CC::Service::PullRequestsPresenter.new(build_payload(issue_counts))
|
48
48
|
end
|
49
49
|
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class TestStashPullRequests < CC::Service::TestCase
|
4
|
+
def test_receive_test
|
5
|
+
@stubs.get "/rest/api/1.0/users" do
|
6
|
+
[200, {}, "{ 'values': [] }"]
|
7
|
+
end
|
8
|
+
|
9
|
+
response = receive_test
|
10
|
+
|
11
|
+
assert_equal({ ok: true, message: "Test succeeded" }, response)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_failed_receive_test
|
15
|
+
@stubs.get "/rest/api/1.0/users" do
|
16
|
+
[401, {}, ""]
|
17
|
+
end
|
18
|
+
|
19
|
+
response = receive_test
|
20
|
+
|
21
|
+
assert_equal({ ok: false, message: "API request unsuccessful (401)" }, response)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_pull_request_status_pending
|
25
|
+
expect_status_update("abc123", {
|
26
|
+
"state" => "INPROGRESS",
|
27
|
+
"description" => /is analyzing/,
|
28
|
+
})
|
29
|
+
|
30
|
+
receive_pull_request(
|
31
|
+
commit_sha: "abc123",
|
32
|
+
state: "pending",
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_pull_request_status_success_detailed
|
37
|
+
expect_status_update("abc123", {
|
38
|
+
"state" => "SUCCESSFUL",
|
39
|
+
"description" => "Code Climate found 2 new issues and 1 fixed issue.",
|
40
|
+
})
|
41
|
+
|
42
|
+
receive_pull_request(
|
43
|
+
commit_sha: "abc123",
|
44
|
+
state: "success",
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_pull_request_status_failure
|
49
|
+
expect_status_update("abc123", {
|
50
|
+
"state" => "FAILED",
|
51
|
+
"description" => "Code Climate found 2 new issues and 1 fixed issue.",
|
52
|
+
})
|
53
|
+
|
54
|
+
receive_pull_request(
|
55
|
+
commit_sha: "abc123",
|
56
|
+
state: "failure"
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_pull_request_status_error
|
61
|
+
expect_status_update("abc123", {
|
62
|
+
"state" => "FAILED",
|
63
|
+
"description" => "Code Climate encountered an error attempting to analyze this pull request."
|
64
|
+
})
|
65
|
+
|
66
|
+
receive_pull_request(
|
67
|
+
commit_sha: "abc123",
|
68
|
+
state: "error"
|
69
|
+
)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_pull_request_status_error_message_provided
|
73
|
+
message = "Everything broke."
|
74
|
+
|
75
|
+
expect_status_update("abc123", {
|
76
|
+
"state" => "FAILED",
|
77
|
+
"description" => message
|
78
|
+
})
|
79
|
+
|
80
|
+
receive_pull_request(
|
81
|
+
commit_sha: "abc123",
|
82
|
+
message: message,
|
83
|
+
state: "error"
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_pull_request_status_skipped
|
88
|
+
expect_status_update("abc123", {
|
89
|
+
"state" => "SUCCESSFUL",
|
90
|
+
"description" => "Code Climate has skipped analysis of this commit."
|
91
|
+
})
|
92
|
+
|
93
|
+
receive_pull_request(
|
94
|
+
commit_sha: "abc123",
|
95
|
+
state: "skipped",
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_failed_receive_pull_request
|
100
|
+
commit_sha = "abc123"
|
101
|
+
|
102
|
+
@stubs.post("/rest/build-status/1.0/commits/#{commit_sha}") do
|
103
|
+
[401, {}, ""]
|
104
|
+
end
|
105
|
+
|
106
|
+
assert_raises(CC::Service::HTTPError) do
|
107
|
+
receive_pull_request(
|
108
|
+
commit_sha: "abc123",
|
109
|
+
state: "success",
|
110
|
+
)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def expect_status_update(commit_sha, params)
|
117
|
+
@stubs.post("/rest/build-status/1.0/commits/#{commit_sha}") do |env|
|
118
|
+
body = JSON.parse(env[:body])
|
119
|
+
|
120
|
+
params.each do |k, v|
|
121
|
+
assert v === body[k],
|
122
|
+
"Unexpected value for #{k}. #{v.inspect} !== #{body[k].inspect}"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def default_config
|
128
|
+
{ domain: "example.com", username: "zaphod", password: "g4rgl3bl4st3r" }
|
129
|
+
end
|
130
|
+
|
131
|
+
def receive_pull_request(event_data, config = {})
|
132
|
+
receive(
|
133
|
+
CC::Service::StashPullRequests,
|
134
|
+
default_config.merge(config),
|
135
|
+
{ name: "pull_request", issue_comparison_counts: {'fixed' => 1, 'new' => 2} }.merge(event_data)
|
136
|
+
)
|
137
|
+
end
|
138
|
+
|
139
|
+
def receive_test(config = {}, event_data = {})
|
140
|
+
receive(
|
141
|
+
CC::Service::StashPullRequests,
|
142
|
+
default_config.merge(config),
|
143
|
+
{ name: "test" }.merge(event_data)
|
144
|
+
)
|
145
|
+
end
|
146
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codeclimate-services
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryan Helmkamp
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -167,7 +167,7 @@ files:
|
|
167
167
|
- lib/cc/helpers/issue_helper.rb
|
168
168
|
- lib/cc/helpers/quality_helper.rb
|
169
169
|
- lib/cc/helpers/vulnerability_helper.rb
|
170
|
-
- lib/cc/presenters/
|
170
|
+
- lib/cc/presenters/pull_requests_presenter.rb
|
171
171
|
- lib/cc/service.rb
|
172
172
|
- lib/cc/service/config.rb
|
173
173
|
- lib/cc/service/formatter.rb
|
@@ -191,6 +191,7 @@ files:
|
|
191
191
|
- lib/cc/services/lighthouse.rb
|
192
192
|
- lib/cc/services/pivotal_tracker.rb
|
193
193
|
- lib/cc/services/slack.rb
|
194
|
+
- lib/cc/services/stash_pull_requests.rb
|
194
195
|
- lib/cc/services/version.rb
|
195
196
|
- pull_request_test.rb
|
196
197
|
- service_test.rb
|
@@ -210,9 +211,10 @@ files:
|
|
210
211
|
- test/jira_test.rb
|
211
212
|
- test/lighthouse_test.rb
|
212
213
|
- test/pivotal_tracker_test.rb
|
213
|
-
- test/presenters/
|
214
|
+
- test/presenters/pull_requests_presenter_test.rb
|
214
215
|
- test/service_test.rb
|
215
216
|
- test/slack_test.rb
|
217
|
+
- test/stash_pull_requests_test.rb
|
216
218
|
- test/support/fake_logger.rb
|
217
219
|
- test/with_metrics_test.rb
|
218
220
|
homepage: ''
|
@@ -235,7 +237,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
235
237
|
version: '0'
|
236
238
|
requirements: []
|
237
239
|
rubyforge_project:
|
238
|
-
rubygems_version: 2.
|
240
|
+
rubygems_version: 2.4.7
|
239
241
|
signing_key:
|
240
242
|
specification_version: 4
|
241
243
|
summary: Service classes for Code Climate
|
@@ -256,8 +258,9 @@ test_files:
|
|
256
258
|
- test/jira_test.rb
|
257
259
|
- test/lighthouse_test.rb
|
258
260
|
- test/pivotal_tracker_test.rb
|
259
|
-
- test/presenters/
|
261
|
+
- test/presenters/pull_requests_presenter_test.rb
|
260
262
|
- test/service_test.rb
|
261
263
|
- test/slack_test.rb
|
264
|
+
- test/stash_pull_requests_test.rb
|
262
265
|
- test/support/fake_logger.rb
|
263
266
|
- test/with_metrics_test.rb
|