Skip to content
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: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ this project. Once you have that jacoco file, you can pass that path to coverage
source_dirs:
- src/main/java
- src/main/kotlin
gh_api_base_url: https://git.target.com
# omit for public github.com (defaults to https://api.github.com)
# for GitHub Enterprise, use the full API root including /api/v3
gh_api_base_url: https://git.target.com/api/v3
module: some-sub-module
secrets:
- source: pull_request_api_key
Expand Down Expand Up @@ -73,7 +75,9 @@ Once you have coverage.xml same can be passed as an input to plugin shown below
coverage_file: coverage.xml
source_dirs:
- /vela/src/github.com/targetOSS/pull-request-code-coverage
gh_api_base_url: https://git.target.com
# omit for public github.com (defaults to https://api.github.com)
# for GitHub Enterprise, use the full API root including /api/v3
gh_api_base_url: https://git.target.com/api/v3
secrets:
- source: pull_request_api_key
target: plugin_gh_api_key
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/reporter/github_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *GithubPullRequest) Write(changedLinesWithCoverage domain.SourceLineCove
return errors.Wrap(bodyErr, "Failed creating payload for github")
}

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

req, newErr := s.httpClient.NewRequest(
"POST",
Expand Down
44 changes: 44 additions & 0 deletions internal/plugin/reporter/github_pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,50 @@ func TestGithubPullRequest_Write_FailedDo_BadStatus(t *testing.T) {
assert.EqualError(t, e, "Failed calling github: bad status code: 400")
}

func TestGithubPullRequest_Write_BuildsPublicGithubURL(t *testing.T) {

mockClient := &pluginhttp.MockClient{}
request := httptest.NewRequest("POST", "http://anywhere", nil)

mockClient.On("NewRequest", "POST", "https://api.github.com/repos/some_owner/some_repo/issues/42/comments", mock.Anything).Return(request, nil)
mockClient.On("Do", request).Return(&http.Response{StatusCode: 201, Body: io.NopCloser(strings.NewReader(""))}, nil)

writer := NewGithubPullRequest("KEY", "https://api.github.com", "42", "some_owner", "some_repo", mockClient, &pluginjson.DefaultClient{})

e := writer.Write(domain.SourceLineCoverageReport{
domain.SourceLineCoverage{
CoverageData: domain.CoverageData{
CoveredInstructionCount: 1,
},
},
})

assert.NoError(t, e)
mockClient.AssertExpectations(t)
}

func TestGithubPullRequest_Write_TrimsTrailingSlashFromEnterpriseURL(t *testing.T) {

mockClient := &pluginhttp.MockClient{}
request := httptest.NewRequest("POST", "http://anywhere", nil)

mockClient.On("NewRequest", "POST", "https://git.target.com/api/v3/repos/some_owner/some_repo/issues/42/comments", mock.Anything).Return(request, nil)
mockClient.On("Do", request).Return(&http.Response{StatusCode: 201, Body: io.NopCloser(strings.NewReader(""))}, nil)

writer := NewGithubPullRequest("KEY", "https://git.target.com/api/v3/", "42", "some_owner", "some_repo", mockClient, &pluginjson.DefaultClient{})

e := writer.Write(domain.SourceLineCoverageReport{
domain.SourceLineCoverage{
CoverageData: domain.CoverageData{
CoveredInstructionCount: 1,
},
},
})

assert.NoError(t, e)
mockClient.AssertExpectations(t)
}

func TestGithubPullRequest_Write_FailedJsonMarshal(t *testing.T) {

mockClient := &pluginjson.MockClient{}
Expand Down
12 changes: 9 additions & 3 deletions internal/plugin/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"github.com/target/pull-request-code-coverage/internal/plugin/sourcelines/unifieddiff"
)

// defaultGithubAPIBaseURL is the public GitHub REST API root. GitHub Enterprise
// users should set PARAMETER_GH_API_BASE_URL to their API root (e.g.
// https://git.example.com/api/v3).
const defaultGithubAPIBaseURL = "https://api.github.com"

type DefaultRunner struct{}

func NewRunner() *DefaultRunner {
Expand Down Expand Up @@ -66,8 +71,9 @@ func (*DefaultRunner) Run(propertyGetter func(string) (string, bool), changedSou
}

ghAPIBaseURL, ghAPIBaseURLFound := propertyGetter("PARAMETER_GH_API_BASE_URL")
if !ghAPIBaseURLFound {
logrus.Info("PARAMETER_GH_API_BASE_URL was missing, will not send report to PR comments")
if !ghAPIBaseURLFound || ghAPIBaseURL == "" {
ghAPIBaseURL = defaultGithubAPIBaseURL
logrus.Info(fmt.Sprintf("PARAMETER_GH_API_BASE_URL was missing, defaulting to %v", ghAPIBaseURL))
}

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

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

if ghAPIKeyFound && ghAPIBaseURLFound && repoPRFound && repoOwnerFound && repoNameFound {
if ghAPIKeyFound && repoPRFound && repoOwnerFound && repoNameFound {
reporters = append(reporters, reporter.NewGithubPullRequest(ghAPIKey, ghAPIBaseURL, repoPR, repoOwner, repoName, &pluginhttp.DefaultClient{}, &pluginjson.DefaultClient{}))
}
logrus.Info("enabled reporters are ")
Expand Down
12 changes: 6 additions & 6 deletions internal/plugin/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Covered Instructions -> 97% (177)
Missed Instructions -> 3% (5)
`, buf.String())

requestAsserter.AssertRequestWasMade(t, "/api/v3/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
requestAsserter.AssertRequestWasMade(t, "/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
"body": `Code Coverage Summary:

Lines Without Coverage Data -> 92% (2216)
Expand Down Expand Up @@ -178,7 +178,7 @@ Covered Instructions -> 97% (177)
Missed Instructions -> 3% (5)
`, buf.String())

requestAsserter.AssertRequestWasMade(t, "/api/v3/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
requestAsserter.AssertRequestWasMade(t, "/repos/some_org/some_repo/issues/123/comments", "SOME_API_KEY", map[string]interface{}{
"body": `Code Coverage Summary:

Lines Without Coverage Data -> 92% (2216)
Expand Down Expand Up @@ -239,7 +239,7 @@ Covered Instructions -> 73% (8)
Missed Instructions -> 27% (3)
`, buf.String())

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

Code Coverage Summary:
Expand Down Expand Up @@ -295,7 +295,7 @@ Covered Instructions -> 73% (8)
Missed Instructions -> 27% (3)
`, buf.String())

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

Code Coverage Summary:
Expand Down Expand Up @@ -353,7 +353,7 @@ Covered Instructions -> 88% (42)
Missed Instructions -> 12% (6)
`, buf.String())

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

Code Coverage Summary:
Expand Down Expand Up @@ -413,7 +413,7 @@ Covered Instructions -> 88% (42)
Missed Instructions -> 12% (6)
`, buf.String())

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

Code Coverage Summary:
Expand Down
Loading