Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 6bb2a12

Browse files
author
Jon Yurek
committed
Merge pull request #56 from codeclimate/jy-do-not-allow-nil-branches
Give an OK of false if the PR comment exists
2 parents d3abaf6 + 5116688 commit 6bb2a12

File tree

6 files changed

+76
-12
lines changed

6 files changed

+76
-12
lines changed

lib/cc/service/invocation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
require 'cc/service/invocation/with_retries'
33
require 'cc/service/invocation/with_metrics'
44
require 'cc/service/invocation/with_error_handling'
5+
require 'cc/service/invocation/with_return_values'
56

67
class CC::Service::Invocation
78
MIDDLEWARE = {
89
retries: WithRetries,
910
metrics: WithMetrics,
1011
error_handling: WithErrorHandling,
12+
return_values: WithReturnValues,
1113
}
1214

1315
attr_reader :result
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class CC::Service::Invocation
2+
class WithReturnValues
3+
def initialize(invocation, message = nil)
4+
@invocation = invocation
5+
@message = message || "An internal error happened"
6+
end
7+
8+
def call
9+
result = @invocation.call
10+
if result.nil?
11+
{ ok: false, message: @message }
12+
else
13+
result
14+
end
15+
end
16+
end
17+
end
18+

lib/cc/services/github_pull_requests.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,17 @@ def update_status_pending
8585
end
8686

8787
def update_status(state, description)
88-
if config.update_status
89-
params = {
90-
state: state,
91-
description: description,
92-
target_url: @payload["details_url"],
93-
context: "codeclimate"
94-
}
95-
service_post(status_url, params.to_json)
96-
end
88+
params = {
89+
state: state,
90+
description: description,
91+
target_url: @payload["details_url"],
92+
context: "codeclimate"
93+
}
94+
service_post(status_url, params.to_json)
9795
end
9896

9997
def add_comment
100-
if config.add_comment && !comment_present?
98+
if !comment_present?
10199
body = {
102100
body: COMMENT_BODY % @payload["compare_url"]
103101
}.to_json
@@ -106,6 +104,8 @@ def add_comment
106104
doc = JSON.parse(response.body)
107105
{ id: doc["id"] }
108106
end
107+
else
108+
{ ok: false, message: "Comment already present" }
109109
end
110110
end
111111

test/github_pull_requests_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def test_pull_request_status_error
3838
commit_sha: "abc123",
3939
state: "error",
4040
})
41-
4241
end
4342

4443
def test_pull_request_status_test_success
@@ -115,11 +114,13 @@ def test_pull_request_comment_already_present
115114

116115
# With no POST expectation, test will fail if request is made.
117116

118-
receive_pull_request({ add_comment: true }, {
117+
rsp = receive_pull_request({ add_comment: true, update_status: false }, {
119118
github_slug: "pbrisbin/foo",
120119
number: 1,
121120
state: "success",
122121
})
122+
assert_not_nil rsp
123+
assert_false rsp[:ok]
123124
end
124125

125126
private

test/invocation_return_values_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require File.expand_path('../helper', __FILE__)
2+
3+
class InvocationReturnValuesTest < CC::Service::TestCase
4+
def test_success_returns_upstream_result
5+
handler = CC::Service::Invocation::WithReturnValues.new(
6+
lambda { :return_value },
7+
"error message"
8+
)
9+
10+
assert_equal :return_value, handler.call
11+
end
12+
13+
def test_empty_results_returns_hash
14+
handler = CC::Service::Invocation::WithReturnValues.new(
15+
lambda { nil },
16+
"error message"
17+
)
18+
19+
assert_equal( {ok: false, message: "error message"}, handler.call )
20+
end
21+
end

test/invocation_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ def test_success
1010
assert_equal :some_result, result
1111
end
1212

13+
def test_success_with_return_values
14+
service = FakeService.new(:some_result)
15+
16+
result = CC::Service::Invocation.invoke(service) do |i|
17+
i.with :return_values, "error"
18+
end
19+
20+
assert_equal 1, service.receive_count
21+
assert_equal :some_result, result
22+
end
23+
24+
def test_failure_with_return_values
25+
service = FakeService.new(nil)
26+
27+
result = CC::Service::Invocation.invoke(service) do |i|
28+
i.with :return_values, "error"
29+
end
30+
31+
assert_equal 1, service.receive_count
32+
assert_equal( {ok: false, message: "error"}, result )
33+
end
34+
1335
def test_retries
1436
service = FakeService.new
1537
service.fake_error = RuntimeError.new

0 commit comments

Comments
 (0)