diff --git a/lib/cc/services/github_enterprise_pull_request.rb b/lib/cc/services/github_enterprise_pull_request.rb
new file mode 100644
index 0000000..3e7dd01
--- /dev/null
+++ b/lib/cc/services/github_enterprise_pull_request.rb
@@ -0,0 +1,32 @@
+require_relative 'github_pull_requests'
+
+class CC::Service::GithubEnterprisePullRequest < CC::Service::GitHubPullRequests
+ class Config < CC::Service::Config
+ attribute :oauth_token, String,
+ label: "OAuth Token",
+ description: "A personal OAuth token with permissions for the repo. The owner of the token will be the author of the pull request update."
+ attribute :base_url, String,
+ label: "Base API URL",
+ description: "The Base URL to your Github Enterprise instance."
+ attribute :ssl_verification, Boolean,
+ default: true,
+ label: "SSL Verification",
+ description: "Turn this off at your own risk. (Useful for self signed certificates)"
+
+ validates :oauth_token, presence: true
+ validates :base_url, presence: true
+ end
+
+ def setup_http
+ http.ssl[:verify] = config.ssl_verification
+ super
+ end
+
+ def base_status_url(commit_sha)
+ "#{config.base_url}/repos/#{github_slug}/statuses/#{commit_sha}"
+ end
+
+ def user_url
+ "#{config.base_url}/user"
+ end
+end
diff --git a/lib/cc/services/github_pull_requests.rb b/lib/cc/services/github_pull_requests.rb
index 0683d87..8659d30 100644
--- a/lib/cc/services/github_pull_requests.rb
+++ b/lib/cc/services/github_pull_requests.rb
@@ -2,33 +2,19 @@ class CC::Service::GitHubPullRequests < CC::Service
class Config < CC::Service::Config
attribute :oauth_token, String,
label: "OAuth Token",
- description: "A personal OAuth token with permissions for the repo. The owner of the token will be the author of the pull request comment."
- attribute :update_status, Boolean,
- label: "Update status?",
- description: "Update the pull request status after analyzing?"
- attribute :add_comment, Boolean,
- label: "Add a comment?",
- description: "Comment on the pull request after analyzing?"
+ description: "A personal OAuth token with permissions for the repo. The owner of the token will be the author of the pull request status."
validates :oauth_token, presence: true
end
class ResponseAggregator
- def initialize(status_response, comment_response)
+ def initialize(status_response)
@status_response = status_response
- @comment_response = comment_response
end
def response
- return @status_response if @status_response[:ok] && @comment_response[:ok]
- message = if !@status_response[:ok] && !@comment_response[:ok]
- "Unable to post comment or update status"
- elsif !@status_response[:ok]
- "Unable to update status: #{@status_response[:message]}"
- elsif !@comment_response[:ok]
- "Unable to post comment: #{@comment_response[:message]}"
- end
- { ok: false, message: message }
+ error_message ||= "Unable to update status: #{@status_response[:message]}"
+ @status_response[:ok] ? @status_response : { ok: false, message: error_message}
end
end
@@ -37,21 +23,13 @@ def response
BASE_URL = "https://api.github.com"
BODY_REGEX = %r{Code Climate has analyzed this pull request}
- COMMENT_BODY = '
Code Climate has analyzed this pull request.'
# Just make sure we can access GH using the configured token. Without
# additional information (github-slug, PR number, etc) we can't test much
# else.
def receive_test
setup_http
-
- if config.update_status && config.add_comment
- ResponseAggregator.new(receive_test_status, receive_test_comment).response
- elsif config.update_status
- receive_test_status
- elsif config.add_comment
- receive_test_comment
- end
+ ResponseAggregator.new(receive_test_status).response
end
def receive_pull_request
@@ -61,7 +39,6 @@ def receive_pull_request
when "pending"
update_status("pending", "Code Climate is analyzing this code.")
when "success"
- add_comment
update_status("success", "Code Climate has analyzed this pull request.")
end
end
@@ -69,26 +46,14 @@ def receive_pull_request
private
def update_status(state, description)
- if config.update_status
- body = {
- state: state,
- description: description,
- target_url: @payload["details_url"],
- context: "codeclimate"
- }.to_json
-
- http_post(status_url, body)
- end
- end
-
- def add_comment
- if config.add_comment && !comment_present?
- body = {
- body: COMMENT_BODY % @payload["compare_url"]
- }.to_json
+ body = {
+ state: state,
+ description: description,
+ target_url: @payload["details_url"],
+ context: "codeclimate"
+ }.to_json
- http_post(comments_url, body)
- end
+ http_post(status_url, body)
end
def receive_test_status
@@ -104,25 +69,6 @@ def receive_test_status
{ ok: false, message: ex.message }
end
- def receive_test_comment
- response = http_get(user_url)
- if response_includes_repo_scope?(response)
- { ok: true, message: "OAuth token is valid" }
- else
- { ok: false, message: "OAuth token requires 'repo' scope to post comments." }
- end
-
- rescue => ex
- { ok: false, message: ex.message }
- end
-
- def comment_present?
- response = http_get(comments_url)
- comments = JSON.parse(response.body)
-
- comments.any? { |comment| comment["body"] =~ BODY_REGEX }
- end
-
def setup_http
http.headers["Content-Type"] = "application/json"
http.headers["Authorization"] = "token #{config.oauth_token}"
@@ -137,10 +83,6 @@ def base_status_url(commit_sha)
"#{BASE_URL}/repos/#{github_slug}/statuses/#{commit_sha}"
end
- def comments_url
- "#{BASE_URL}/repos/#{github_slug}/issues/#{number}/comments"
- end
-
def user_url
"#{BASE_URL}/user"
end
diff --git a/pull_request_test.rb b/pull_request_test.rb
index 8d0664b..3b6f6e6 100755
--- a/pull_request_test.rb
+++ b/pull_request_test.rb
@@ -22,8 +22,6 @@ def call
service = CC::Service::GitHubPullRequests.new({
oauth_token: ENV.fetch("OAUTH_TOKEN"),
- update_status: true,
- add_comment: true,
}, {
name: "pull_request",
# https://github.com/codeclimate/nillson/pull/33
@@ -36,3 +34,19 @@ def call
CC::Service::Invocation.new(service) do |i|
i.wrap(WithResponseLogging)
end
+
+ghe_service = CC::Service::GithubEnterprisePullRequest.new({
+ oauth_token: ENV.fetch("OAUTH_TOKEN"),
+ base_url: ENV.fetch("GITHUB_BASE_URL"),
+ ssl_verification: false
+}, {
+ name: "pull_request",
+ state: "success",
+ github_slug: ENV.fetch("GITHUB_SLUG"),
+ number: ENV.fetch("GITHUB_PR_NUMBER"),
+ commit_sha: ENV.fetch("GITHUB_COMMIT_SHA")
+})
+
+CC::Service::Invocation.new(ghe_service) do |i|
+ i.wrap(WithResponseLogging)
+end
diff --git a/test/github_enterprise_pull_request_test.rb b/test/github_enterprise_pull_request_test.rb
new file mode 100644
index 0000000..3e58306
--- /dev/null
+++ b/test/github_enterprise_pull_request_test.rb
@@ -0,0 +1,88 @@
+require File.expand_path('../helper', __FILE__)
+
+class TestGithubEnterprisePullRequests < CC::Service::TestCase
+ def test_pull_request_status_pending
+ expect_status_update("pbrisbin/foo", "abc123", {
+ "state" => "pending",
+ "description" => /is analyzing/,
+ })
+
+ receive_pull_request({ update_status: true }, {
+ github_slug: "pbrisbin/foo",
+ commit_sha: "abc123",
+ state: "pending",
+ })
+ end
+
+ def test_pull_request_status_success
+ expect_status_update("pbrisbin/foo", "abc123", {
+ "state" => "success",
+ "description" => /has analyzed/,
+ })
+
+ receive_pull_request({ update_status: true }, {
+ github_slug: "pbrisbin/foo",
+ commit_sha: "abc123",
+ state: "success",
+ })
+ end
+
+ def test_pull_request_status_test_success
+ @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
+
+ assert receive_test({ update_status: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
+ end
+
+ def test_pull_request_status_test_failure
+ @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [401, {}, ""] }
+
+ assert !receive_test({ update_status: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected failed test of pull request"
+ end
+
+ def test_response_aggregator_success
+ response = aggregrate_response({ok: true, message: "OK"},)
+ assert_equal response, { ok: true, message: "OK" }
+ end
+
+ def test_response_aggregator_failure_status
+ response = aggregrate_response({ok: false, message: "Bad Token"})
+ assert !response[:ok], "Expected invalid response because status response is invalid"
+ assert_match /Bad Token/, response[:message]
+ end
+
+private
+
+ def expect_status_update(repo, commit_sha, params)
+ @stubs.post "repos/#{repo}/statuses/#{commit_sha}" do |env|
+ assert_equal "token 123", env[:request_headers]["Authorization"]
+
+ body = JSON.parse(env[:body])
+
+ params.each do |k, v|
+ assert v === body[k],
+ "Unexpected value for #{k}. #{v.inspect} !== #{body[k].inspect}"
+ end
+ end
+ end
+
+ def receive_pull_request(config, event_data)
+ receive(
+ CC::Service::GithubEnterprisePullRequest,
+ { oauth_token: "123", base_url: "http://github.test.com" }.merge(config),
+ { name: "pull_request" }.merge(event_data)
+ )
+ end
+
+ def receive_test(config, event_data = {})
+ receive(
+ CC::Service::GithubEnterprisePullRequest,
+ { oauth_token: "123", base_url: "http://github.test.com" }.merge(config),
+ { name: "test" }.merge(event_data)
+ )
+ end
+
+ def aggregrate_response(status_response)
+ CC::Service::GithubEnterprisePullRequest::ResponseAggregator.new(status_response).response
+ end
+
+end
diff --git a/test/github_pull_requests_test.rb b/test/github_pull_requests_test.rb
index 638c487..9006f43 100644
--- a/test/github_pull_requests_test.rb
+++ b/test/github_pull_requests_test.rb
@@ -39,81 +39,17 @@ def test_pull_request_status_test_failure
assert !receive_test({ update_status: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected failed test of pull request"
end
- def test_pull_request_comment_test_success
- @stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user, repo" }, ""] }
-
- assert receive_test({ add_comment: true })[:ok], "Expected test of pull request to be true"
- end
-
- def test_pull_request_comment_test_failure_insufficient_permissions
- @stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user" }, ""] }
-
- assert !receive_test({ add_comment: true })[:ok], "Expected failed test of pull request"
- end
-
- def test_pull_request_comment_test_failure_bad_token
- @stubs.get("/user") { |env| [401, {}, ""] }
-
- assert !receive_test({ add_comment: true })[:ok], "Expected failed test of pull request"
- end
-
- def test_pull_request_success_both
- @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
- @stubs.get("/user") { |env| [200, { "x-oauth-scopes" => "gist, user, repo" }, ""] }
-
- assert receive_test({ update_status: true, add_comment: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
- end
-
def test_response_aggregator_success
- response = aggregrate_response({ok: true, message: "OK"}, {ok: true, message: "OK 2"})
+ response = aggregrate_response({ok: true, message: "OK"})
assert_equal response, { ok: true, message: "OK" }
end
- def test_response_aggregator_failure_both
- response = aggregrate_response({ok: false, message: "Bad Token"}, {ok: false, message: "Bad Stuff"})
- assert_equal response, { ok: false, message: "Unable to post comment or update status" }
- end
-
def test_response_aggregator_failure_status
- response = aggregrate_response({ok: false, message: "Bad Token"}, {ok: true, message: "OK"})
+ response = aggregrate_response({ok: false, message: "Bad Token"})
assert !response[:ok], "Expected invalid response because status response is invalid"
assert_match /Bad Token/, response[:message]
end
-
- def test_response_aggregator_failure_status
- response = aggregrate_response({ok: true, message: "OK"}, {ok: false, message: "Bad Stuff"})
- assert !response[:ok], "Expected invalid response because comment response is invalid"
- assert_match /Bad Stuff/, response[:message]
- end
-
- def test_pull_request_comment
- stub_existing_comments("pbrisbin/foo", 1, %w[Hey Yo])
-
- expect_comment("pbrisbin/foo", 1, %r{href="http://example.com">analyzed})
-
- receive_pull_request({ add_comment: true }, {
- github_slug: "pbrisbin/foo",
- number: 1,
- state: "success",
- compare_url: "http://example.com",
- })
- end
-
- def test_pull_request_comment_already_present
- stub_existing_comments("pbrisbin/foo", 1, [
- 'Code Climate has analyzed this pull request'
- ])
-
- # With no POST expectation, test will fail if request is made.
-
- receive_pull_request({ add_comment: true }, {
- github_slug: "pbrisbin/foo",
- number: 1,
- state: "success",
- })
- end
-
private
def expect_status_update(repo, commit_sha, params)
@@ -129,21 +65,6 @@ def expect_status_update(repo, commit_sha, params)
end
end
- def stub_existing_comments(repo, number, bodies)
- body = bodies.map { |b| { body: b } }.to_json
-
- @stubs.get("repos/#{repo}/issues/#{number}/comments") { [200, {}, body] }
- end
-
- def expect_comment(repo, number, content)
- @stubs.post "repos/#{repo}/issues/#{number}/comments" do |env|
- body = JSON.parse(env[:body])
- assert_equal "token 123", env[:request_headers]["Authorization"]
- assert content === body["body"],
- "Unexpected comment body. #{content.inspect} !== #{body["body"].inspect}"
- end
- end
-
def receive_pull_request(config, event_data)
receive(
CC::Service::GitHubPullRequests,
@@ -160,8 +81,8 @@ def receive_test(config, event_data = {})
)
end
- def aggregrate_response(status_response, comment_response)
- CC::Service::GitHubPullRequests::ResponseAggregator.new(status_response, comment_response).response
+ def aggregrate_response(status_response)
+ CC::Service::GitHubPullRequests::ResponseAggregator.new(status_response).response
end
end