Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed gist direct link generation #2115

Merged
merged 3 commits into from
Nov 20, 2023
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
20 changes: 19 additions & 1 deletion pkg/giturl/giturl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions pkg/giturl/giturl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading