Skip to content

Commit

Permalink
modify git blame url to use public base url without api prefix (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jared-logan-patrick-ct authored Oct 17, 2024
1 parent d60f34e commit a8a51a9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
3 changes: 1 addition & 2 deletions internal/pkg/githubapi/drift_detection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/githubapi/drift_detection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package githubapi
import (
"bytes"
"encoding/json"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -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)
}
}

0 comments on commit a8a51a9

Please sign in to comment.