diff --git a/lib/cc/services/github_issues.rb b/lib/cc/services/github_issues.rb index 0ff03f6..bc5b86a 100644 --- a/lib/cc/services/github_issues.rb +++ b/lib/cc/services/github_issues.rb @@ -9,6 +9,10 @@ class Config < CC::Service::Config attribute :labels, String, label: "Labels (comma separated)", description: "Comma separated list of labels to apply to the issue" + attribute :base_url, String, + label: "Github API Base URL", + description: "Base URL for the Github API", + default: "https://api.github.com" validates :oauth_token, presence: true end @@ -17,8 +21,6 @@ class Config < CC::Service::Config self.description = "Open issues on GitHub" self.issue_tracker = true - BASE_URL = "https://api.github.com" - def receive_test result = create_issue("Test ticket from Code Climate", "") result.merge( @@ -66,7 +68,7 @@ def create_issue(title, issue_body) http.headers["Authorization"] = "token #{config.oauth_token}" http.headers["User-Agent"] = "Code Climate" - url = "#{BASE_URL}/repos/#{config.project}/issues" + url = "#{config.base_url}/repos/#{config.project}/issues" service_post(url, params.to_json) do |response| body = JSON.parse(response.body) { diff --git a/lib/cc/services/github_pull_requests.rb b/lib/cc/services/github_pull_requests.rb index f8e67c1..85f5b1b 100644 --- a/lib/cc/services/github_pull_requests.rb +++ b/lib/cc/services/github_pull_requests.rb @@ -11,6 +11,10 @@ class Config < CC::Service::Config attribute :add_comment, Boolean, label: "Add a comment?", description: "Comment on the pull request after analyzing?" + attribute :base_url, String, + label: "Github API Base URL", + description: "Base URL for the Github API", + default: "https://api.github.com" validates :oauth_token, presence: true end @@ -18,7 +22,6 @@ class Config < CC::Service::Config self.title = "GitHub Pull Requests" self.description = "Update pull requests on GitHub" - 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.' MESSAGES = [ @@ -180,15 +183,15 @@ def status_url end def base_status_url(commit_sha) - "#{BASE_URL}/repos/#{github_slug}/statuses/#{commit_sha}" + "#{config.base_url}/repos/#{github_slug}/statuses/#{commit_sha}" end def comments_url - "#{BASE_URL}/repos/#{github_slug}/issues/#{number}/comments" + "#{config.base_url}/repos/#{github_slug}/issues/#{number}/comments" end def user_url - "#{BASE_URL}/user" + "#{config.base_url}/user" end def github_slug diff --git a/test/github_issues_test.rb b/test/github_issues_test.rb index f52340c..17915d1 100644 --- a/test/github_issues_test.rb +++ b/test/github_issues_test.rb @@ -60,6 +60,17 @@ def test_receive_test assert_equal "Issue #2 created.", response[:message] end + def test_different_base_url + @stubs.post request_url do |env| + assert env[:url].to_s == "http://example.com/#{request_url}" + [200, {}, '{"number": 2, "html_url": "http://foo.bar"}'] + end + + response = receive_event({ name: "test" }, base_url: "http://example.com") + + assert_equal "Issue #2 created.", response[:message] + end + private def project @@ -86,10 +97,10 @@ def assert_github_receives(event_data, title, ticket_body) receive_event(event_data) end - def receive_event(event_data = nil) + def receive_event(event_data = nil, config = {}) receive( CC::Service::GitHubIssues, - { oauth_token: "123", project: project }, + { oauth_token: "123", project: project }.merge(config), event_data || event(:quality, from: "D", to: "C") ) end diff --git a/test/github_pull_requests_test.rb b/test/github_pull_requests_test.rb index 376352e..0dda93d 100644 --- a/test/github_pull_requests_test.rb +++ b/test/github_pull_requests_test.rb @@ -244,6 +244,15 @@ def test_pull_request_nothing_happened assert_equal({ ok: false, message: "Nothing happened" }, response) end + def test_different_base_url + @stubs.get("/user") do |env| + assert env[:url].to_s == "http://example.com/user" + [200, { "x-oauth-scopes" => "gist, user, repo" }, ""] + end + + assert receive_test({ add_comment: true, base_url: "http://example.com" })[:ok], "Expected test of pull request to be true" + end + private def expect_status_update(repo, commit_sha, params)