Skip to content

Add support for specifying a separate Github base URL #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2015
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
8 changes: 5 additions & 3 deletions lib/cc/services/github_issues.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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)
{
Expand Down
11 changes: 7 additions & 4 deletions lib/cc/services/github_pull_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ 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

self.title = "GitHub Pull Requests"
self.description = "Update pull requests on GitHub"

BASE_URL = "https://api.github.com"
BODY_REGEX = %r{<b>Code Climate</b> has <a href=".*">analyzed this pull request</a>}
COMMENT_BODY = '<img src="https://codeclimate.com/favicon.png" width="20" height="20" />&nbsp;<b>Code Climate</b> has <a href="%s">analyzed this pull request</a>.'
MESSAGES = [
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions test/github_issues_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def test_receive_test
assert_equal "Issue <a href='http://foo.bar'>#2</a> 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 <a href='http://foo.bar'>#2</a> created.", response[:message]
end

private

def project
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/github_pull_requests_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down