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

Commit ab39f58

Browse files
committed
Merge pull request #57 from codeclimate/ps-fix-update-status-disabled
Do not send PR status update if user disable it
2 parents 4aa7dfa + fd55ad0 commit ab39f58

File tree

2 files changed

+79
-43
lines changed

2 files changed

+79
-43
lines changed

lib/cc/services/github_pull_requests.rb

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,15 @@ def receive_test
4040

4141
def receive_pull_request
4242
setup_http
43+
state = @payload["state"]
4344

44-
case @payload["state"]
45-
when "pending"
46-
update_status_pending
47-
when "success"
48-
if config.update_status && config.add_comment
49-
update_status_success
50-
add_comment
51-
elsif config.update_status
52-
update_status_success
53-
elsif config.add_comment
54-
add_comment
55-
else
56-
simple_failure("Nothing happened")
57-
end
58-
when "skipped"
59-
if config.update_status
60-
update_status_skipped
61-
end
62-
when "error"
63-
update_status_error
45+
if %w(pending success skipped error).include?(state)
46+
send("update_status_#{state}")
6447
else
65-
simple_failure("Unknown state")
48+
@response = simple_failure("Unknown state")
6649
end
50+
51+
response
6752
end
6853

6954
private
@@ -72,19 +57,27 @@ def simple_failure(message)
7257
{ ok: false, message: message }
7358
end
7459

60+
def response
61+
@response || simple_failure("Nothing happened")
62+
end
63+
7564
def update_status_skipped
76-
update_status("success", "Code Climate has skipped analysis of this commit.")
65+
update_status(
66+
"success",
67+
"Code Climate has skipped analysis of this commit."
68+
)
7769
end
7870

7971
def update_status_success
8072
update_status("success", "Code Climate has analyzed this pull request.")
73+
add_comment
8174
end
8275

8376
def update_status_error
8477
update_status(
8578
"error",
8679
"Code Climate encountered an error while attempting to analyze this " +
87-
"pull request."
80+
"pull request."
8881
)
8982
end
9083

@@ -93,27 +86,34 @@ def update_status_pending
9386
end
9487

9588
def update_status(state, description)
96-
params = {
97-
state: state,
98-
description: description,
99-
target_url: @payload["details_url"],
100-
context: "codeclimate"
101-
}
102-
service_post(status_url, params.to_json)
89+
if config.update_status
90+
params = {
91+
state: state,
92+
description: description,
93+
target_url: @payload["details_url"],
94+
context: "codeclimate"
95+
}
96+
@response = service_post(status_url, params.to_json)
97+
end
10398
end
10499

105100
def add_comment
106-
if !comment_present?
107-
body = {
108-
body: COMMENT_BODY % @payload["compare_url"]
109-
}.to_json
110-
111-
service_post(comments_url, body) do |response|
112-
doc = JSON.parse(response.body)
113-
{ id: doc["id"] }
101+
if config.add_comment
102+
if !comment_present?
103+
body = {
104+
body: COMMENT_BODY % @payload["compare_url"]
105+
}.to_json
106+
107+
@response = service_post(comments_url, body) do |response|
108+
doc = JSON.parse(response.body)
109+
{ id: doc["id"] }
110+
end
111+
else
112+
@response = {
113+
ok: false,
114+
message: "Comment already present"
115+
}
114116
end
115-
else
116-
{ ok: false, message: "Comment already present" }
117117
end
118118
end
119119

test/github_pull_requests_test.rb

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ def test_no_status_update_for_skips_when_update_status_config_is_falsey
6363
})
6464
end
6565

66+
def test_no_status_update_for_pending_when_update_status_config_is_falsey
67+
# With no POST expectation, test will fail if request is made.
68+
69+
receive_pull_request({}, {
70+
github_slug: "pbrisbin/foo",
71+
commit_sha: "abc123",
72+
state: "pending",
73+
})
74+
end
75+
76+
def test_no_status_update_for_error_when_update_status_config_is_falsey
77+
# With no POST expectation, test will fail if request is made.
78+
79+
receive_pull_request({}, {
80+
github_slug: "pbrisbin/foo",
81+
commit_sha: "abc123",
82+
state: "error",
83+
})
84+
end
85+
86+
6687
def test_no_comment_for_skips_regardless_of_add_comment_config
6788
# With no POST expectation, test will fail if request is made.
6889

@@ -147,13 +168,28 @@ def test_pull_request_comment_already_present
147168

148169
# With no POST expectation, test will fail if request is made.
149170

150-
rsp = receive_pull_request({ add_comment: true, update_status: false }, {
171+
response = receive_pull_request({
172+
add_comment: true,
173+
update_status: false
174+
}, {
151175
github_slug: "pbrisbin/foo",
152176
number: 1,
153177
state: "success",
154178
})
155-
assert_not_nil rsp
156-
assert_false rsp[:ok]
179+
180+
assert_equal({ ok: false, message: "Comment already present" }, response)
181+
end
182+
183+
def test_pull_request_unknown_state
184+
response = receive_pull_request({}, { state: "unknown" })
185+
186+
assert_equal({ ok: false, message: "Unknown state" }, response)
187+
end
188+
189+
def test_pull_request_nothing_happened
190+
response = receive_pull_request({}, { state: "success" })
191+
192+
assert_equal({ ok: false, message: "Nothing happened" }, response)
157193
end
158194

159195
private

0 commit comments

Comments
 (0)