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

Commit f4c2a54

Browse files
committed
Test permission of OAuth token to post comments to GH
1 parent e265b10 commit f4c2a54

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

lib/cc/services/github_pull_requests.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ class Config < CC::Service::Config
2424
# additional information (github-slug, PR number, etc) we can't test much
2525
# else.
2626
def receive_test
27-
receive_test_status if config.update_status
27+
if config.update_status
28+
receive_test_status
29+
elsif config.add_comment
30+
receive_test_comment
31+
end
2832
end
2933

3034
def receive_test_status
@@ -42,6 +46,20 @@ def receive_test_status
4246
{ ok: false, message: ex.message }
4347
end
4448

49+
def receive_test_comment
50+
setup_http
51+
52+
response = http_get(user_url)
53+
if response_includes_repo_scope?(response)
54+
{ ok: true, message: "OAuth token is valid" }
55+
else
56+
{ ok: false, message: "OAuth token requires 'repo' scope to post comments." }
57+
end
58+
59+
rescue => ex
60+
{ ok: false, message: ex.message }
61+
end
62+
4563
def receive_pull_request
4664
setup_http
4765

@@ -104,6 +122,10 @@ def comments_url
104122
"#{BASE_URL}/repos/#{github_slug}/issues/#{number}/comments"
105123
end
106124

125+
def user_url
126+
"#{BASE_URL}/user"
127+
end
128+
107129
def github_slug
108130
@payload.fetch("github_slug")
109131
end
@@ -116,4 +138,8 @@ def number
116138
@payload.fetch("number")
117139
end
118140

141+
def response_includes_repo_scope?(response)
142+
response.headers['x-oauth-scopes'] && response.headers['x-oauth-scopes'].split(/\s*,\s*/).include?("repo")
143+
end
144+
119145
end

service_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +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
14+
# $ GITHUBPULLREQUESTS_UPDATE_STATUS=false GITHUBPULLREQUESTS_ADD_COMMENT=true GITHUBPULLREQUESTS_OAUTH_TOKEN=06083a4a060d358ca709939b1f00645777661c44 bundle exec ruby service_test.rb
1515
#
1616
# Other Environment variables used:
1717
#
@@ -80,4 +80,4 @@ def test_service(klass, config, payload)
8080
ServiceTest.new(CC::Service::Flowdock, :api_token).test
8181
ServiceTest.new(CC::Service::Jira, :username, :password, :domain, :project_id).test
8282
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" })
83+
ServiceTest.new(CC::Service::GitHubPullRequests, :oauth_token, :update_status, :add_comment).test({ github_slug: "codeclimate/codeclimate" })

test/github_pull_requests_test.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ def test_pull_request_status_test_failure
3939
assert !receive_test({ update_status: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected failed test of pull request"
4040
end
4141

42+
def test_pull_request_comment_test_success
43+
@stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user, repo" }, ""] }
44+
45+
assert receive_test({ add_comment: true })[:ok], "Expected test of pull request to be true"
46+
end
47+
48+
def test_pull_request_comment_test_failure_insufficient_permissions
49+
@stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user" }, ""] }
50+
51+
assert !receive_test({ add_comment: true })[:ok], "Expected failed test of pull request"
52+
end
53+
54+
def test_pull_request_comment_test_failure_bad_token
55+
@stubs.get("/user") { |env| [401, {}, ""] }
56+
57+
assert !receive_test({ add_comment: true })[:ok], "Expected failed test of pull request"
58+
end
59+
4260
def test_pull_request_comment
4361
stub_existing_comments("pbrisbin/foo", 1, %w[Hey Yo])
4462

@@ -104,7 +122,7 @@ def receive_pull_request(config, event_data)
104122
)
105123
end
106124

107-
def receive_test(config, event_data)
125+
def receive_test(config, event_data = {})
108126
receive(
109127
CC::Service::GitHubPullRequests,
110128
{ oauth_token: "123" }.merge(config),

0 commit comments

Comments
 (0)