codeclimate-services 0.6.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a54f744a90038f57b0394f3fd4d98be62ab52ffa
4
- data.tar.gz: dc870ec854172e950e2cb7eb9ebfd932f073236a
3
+ metadata.gz: 284a96fe4285fcc34d367b9dd24abdf4cb51b43a
4
+ data.tar.gz: e60dd3be41f2ed70a13602b260a4f7364c7b9656
5
5
  SHA512:
6
- metadata.gz: c7e8ee7f277ff54a9e9d77dbfc0268b1b5cc4bced5512594ce75d0448f6a98c501039a3f02cc00d0a1bf448f233c9fb08bc2363336414fe001283987313c3171
7
- data.tar.gz: 2e2c7ac2be8a98a00977860623b5523de21f1078a21368e673ca6bc32943bbbb5dde377037063160e09692576e94a9a396ee776a5ff5486e4eb05d7eea96454f
6
+ metadata.gz: 7e33711b3282b2870de63ef36e7fe520ebe7276949c44fd33f020a84eb30fb0afa4dfeec7113d939b6921f7d96e1ca2cbf26d0aa1e7b4830ae8f137c5bd91ab1
7
+ data.tar.gz: 44d1e4c810e0392b32ba9aadb6cd79bf963bb67d227607340ca1620728e7c47c4e8fcd30be328e9177d57d23cf705a6e8f1b7ec530a43556eccd167a36e99842
@@ -8,9 +8,6 @@ class CC::Service::GitHubPullRequests < CC::Service
8
8
  attribute :update_status, Axiom::Types::Boolean,
9
9
  label: "Update status?",
10
10
  description: "Update the pull request status after analyzing?"
11
- attribute :add_comment, Axiom::Types::Boolean,
12
- label: "Add a comment?",
13
- description: "Comment on the pull request after analyzing?"
14
11
  attribute :base_url, Axiom::Types::String,
15
12
  label: "Github API Base URL",
16
13
  description: "Base URL for the Github API",
@@ -26,22 +23,14 @@ class CC::Service::GitHubPullRequests < CC::Service
26
23
  self.title = "GitHub Pull Requests"
27
24
  self.description = "Update pull requests on GitHub"
28
25
 
29
- BODY_REGEX = %r{<b>Code Climate</b> has <a href=".*">analyzed this pull request</a>}
30
- COMMENT_BODY = '<img src="https://codeclimate.com/favicon.png" width="20" height="20" />&nbsp;<b>Code Climate</b> has <a href="%s">analyzed this pull request</a>.'
31
-
32
26
  # Just make sure we can access GH using the configured token. Without
33
27
  # additional information (github-slug, PR number, etc) we can't test much
34
28
  # else.
35
29
  def receive_test
36
30
  setup_http
37
31
 
38
- if update_status? && add_comment?
39
- receive_test_status
40
- receive_test_comment
41
- elsif update_status?
32
+ if update_status?
42
33
  receive_test_status
43
- elsif add_comment?
44
- receive_test_comment
45
34
  else
46
35
  simple_failure("Nothing happened")
47
36
  end
@@ -62,10 +51,6 @@ class CC::Service::GitHubPullRequests < CC::Service
62
51
 
63
52
  private
64
53
 
65
- def add_comment?
66
- [true, "1"].include?(config.add_comment)
67
- end
68
-
69
54
  def update_status?
70
55
  [true, "1"].include?(config.update_status)
71
56
  end
@@ -83,12 +68,10 @@ private
83
68
  end
84
69
 
85
70
  def update_status_success
86
- add_comment
87
71
  update_status("success", presenter.success_message)
88
72
  end
89
73
 
90
74
  def update_status_failure
91
- add_comment
92
75
  update_status("failure", presenter.success_message)
93
76
  end
94
77
 
@@ -122,26 +105,6 @@ private
122
105
  end
123
106
  end
124
107
 
125
- def add_comment
126
- if add_comment?
127
- if !comment_present?
128
- body = {
129
- body: COMMENT_BODY % @payload["compare_url"]
130
- }.to_json
131
-
132
- @response = service_post(comments_url, body) do |response|
133
- doc = JSON.parse(response.body)
134
- { id: doc["id"] }
135
- end
136
- else
137
- @response = {
138
- ok: true,
139
- message: "Comment already present"
140
- }
141
- end
142
- end
143
- end
144
-
145
108
  def receive_test_status
146
109
  url = base_status_url("0" * 40)
147
110
  params = {}
@@ -160,24 +123,6 @@ private
160
123
  end
161
124
  end
162
125
 
163
- def receive_test_comment
164
- response = service_get(user_url)
165
- if response_includes_repo_scope?(response)
166
- { ok: true, message: "OAuth token is valid" }
167
- else
168
- { ok: false, message: "OAuth token requires 'repo' scope to post comments." }
169
- end
170
- rescue => ex
171
- { ok: false, message: ex.message }
172
- end
173
-
174
- def comment_present?
175
- response = service_get(comments_url)
176
- comments = JSON.parse(response.body)
177
-
178
- comments.any? { |comment| comment["body"] =~ BODY_REGEX }
179
- end
180
-
181
126
  def setup_http
182
127
  http.headers["Content-Type"] = "application/json"
183
128
  http.headers["Authorization"] = "token #{config.oauth_token}"
@@ -192,10 +137,6 @@ private
192
137
  "#{config.base_url}/repos/#{github_slug}/statuses/#{commit_sha}"
193
138
  end
194
139
 
195
- def comments_url
196
- "#{config.base_url}/repos/#{github_slug}/issues/#{number}/comments"
197
- end
198
-
199
140
  def user_url
200
141
  "#{config.base_url}/user"
201
142
  end
@@ -1,5 +1,5 @@
1
1
  module CC
2
2
  module Services
3
- VERSION = "0.6.2"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
data/pull_request_test.rb CHANGED
@@ -31,7 +31,6 @@ end
31
31
  service = CC::Service::GitHubPullRequests.new({
32
32
  oauth_token: ENV.fetch("OAUTH_TOKEN"),
33
33
  update_status: true,
34
- add_comment: true,
35
34
  }, {
36
35
  name: "pull_request",
37
36
  # https://github.com/codeclimate/nillson/pull/33
data/service_test.rb CHANGED
@@ -11,7 +11,7 @@
11
11
  # Example:
12
12
  #
13
13
  # $ SLACK_WEBHOOK_URL="http://..." bundle exec ruby service_test.rb
14
- # $ GITHUBPULLREQUESTS_UPDATE_STATUS=false GITHUBPULLREQUESTS_ADD_COMMENT=true GITHUBPULLREQUESTS_OAUTH_TOKEN=06083a4a060d358ca709939b1f00645777661c44 bundle exec ruby service_test.rb
14
+ # $ GITHUBPULLREQUESTS_UPDATE_STATUS=false GITHUBPULLREQUESTS_OAUTH_TOKEN=06083a4a060d358ca709939b1f00645777661c44 bundle exec ruby service_test.rb
15
15
  #
16
16
  # Other Environment variables used:
17
17
  #
@@ -73,7 +73,7 @@ class TestGitHubPullRequests < CC::Service::TestCase
73
73
  })
74
74
  end
75
75
 
76
- def test_pull_request_status_error_message_provided
76
+ def test_pull_request_status_error_message_provided
77
77
  expect_status_update("pbrisbin/foo", "abc123", {
78
78
  "state" => "error",
79
79
  "description" => "descriptive message",
@@ -131,107 +131,26 @@ class TestGitHubPullRequests < CC::Service::TestCase
131
131
  })
132
132
  end
133
133
 
134
-
135
- def test_no_comment_for_skips_regardless_of_add_comment_config
136
- # With no POST expectation, test will fail if request is made.
137
-
138
- receive_pull_request({ add_comment: true }, {
139
- github_slug: "pbrisbin/foo",
140
- commit_sha: "abc123",
141
- state: "skipped",
142
- })
143
- end
144
-
145
134
  def test_pull_request_status_test_success
146
135
  @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
147
136
 
148
137
  assert receive_test({ update_status: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
149
138
  end
150
139
 
151
- def test_pull_request_status_test_failure
152
- @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [401, {}, ""] }
153
-
154
- assert_raises(CC::Service::HTTPError) do
155
- receive_test({ update_status: true }, { github_slug: "pbrisbin/foo" })
156
- end
157
- end
158
-
159
- def test_pull_request_comment_test_success
160
- @stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user, repo" }, ""] }
161
-
162
- assert receive_test({ add_comment: true })[:ok], "Expected test of pull request to be true"
163
- end
164
-
165
- def test_pull_request_comment_test_failure_insufficient_permissions
166
- @stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user" }, ""] }
167
-
168
- assert !receive_test({ add_comment: true })[:ok], "Expected failed test of pull request"
169
- end
170
-
171
- def test_pull_request_comment_test_failure_bad_token
172
- @stubs.get("/user") { |env| [401, {}, ""] }
140
+ def test_pull_request_status_test_doesnt_blow_up_when_unused_keys_present_in_config
141
+ @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
173
142
 
174
- assert !receive_test({ add_comment: true })[:ok], "Expected failed test of pull request"
143
+ assert receive_test({ update_status: true, add_comment: true, wild_flamingo: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
175
144
  end
176
145
 
177
- def test_pull_request_failure_on_status_requesting_both
146
+ def test_pull_request_status_test_failure
178
147
  @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [401, {}, ""] }
179
148
 
180
149
  assert_raises(CC::Service::HTTPError) do
181
- receive_test({ update_status: true, add_comment: true }, { github_slug: "pbrisbin/foo" })
150
+ receive_test({ update_status: true }, { github_slug: "pbrisbin/foo" })
182
151
  end
183
152
  end
184
153
 
185
- def test_pull_request_failure_on_comment_requesting_both
186
- @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
187
- @stubs.get("/user") { |env| [401, { "x-oauth-scopes" => "gist, user, repo" }, ""] }
188
-
189
- assert_false receive_test({ update_status: true, add_comment: true }, { github_slug: "pbrisbin/foo" })[:ok]
190
- end
191
-
192
- def test_pull_request_success_both
193
- @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
194
- @stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user, repo" }, ""] }
195
-
196
- assert receive_test({ update_status: true, add_comment: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
197
- end
198
-
199
- def test_pull_request_comment
200
- stub_existing_comments("pbrisbin/foo", 1, %w[Hey Yo])
201
-
202
- expect_comment("pbrisbin/foo", 1, %r{href="http://example.com">analyzed})
203
-
204
- receive_pull_request({ add_comment: true }, {
205
- github_slug: "pbrisbin/foo",
206
- number: 1,
207
- state: "success",
208
- compare_url: "http://example.com",
209
- issue_comparison_counts: {
210
- "fixed" => 2,
211
- "new" => 1,
212
- }
213
- })
214
- end
215
-
216
- def test_pull_request_comment_already_present
217
- stub_existing_comments("pbrisbin/foo", 1, [
218
- '<b>Code Climate</b> has <a href="">analyzed this pull request</a>'
219
- ])
220
-
221
- # With no POST expectation, test will fail if request is made.
222
-
223
- response = receive_pull_request({
224
- add_comment: true,
225
- update_status: false
226
- }, {
227
- github_slug: "pbrisbin/foo",
228
- number: 1,
229
- state: "success",
230
- })
231
-
232
- assert_equal({ ok: true, message: "Comment already present" }, response)
233
- end
234
-
235
154
  def test_pull_request_unknown_state
236
155
  response = receive_pull_request({}, { state: "unknown" })
237
156
 
@@ -245,12 +164,12 @@ class TestGitHubPullRequests < CC::Service::TestCase
245
164
  end
246
165
 
247
166
  def test_different_base_url
248
- @stubs.get("/user") do |env|
249
- assert env[:url].to_s == "http://example.com/user"
250
- [200, { "x-oauth-scopes" => "gist, user, repo" }, ""]
167
+ @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") do |env|
168
+ assert env[:url].to_s == "http://example.com/repos/pbrisbin/foo/statuses/#{"0" * 40}"
169
+ [422, { "x-oauth-scopes" => "gist, user, repo" }, ""]
251
170
  end
252
171
 
253
- assert receive_test({ add_comment: true, base_url: "http://example.com" })[:ok], "Expected test of pull request to be true"
172
+ assert receive_test({ update_status: true, base_url: "http://example.com" }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
254
173
  end
255
174
 
256
175
  def test_default_context
@@ -280,15 +199,15 @@ class TestGitHubPullRequests < CC::Service::TestCase
280
199
  end
281
200
 
282
201
  def test_config_coerce_bool_true
283
- c = CC::Service::GitHubPullRequests::Config.new(oauth_token: "a1b2c3", add_comment: "1")
202
+ c = CC::Service::GitHubPullRequests::Config.new(oauth_token: "a1b2c3", update_status: "1")
284
203
  assert c.valid?
285
- assert_equal true, c.add_comment
204
+ assert_equal true, c.update_status
286
205
  end
287
206
 
288
207
  def test_config_coerce_bool_false
289
- c = CC::Service::GitHubPullRequests::Config.new(oauth_token: "a1b2c3", add_comment: "0")
208
+ c = CC::Service::GitHubPullRequests::Config.new(oauth_token: "a1b2c3", update_status: "0")
290
209
  assert c.valid?
291
- assert_equal false, c.add_comment
210
+ assert_equal false, c.update_status
292
211
  end
293
212
 
294
213
  private
@@ -306,22 +225,6 @@ private
306
225
  end
307
226
  end
308
227
 
309
- def stub_existing_comments(repo, number, bodies)
310
- body = bodies.map { |b| { body: b } }.to_json
311
-
312
- @stubs.get("repos/#{repo}/issues/#{number}/comments") { [200, {}, body] }
313
- end
314
-
315
- def expect_comment(repo, number, content)
316
- @stubs.post "repos/#{repo}/issues/#{number}/comments" do |env|
317
- body = JSON.parse(env[:body])
318
- assert_equal "token 123", env[:request_headers]["Authorization"]
319
- assert content === body["body"],
320
- "Unexpected comment body. #{content.inspect} !== #{body["body"].inspect}"
321
- [200, {}, '{"id": 2}']
322
- end
323
- end
324
-
325
228
  def receive_pull_request(config, event_data)
326
229
  receive(
327
230
  CC::Service::GitHubPullRequests,
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.6.2
4
+ version: 1.0.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: 2016-03-18 00:00:00.000000000 Z
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday