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

Commit 49af669

Browse files
committed
Use aggregate response if both comment/status are checked
1 parent f4c2a54 commit 49af669

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/cc/services/github_pull_requests.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ class Config < CC::Service::Config
1313
validates :oauth_token, presence: true
1414
end
1515

16+
class ResponseAggregator
17+
def initialize(status_response, comment_response)
18+
@status_response = status_response
19+
@comment_response = comment_response
20+
end
21+
22+
def response
23+
return @status_response if @status_response[:ok] && @comment_response[:ok]
24+
message = if !@status_response[:ok] && !@comment_response[:ok]
25+
"Unable to post comment or update status"
26+
elsif !@status_response[:ok]
27+
"Unable to update status: #{@status_response[:message]}"
28+
elsif !@comment_response[:ok]
29+
"Unable to post comment: #{@comment_response[:message]}"
30+
end
31+
{ ok: false, message: message }
32+
end
33+
end
34+
1635
self.title = "GitHub Pull Requests"
1736
self.description = "Update pull requests on GitHub"
1837

@@ -24,7 +43,9 @@ class Config < CC::Service::Config
2443
# additional information (github-slug, PR number, etc) we can't test much
2544
# else.
2645
def receive_test
27-
if config.update_status
46+
if config.update_status && config.add_comment
47+
ResponseAggregator.new(receive_test_status, receive_test_comment).response
48+
elsif config.update_status
2849
receive_test_status
2950
elsif config.add_comment
3051
receive_test_comment

test/github_pull_requests_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ def test_pull_request_comment_test_failure_bad_token
5757
assert !receive_test({ add_comment: true })[:ok], "Expected failed test of pull request"
5858
end
5959

60+
def test_pull_request_success_both
61+
@stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
62+
@stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user, repo" }, ""] }
63+
64+
assert receive_test({ update_status: true, add_comment: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
65+
end
66+
67+
def test_response_aggregator_success
68+
response = aggregrate_response({ok: true, message: "OK"}, {ok: true, message: "OK 2"})
69+
assert_equal response, { ok: true, message: "OK" }
70+
end
71+
72+
def test_response_aggregator_failure_both
73+
response = aggregrate_response({ok: false, message: "Bad Token"}, {ok: false, message: "Bad Stuff"})
74+
assert_equal response, { ok: false, message: "Unable to post comment or update status" }
75+
end
76+
77+
def test_response_aggregator_failure_status
78+
response = aggregrate_response({ok: false, message: "Bad Token"}, {ok: true, message: "OK"})
79+
assert !response[:ok], "Expected invalid response because status response is invalid"
80+
assert_match /Bad Token/, response[:message]
81+
end
82+
83+
84+
def test_response_aggregator_failure_status
85+
response = aggregrate_response({ok: true, message: "OK"}, {ok: false, message: "Bad Stuff"})
86+
assert !response[:ok], "Expected invalid response because comment response is invalid"
87+
assert_match /Bad Stuff/, response[:message]
88+
end
89+
6090
def test_pull_request_comment
6191
stub_existing_comments("pbrisbin/foo", 1, %w[Hey Yo])
6292

@@ -130,4 +160,8 @@ def receive_test(config, event_data = {})
130160
)
131161
end
132162

163+
def aggregrate_response(status_response, comment_response)
164+
CC::Service::GitHubPullRequests::ResponseAggregator.new(status_response, comment_response).response
165+
end
166+
133167
end

0 commit comments

Comments
 (0)