Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/cc/service/response_check.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class CC::Service
class HTTPError < StandardError
attr_reader :response_body
attr_reader :response_body, :status

def initialize(message, response_body)
@response_body = response_body
def initialize(message, env)
@response_body = env[:body]
@status = env[:status]

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

raise HTTPError.new(message, env[:body])
raise HTTPError.new(message, env)
end
end

Expand Down
13 changes: 11 additions & 2 deletions lib/cc/services/github_pull_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ class Config < CC::Service::Config
def receive_test
setup_http

http_get("#{BASE_URL}")
http_post(base_status_url("0" * 40), "{}")

{ ok: true, message: "OAuth token is valid" }
rescue HTTPError => ex
if ex.status == 422 # response message: "No commit found for SHA"
{ ok: true, message: "OAuth token is valid" }
else ex.status == 401 # response message: "Bad credentials"
{ ok: false, message: ex.message }
end
rescue => ex
{ ok: false, message: ex.message }
end
Expand Down Expand Up @@ -84,6 +89,10 @@ def setup_http
end

def status_url
base_status_url(commit_sha)
end

def base_status_url(commit_sha)
"#{BASE_URL}/repos/#{github_slug}/statuses/#{commit_sha}"
end

Expand Down
10 changes: 6 additions & 4 deletions service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Example:
#
# $ SLACK_WEBHOOK_URL="http://..." bundle exec ruby service_test.rb
# $ GITHUBPULLREQUESTS_OAUTH_TOKEN=06083a4a060d358ca709939b1f00645777661c44 bundle exec ruby service_test.rb
#
# Other Environment variables used:
#
Expand All @@ -36,7 +37,7 @@ def initialize(klass, *params)
@params = params
end

def test
def test(payload = {})
config = {}

puts "-"*80
Expand All @@ -55,7 +56,7 @@ def test
puts " -> #{config.inspect}"
print " => "

test_service(@klass, config)
test_service(@klass, config, payload)
end

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

def test_service(klass, config)
def test_service(klass, config, payload)
repo_name = ENV["REPO_NAME"] || "App"

service = klass.new(config, name: :test, repo_name: repo_name)
service = klass.new(config, { name: :test, repo_name: repo_name }.merge(payload))

CC::Service::Invocation.new(service) do |i|
i.wrap(WithResponseLogging)
Expand All @@ -79,3 +80,4 @@ def test_service(klass, config)
ServiceTest.new(CC::Service::Flowdock, :api_token).test
ServiceTest.new(CC::Service::Jira, :username, :password, :domain, :project_id).test
ServiceTest.new(CC::Service::Asana, :api_key, :workspace_id, :project_id).test
ServiceTest.new(CC::Service::GitHubPullRequests, :oauth_token).test({ github_slug: "codeclimate/codeclimate" })
20 changes: 20 additions & 0 deletions test/github_pull_requests_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ def test_pull_request_status_success
})
end

def test_pull_request_test_success
@stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }

assert receive_test({}, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
end

def test_pull_request_test_failure
@stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [401, {}, ""] }

assert !receive_test({}, { github_slug: "pbrisbin/foo" })[:ok], "Expected failed test of pull request"
end

def test_pull_request_comment
stub_existing_comments("pbrisbin/foo", 1, %w[Hey Yo])

Expand Down Expand Up @@ -92,4 +104,12 @@ def receive_pull_request(config, event_data)
)
end

def receive_test(config, event_data)
receive(
CC::Service::GitHubPullRequests,
{ oauth_token: "123" }.merge(config),
{ name: "test" }.merge(event_data)
)
end

end