Skip to content

Commit fa8a49f

Browse files
authored
Merge pull request #2 from CodeYogiCo/github-com-pr
feat: github url
2 parents 222a964 + 6c78ba1 commit fa8a49f

5 files changed

Lines changed: 66 additions & 12 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ this project. Once you have that jacoco file, you can pass that path to coverage
4141
source_dirs:
4242
- src/main/java
4343
- src/main/kotlin
44-
gh_api_base_url: https://git.target.com
44+
# omit for public github.com (defaults to https://api.github.com)
45+
# for GitHub Enterprise, use the full API root including /api/v3
46+
gh_api_base_url: https://git.target.com/api/v3
4547
module: some-sub-module
4648
secrets:
4749
- source: pull_request_api_key
@@ -73,7 +75,9 @@ Once you have coverage.xml same can be passed as an input to plugin shown below
7375
coverage_file: coverage.xml
7476
source_dirs:
7577
- /vela/src/github.com/targetOSS/pull-request-code-coverage
76-
gh_api_base_url: https://git.target.com
78+
# omit for public github.com (defaults to https://api.github.com)
79+
# for GitHub Enterprise, use the full API root including /api/v3
80+
gh_api_base_url: https://git.target.com/api/v3
7781
secrets:
7882
- source: pull_request_api_key
7983
target: plugin_gh_api_key

internal/plugin/reporter/github_pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (s *GithubPullRequest) Write(changedLinesWithCoverage domain.SourceLineCove
5050
return errors.Wrap(bodyErr, "Failed creating payload for github")
5151
}
5252

53-
url := fmt.Sprintf("%v/api/v3/repos/%v/%v/issues/%v/comments", s.apiBaseURL, s.owner, s.repo, s.pr)
53+
url := fmt.Sprintf("%v/repos/%v/%v/issues/%v/comments", strings.TrimRight(s.apiBaseURL, "/"), s.owner, s.repo, s.pr)
5454

5555
req, newErr := s.httpClient.NewRequest(
5656
"POST",

internal/plugin/reporter/github_pr_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,50 @@ func TestGithubPullRequest_Write_FailedDo_BadStatus(t *testing.T) {
8787
assert.EqualError(t, e, "Failed calling github: bad status code: 400")
8888
}
8989

90+
func TestGithubPullRequest_Write_BuildsPublicGithubURL(t *testing.T) {
91+
92+
mockClient := &pluginhttp.MockClient{}
93+
request := httptest.NewRequest("POST", "http://anywhere", nil)
94+
95+
mockClient.On("NewRequest", "POST", "https://api.github.com/repos/some_owner/some_repo/issues/42/comments", mock.Anything).Return(request, nil)
96+
mockClient.On("Do", request).Return(&http.Response{StatusCode: 201, Body: io.NopCloser(strings.NewReader(""))}, nil)
97+
98+
writer := NewGithubPullRequest("KEY", "https://api.github.com", "42", "some_owner", "some_repo", mockClient, &pluginjson.DefaultClient{})
99+
100+
e := writer.Write(domain.SourceLineCoverageReport{
101+
domain.SourceLineCoverage{
102+
CoverageData: domain.CoverageData{
103+
CoveredInstructionCount: 1,
104+
},
105+
},
106+
})
107+
108+
assert.NoError(t, e)
109+
mockClient.AssertExpectations(t)
110+
}
111+
112+
func TestGithubPullRequest_Write_TrimsTrailingSlashFromEnterpriseURL(t *testing.T) {
113+
114+
mockClient := &pluginhttp.MockClient{}
115+
request := httptest.NewRequest("POST", "http://anywhere", nil)
116+
117+
mockClient.On("NewRequest", "POST", "https://git.target.com/api/v3/repos/some_owner/some_repo/issues/42/comments", mock.Anything).Return(request, nil)
118+
mockClient.On("Do", request).Return(&http.Response{StatusCode: 201, Body: io.NopCloser(strings.NewReader(""))}, nil)
119+
120+
writer := NewGithubPullRequest("KEY", "https://git.target.com/api/v3/", "42", "some_owner", "some_repo", mockClient, &pluginjson.DefaultClient{})
121+
122+
e := writer.Write(domain.SourceLineCoverageReport{
123+
domain.SourceLineCoverage{
124+
CoverageData: domain.CoverageData{
125+
CoveredInstructionCount: 1,
126+
},
127+
},
128+
})
129+
130+
assert.NoError(t, e)
131+
mockClient.AssertExpectations(t)
132+
}
133+
90134
func TestGithubPullRequest_Write_FailedJsonMarshal(t *testing.T) {
91135

92136
mockClient := &pluginjson.MockClient{}

internal/plugin/runner.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import (
1818
"github.com/target/pull-request-code-coverage/internal/plugin/sourcelines/unifieddiff"
1919
)
2020

21+
// defaultGithubAPIBaseURL is the public GitHub REST API root. GitHub Enterprise
22+
// users should set PARAMETER_GH_API_BASE_URL to their API root (e.g.
23+
// https://git.example.com/api/v3).
24+
const defaultGithubAPIBaseURL = "https://api.github.com"
25+
2126
type DefaultRunner struct{}
2227

2328
func NewRunner() *DefaultRunner {
@@ -66,8 +71,9 @@ func (*DefaultRunner) Run(propertyGetter func(string) (string, bool), changedSou
6671
}
6772

6873
ghAPIBaseURL, ghAPIBaseURLFound := propertyGetter("PARAMETER_GH_API_BASE_URL")
69-
if !ghAPIBaseURLFound {
70-
logrus.Info("PARAMETER_GH_API_BASE_URL was missing, will not send report to PR comments")
74+
if !ghAPIBaseURLFound || ghAPIBaseURL == "" {
75+
ghAPIBaseURL = defaultGithubAPIBaseURL
76+
logrus.Info(fmt.Sprintf("PARAMETER_GH_API_BASE_URL was missing, defaulting to %v", ghAPIBaseURL))
7177
}
7278

7379
repoPR, repoPRFound := propertyGetter("BUILD_PULL_REQUEST_NUMBER")
@@ -112,7 +118,7 @@ func (*DefaultRunner) Run(propertyGetter func(string) (string, bool), changedSou
112118

113119
reporters := []reporter.Reporter{reporter.NewSimple(reportDefaultOut)}
114120

115-
if ghAPIKeyFound && ghAPIBaseURLFound && repoPRFound && repoOwnerFound && repoNameFound {
121+
if ghAPIKeyFound && repoPRFound && repoOwnerFound && repoNameFound {
116122
reporters = append(reporters, reporter.NewGithubPullRequest(ghAPIKey, ghAPIBaseURL, repoPR, repoOwner, repoName, &pluginhttp.DefaultClient{}, &pluginjson.DefaultClient{}))
117123
}
118124
logrus.Info("enabled reporters are ")

internal/plugin/runner_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Covered Instructions -> 97% (177)
107107
Missed Instructions -> 3% (5)
108108
`, buf.String())
109109

110-
requestAsserter.AssertRequestWasMade(t, "/api/v3/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
110+
requestAsserter.AssertRequestWasMade(t, "/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
111111
"body": `Code Coverage Summary:
112112
113113
Lines Without Coverage Data -> 92% (2216)
@@ -178,7 +178,7 @@ Covered Instructions -> 97% (177)
178178
Missed Instructions -> 3% (5)
179179
`, buf.String())
180180

181-
requestAsserter.AssertRequestWasMade(t, "/api/v3/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
181+
requestAsserter.AssertRequestWasMade(t, "/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
182182
"body": `Code Coverage Summary:
183183
184184
Lines Without Coverage Data -> 92% (2216)
@@ -239,7 +239,7 @@ Covered Instructions -> 73% (8)
239239
Missed Instructions -> 27% (3)
240240
`, buf.String())
241241

242-
requestAsserter.AssertRequestWasMade(t, "/api/v3/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
242+
requestAsserter.AssertRequestWasMade(t, "/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
243243
"body": `*Modules: category-search*
244244
245245
Code Coverage Summary:
@@ -295,7 +295,7 @@ Covered Instructions -> 73% (8)
295295
Missed Instructions -> 27% (3)
296296
`, buf.String())
297297

298-
requestAsserter.AssertRequestWasMade(t, "/api/v3/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
298+
requestAsserter.AssertRequestWasMade(t, "/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
299299
"body": `*Modules: category-search*
300300
301301
Code Coverage Summary:
@@ -353,7 +353,7 @@ Covered Instructions -> 88% (42)
353353
Missed Instructions -> 12% (6)
354354
`, buf.String())
355355

356-
requestAsserter.AssertRequestWasMade(t, "/api/v3/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
356+
requestAsserter.AssertRequestWasMade(t, "/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
357357
"body": `*Modules: category-search*
358358
359359
Code Coverage Summary:
@@ -413,7 +413,7 @@ Covered Instructions -> 88% (42)
413413
Missed Instructions -> 12% (6)
414414
`, buf.String())
415415

416-
requestAsserter.AssertRequestWasMade(t, "/api/v3/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
416+
requestAsserter.AssertRequestWasMade(t, "/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
417417
"body": `*Modules: category-search*
418418
419419
Code Coverage Summary:

0 commit comments

Comments
 (0)