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

Commit d2d0c50

Browse files
committed
Merge pull request #49 from codeclimate/nd-new-pr-test-token
Use more accurate way of testing GitHub Pull Request service
2 parents 26b8ba8 + 81d032c commit d2d0c50

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

lib/cc/service/response_check.rb

Lines changed: 5 additions & 4 deletions
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

5-
def initialize(message, response_body)
6-
@response_body = response_body
5+
def initialize(message, env)
6+
@response_body = env[:body]
7+
@status = env[:status]
78

89
super(message)
910
end
@@ -17,7 +18,7 @@ def on_complete(env)
1718
message = error_message(env) ||
1819
"API request unsuccessful (#{env[:status]})"
1920

20-
raise HTTPError.new(message, env[:body])
21+
raise HTTPError.new(message, env)
2122
end
2223
end
2324

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 # response message: "No commit found for SHA"
33+
{ ok: true, message: "OAuth token is valid" }
34+
else ex.status == 401 # response message: "Bad credentials"
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

service_test.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# Example:
1212
#
1313
# $ SLACK_WEBHOOK_URL="http://..." bundle exec ruby service_test.rb
14+
# $ GITHUBPULLREQUESTS_OAUTH_TOKEN=06083a4a060d358ca709939b1f00645777661c44 bundle exec ruby service_test.rb
1415
#
1516
# Other Environment variables used:
1617
#
@@ -36,7 +37,7 @@ def initialize(klass, *params)
3637
@params = params
3738
end
3839

39-
def test
40+
def test(payload = {})
4041
config = {}
4142

4243
puts "-"*80
@@ -55,7 +56,7 @@ def test
5556
puts " -> #{config.inspect}"
5657
print " => "
5758

58-
test_service(@klass, config)
59+
test_service(@klass, config, payload)
5960
end
6061

6162
private
@@ -64,10 +65,10 @@ def to_env_var(param)
6465
"#{@klass.to_s.split("::").last}_#{param}".upcase
6566
end
6667

67-
def test_service(klass, config)
68+
def test_service(klass, config, payload)
6869
repo_name = ENV["REPO_NAME"] || "App"
6970

70-
service = klass.new(config, name: :test, repo_name: repo_name)
71+
service = klass.new(config, { name: :test, repo_name: repo_name }.merge(payload))
7172

7273
CC::Service::Invocation.new(service) do |i|
7374
i.wrap(WithResponseLogging)
@@ -79,3 +80,4 @@ def test_service(klass, config)
7980
ServiceTest.new(CC::Service::Flowdock, :api_token).test
8081
ServiceTest.new(CC::Service::Jira, :username, :password, :domain, :project_id).test
8182
ServiceTest.new(CC::Service::Asana, :api_key, :workspace_id, :project_id).test
83+
ServiceTest.new(CC::Service::GitHubPullRequests, :oauth_token).test({ github_slug: "codeclimate/codeclimate" })

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)