Skip to content

Commit 7d3d940

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 environment to handle that case better.
1 parent b4771cb commit 7d3d940

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

lib/cc/services/github_issues.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Config < CC::Service::Config
1717
self.description = "Open issues on GitHub"
1818
self.issue_tracker = true
1919

20-
BASE_URL = "https://api.github.com"
20+
DEFAULT_BASE_URL = "https://api.github.com".freeze
2121

2222
def receive_test
2323
result = create_issue("Test ticket from Code Climate", "")
@@ -55,6 +55,10 @@ def receive_issue
5555

5656
private
5757

58+
def base_url
59+
ENV["CODECLIMATE_GITHUB_API_URL"] || DEFAULT_BASE_URL
60+
end
61+
5862
def create_issue(title, issue_body)
5963
params = { title: title, body: issue_body }
6064

@@ -66,7 +70,7 @@ def create_issue(title, issue_body)
6670
http.headers["Authorization"] = "token #{config.oauth_token}"
6771
http.headers["User-Agent"] = "Code Climate"
6872

69-
url = "#{BASE_URL}/repos/#{config.project}/issues"
73+
url = "#{base_url}/repos/#{config.project}/issues"
7074
service_post(url, params.to_json) do |response|
7175
body = JSON.parse(response.body)
7276
{

lib/cc/services/github_pull_requests.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Config < CC::Service::Config
1818
self.title = "GitHub Pull Requests"
1919
self.description = "Update pull requests on GitHub"
2020

21-
BASE_URL = "https://api.github.com"
21+
DEFAULT_BASE_URL = "https://api.github.com".freeze
2222
BODY_REGEX = %r{<b>Code Climate</b> has <a href=".*">analyzed this pull request</a>}
2323
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>.'
2424
MESSAGES = [
@@ -175,20 +175,24 @@ def setup_http
175175
http.headers["User-Agent"] = "Code Climate"
176176
end
177177

178+
def base_url
179+
ENV["CODECLIMATE_GITHUB_API_URL"] || DEFAULT_BASE_URL
180+
end
181+
178182
def status_url
179183
base_status_url(commit_sha)
180184
end
181185

182186
def base_status_url(commit_sha)
183-
"#{BASE_URL}/repos/#{github_slug}/statuses/#{commit_sha}"
187+
"#{base_url}/repos/#{github_slug}/statuses/#{commit_sha}"
184188
end
185189

186190
def comments_url
187-
"#{BASE_URL}/repos/#{github_slug}/issues/#{number}/comments"
191+
"#{base_url}/repos/#{github_slug}/issues/#{number}/comments"
188192
end
189193

190194
def user_url
191-
"#{BASE_URL}/user"
195+
"#{base_url}/user"
192196
end
193197

194198
def github_slug

test/github_issues_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ 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+
old_base = ENV["CODECLIMATE_GITHUB_API_URL"]
65+
ENV["CODECLIMATE_GITHUB_API_URL"] = "http://example.com"
66+
67+
@stubs.post request_url do |env|
68+
assert env[:url].to_s == "http://example.com/#{request_url}"
69+
[200, {}, '{"number": 2, "html_url": "http://foo.bar"}']
70+
end
71+
72+
response = receive_event(name: "test")
73+
74+
assert_equal "Issue <a href='http://foo.bar'>#2</a> created.", response[:message]
75+
ensure
76+
ENV["CODECLIMATE_GITHUB_API_URL"] = old_base
77+
end
78+
6379
private
6480

6581
def project

test/github_pull_requests_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ 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+
old_base = ENV["CODECLIMATE_GITHUB_API_URL"]
249+
ENV["CODECLIMATE_GITHUB_API_URL"] = "http://example.com"
250+
251+
@stubs.get("/user") do |env|
252+
assert env[:url].to_s == "http://example.com/user"
253+
[200, { "x-oauth-scopes" => "gist, user, repo" }, ""]
254+
end
255+
256+
assert receive_test({ add_comment: true })[:ok], "Expected test of pull request to be true"
257+
ensure
258+
ENV["CODECLIMATE_GITHUB_API_URL"] = old_base
259+
end
260+
247261
private
248262

249263
def expect_status_update(repo, commit_sha, params)

0 commit comments

Comments
 (0)