diff --git a/pkg/giturl/giturl.go b/pkg/giturl/giturl.go index 737167da0..d64a40b1c 100644 --- a/pkg/giturl/giturl.go +++ b/pkg/giturl/giturl.go @@ -135,7 +135,25 @@ func GenerateLink(repo, commit, file string, line int64) string { fallthrough default: var baseLink string - if file == "" { + + //Gist links are formatted differently + if strings.HasPrefix(repo, "https://gist.github.com") { + baseLink = repo[:len(repo)-4] + "/" + if commit != "" { + baseLink += commit + "/" + } + if file != "" { + cleanedFileName := strings.ReplaceAll(file, ".", "-") + baseLink += "#file-" + cleanedFileName + } + if line > 0 { + if strings.Contains(baseLink, "#") { + baseLink += "-L" + strconv.FormatInt(line, 10) + } else { + baseLink += "#L" + strconv.FormatInt(line, 10) + } + } + } else if file == "" { baseLink = repo[:len(repo)-4] + "/commit/" + commit } else { baseLink = repo[:len(repo)-4] + "/blob/" + commit + "/" + file diff --git a/pkg/giturl/giturl_test.go b/pkg/giturl/giturl_test.go index 59bb01436..69196b2e0 100644 --- a/pkg/giturl/giturl_test.go +++ b/pkg/giturl/giturl_test.go @@ -180,6 +180,26 @@ func TestGenerateLink(t *testing.T) { }, want: "https://onprem.customdomain.com/org/repo/commit/xyz123", }, + { + name: "gist link gen", + args: args{ + repo: "https://gist.github.com/joeleonjr/be68e34b002e236160dbb394bbda86fb.git", + commit: "e94c5a1d5607e68f1cae4962bc4dce5de522371b", + file: "test", + line: int64(4), + }, + want: "https://gist.github.com/joeleonjr/be68e34b002e236160dbb394bbda86fb/e94c5a1d5607e68f1cae4962bc4dce5de522371b/#file-test-L4", + }, + { + name: "gist link gen - file with multiple extensions", + args: args{ + repo: "https://gist.github.com/joeleonjr/be68e34b002e236160dbb394bbda86fb.git", + commit: "c64bf2345256cca7d2621f9cb78401e8860f82c8", + file: "test.txt.ps1", + line: int64(4), + }, + want: "https://gist.github.com/joeleonjr/be68e34b002e236160dbb394bbda86fb/c64bf2345256cca7d2621f9cb78401e8860f82c8/#file-test-txt-ps1-L4", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {