fixed gist direct link generation (#2115)

* fixed gist direct link generation

* added two test cases for gist link generation
This commit is contained in:
joeleonjr 2023-11-20 13:41:19 -05:00 committed by GitHub
parent d69de658b2
commit cd9c1ae186
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View file

@ -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

View file

@ -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) {