diff --git a/internal/pkg/githubapi/drift_detection.go b/internal/pkg/githubapi/drift_detection.go index 8546f575..4d3d0ea7 100644 --- a/internal/pkg/githubapi/drift_detection.go +++ b/internal/pkg/githubapi/drift_detection.go @@ -52,8 +52,7 @@ func generateDiffOutput(ghPrClientDetails GhPrClientDetails, defaultBranch strin if len(filesWithDiff) != 0 { diffOutput.WriteString("\n### Blame Links:\n") - githubURL := ghPrClientDetails.GhClientPair.v3Client.BaseURL.String() - blameUrlPrefix := githubURL + ghPrClientDetails.Owner + "/" + ghPrClientDetails.Repo + "/blame" + blameUrlPrefix := ghPrClientDetails.getBlameURLPrefix() for _, f := range filesWithDiff { diffOutput.WriteString("[" + f + "](" + blameUrlPrefix + "/HEAD/" + f + ")\n") // TODO consider switching HEAD to specific SHA diff --git a/internal/pkg/githubapi/drift_detection_test.go b/internal/pkg/githubapi/drift_detection_test.go index 41c93677..8c496a46 100644 --- a/internal/pkg/githubapi/drift_detection_test.go +++ b/internal/pkg/githubapi/drift_detection_test.go @@ -128,7 +128,7 @@ func TestGenerateDiffOutputDiffFileContent(t *testing.T) { @@ -1 +1 @@ -File A content +File B content` + "\n\n```\n\n" + `### Blame Links: -[source-path/file-1.text](https://api.github.com/AnOwner/Arepo/blame/HEAD/source-path/file-1.text) +[source-path/file-1.text](https://github.com/AnOwner/Arepo/blame/HEAD/source-path/file-1.text) ` var sourceFilesSHAs map[string]string diff --git a/internal/pkg/githubapi/github.go b/internal/pkg/githubapi/github.go index adc1b9b0..f9be31a7 100644 --- a/internal/pkg/githubapi/github.go +++ b/internal/pkg/githubapi/github.go @@ -28,7 +28,10 @@ import ( "golang.org/x/exp/maps" ) -const githubCommentMaxSize = 65536 +const ( + githubCommentMaxSize = 65536 + githubPublicBaseURL = "https://github.com" +) type DiffCommentData struct { DiffOfChangedComponents []argocd.DiffResult @@ -93,6 +96,14 @@ func (ghPrClientDetails *GhPrClientDetails) getPrMetadata(prBody string) { } } +func (ghPrClientDetails *GhPrClientDetails) getBlameURLPrefix() string { + githubHost := getEnv("GITHUB_HOST", "") + if githubHost == "" { + githubHost = githubPublicBaseURL + } + return fmt.Sprintf("%s/%s/%s/blame", githubHost, ghPrClientDetails.Owner, ghPrClientDetails.Repo) +} + func HandlePREvent(eventPayload *github.PullRequestEvent, ghPrClientDetails GhPrClientDetails, mainGithubClientPair GhClientPair, approverGithubClientPair GhClientPair, ctx context.Context) { ghPrClientDetails.getPrMetadata(eventPayload.PullRequest.GetBody()) // wasCommitStatusSet and the placement of SetCommitStatus in the flow is used to ensure an API call is only made where it needed diff --git a/internal/pkg/githubapi/github_test.go b/internal/pkg/githubapi/github_test.go index a2d965aa..2f99c449 100644 --- a/internal/pkg/githubapi/github_test.go +++ b/internal/pkg/githubapi/github_test.go @@ -3,6 +3,7 @@ package githubapi import ( "bytes" "encoding/json" + "fmt" "os" "testing" @@ -251,3 +252,36 @@ func TestPrBody(t *testing.T) { } assert.Equal(t, string(expectedPrBody), newPrBody) } + +func TestGhPrClientDetailsGetBlameURLPrefix(t *testing.T) { + t.Parallel() + tests := []struct { + Host string + Owner string + Repo string + ExpectURL string + }{ + { + "", + "commercetools", + "test", + fmt.Sprintf("%s/commercetools/test/blame", githubPublicBaseURL), + }, + { + "https://myserver.github.com", + "some-other-owner", + "some-other-repo", + "https://myserver.github.com/some-other-owner/some-other-repo/blame", + }, + } + + // reset the GITHUB_HOST env to prevent conflicts with other tests. + defer os.Unsetenv("GITHUB_HOST") + + for _, tc := range tests { + os.Setenv("GITHUB_HOST", tc.Host) + ghPrClientDetails := &GhPrClientDetails{Owner: tc.Owner, Repo: tc.Repo} + blameURLPrefix := ghPrClientDetails.getBlameURLPrefix() + assert.Equal(t, tc.ExpectURL, blameURLPrefix) + } +}