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

Commit 238daee

Browse files
committed
More robust test for GitHub Pull Request tokens
1 parent d664c11 commit 238daee

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/cc/service/response_check.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class CC::Service
22
class HTTPError < StandardError
3-
attr_reader :response_body
3+
attr_reader :response_body, :status
44

55
def initialize(message, env)
66
@response_body = env[:body]
7+
@status = env[:status]
78

89
super(message)
910
end

lib/cc/services/github_pull_requests.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ class Config < CC::Service::Config
2626
def receive_test
2727
setup_http
2828

29-
http_get("#{BASE_URL}")
29+
http_post(base_status_url("0" * 40))
3030

31-
{ ok: true, message: "OAuth token is valid" }
31+
rescue HTTPError => ex
32+
if ex.status == 422
33+
{ ok: true, message: "OAuth token is valid" }
34+
else ex.status == 401
35+
{ ok: false, message: ex.message }
36+
end
3237
rescue => ex
3338
{ ok: false, message: ex.message }
3439
end
@@ -84,6 +89,10 @@ def setup_http
8489
end
8590

8691
def status_url
92+
base_status_url(commit_sha)
93+
end
94+
95+
def base_status_url(commit_sha)
8796
"#{BASE_URL}/repos/#{github_slug}/statuses/#{commit_sha}"
8897
end
8998

test/github_pull_requests_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ def test_pull_request_status_success
2727
})
2828
end
2929

30+
def test_pull_request_test_success
31+
@stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
32+
33+
assert receive_test({}, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
34+
end
35+
36+
def test_pull_request_test_failure
37+
@stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [401, {}, ""] }
38+
39+
assert !receive_test({}, { github_slug: "pbrisbin/foo" })[:ok], "Expected failed test of pull request"
40+
end
41+
3042
def test_pull_request_comment
3143
stub_existing_comments("pbrisbin/foo", 1, %w[Hey Yo])
3244

@@ -92,4 +104,12 @@ def receive_pull_request(config, event_data)
92104
)
93105
end
94106

107+
def receive_test(config, event_data)
108+
receive(
109+
CC::Service::GitHubPullRequests,
110+
{ oauth_token: "123" }.merge(config),
111+
{ name: "test" }.merge(event_data)
112+
)
113+
end
114+
95115
end

0 commit comments

Comments
 (0)