Skip to content

Commit c6b5f49

Browse files
committed
Add support for specifying a separate Github base URL
When using GH:E, api.github.com will not be the endpoint used to handle PRs and issues. We can specify it in the configuration to handle that case better. Defaulting it to api.github.com means that we do not need to backfil any data.
1 parent b4771cb commit c6b5f49

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

lib/cc/services/github_issues.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class Config < CC::Service::Config
99
attribute :labels, String,
1010
label: "Labels (comma separated)",
1111
description: "Comma separated list of labels to apply to the issue"
12+
attribute :base_url, String,
13+
label: "Github API Base URL",
14+
description: "Base URL for the Github API",
15+
default: "https://api.github.com"
1216

1317
validates :oauth_token, presence: true
1418
end
@@ -17,8 +21,6 @@ class Config < CC::Service::Config
1721
self.description = "Open issues on GitHub"
1822
self.issue_tracker = true
1923

20-
BASE_URL = "https://api.github.com"
21-
2224
def receive_test
2325
result = create_issue("Test ticket from Code Climate", "")
2426
result.merge(
@@ -66,7 +68,7 @@ def create_issue(title, issue_body)
6668
http.headers["Authorization"] = "token #{config.oauth_token}"
6769
http.headers["User-Agent"] = "Code Climate"
6870

69-
url = "#{BASE_URL}/repos/#{config.project}/issues"
71+
url = "#{config.base_url}/repos/#{config.project}/issues"
7072
service_post(url, params.to_json) do |response|
7173
body = JSON.parse(response.body)
7274
{

lib/cc/services/github_pull_requests.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ class Config < CC::Service::Config
1111
attribute :add_comment, Boolean,
1212
label: "Add a comment?",
1313
description: "Comment on the pull request after analyzing?"
14+
attribute :base_url, String,
15+
label: "Github API Base URL",
16+
description: "Base URL for the Github API",
17+
default: "https://api.github.com"
1418

1519
validates :oauth_token, presence: true
1620
end
1721

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

21-
BASE_URL = "https://api.github.com"
2225
BODY_REGEX = %r{<b>Code Climate</b> has <a href=".*">analyzed this pull request</a>}
2326
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>.'
2427
MESSAGES = [
@@ -180,15 +183,15 @@ def status_url
180183
end
181184

182185
def base_status_url(commit_sha)
183-
"#{BASE_URL}/repos/#{github_slug}/statuses/#{commit_sha}"
186+
"#{config.base_url}/repos/#{github_slug}/statuses/#{commit_sha}"
184187
end
185188

186189
def comments_url
187-
"#{BASE_URL}/repos/#{github_slug}/issues/#{number}/comments"
190+
"#{config.base_url}/repos/#{github_slug}/issues/#{number}/comments"
188191
end
189192

190193
def user_url
191-
"#{BASE_URL}/user"
194+
"#{config.base_url}/user"
192195
end
193196

194197
def github_slug

test/github_issues_test.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ def test_receive_test
6060
assert_equal "Issue <a href='http://foo.bar'>#2</a> created.", response[:message]
6161
end
6262

63+
def test_different_base_url
64+
@stubs.post request_url do |env|
65+
assert env[:url].to_s == "http://example.com/#{request_url}"
66+
[200, {}, '{"number": 2, "html_url": "http://foo.bar"}']
67+
end
68+
69+
response = receive_event({ name: "test" }, base_url: "http://example.com")
70+
71+
assert_equal "Issue <a href='http://foo.bar'>#2</a> created.", response[:message]
72+
end
73+
6374
private
6475

6576
def project
@@ -86,10 +97,10 @@ def assert_github_receives(event_data, title, ticket_body)
8697
receive_event(event_data)
8798
end
8899

89-
def receive_event(event_data = nil)
100+
def receive_event(event_data = nil, config = {})
90101
receive(
91102
CC::Service::GitHubIssues,
92-
{ oauth_token: "123", project: project },
103+
{ oauth_token: "123", project: project }.merge(config),
93104
event_data || event(:quality, from: "D", to: "C")
94105
)
95106
end

test/github_pull_requests_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ def test_pull_request_nothing_happened
244244
assert_equal({ ok: false, message: "Nothing happened" }, response)
245245
end
246246

247+
def test_different_base_url
248+
@stubs.get("/user") do |env|
249+
assert env[:url].to_s == "http://example.com/user"
250+
[200, { "x-oauth-scopes" => "gist, user, repo" }, ""]
251+
end
252+
253+
assert receive_test({ add_comment: true, base_url: "http://example.com" })[:ok], "Expected test of pull request to be true"
254+
end
255+
247256
private
248257

249258
def expect_status_update(repo, commit_sha, params)

0 commit comments

Comments
 (0)