Skip to content

Commit

Permalink
fix(giturl): encode '%' in path (#2214)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmz authored Dec 14, 2023
1 parent f38b6a2 commit 22ae6a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/giturl/giturl.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func NormalizeOrgRepoURL(provider provider, repoURL string) (string, error) {
// Supports GitHub, GitLab, Bitbucket, and Azure Repos.
// If the provider supports hyperlinks to specific lines, the line number will be included.
func GenerateLink(repo, commit, file string, line int64) string {
// Some paths contain '%' which breaks |url.Parse| if not encoded.
// https://developer.mozilla.org/en-US/docs/Glossary/Percent-encoding
file = strings.Replace(file, "%", "%25", -1)

switch determineProvider(repo) {
case providerBitbucket:
return repo[:len(repo)-4] + "/commits/" + commit
Expand Down
10 changes: 10 additions & 0 deletions pkg/giturl/giturl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ func TestGenerateLink(t *testing.T) {
},
want: "https://gist.github.com/joeleonjr/be68e34b002e236160dbb394bbda86fb/c64bf2345256cca7d2621f9cb78401e8860f82c8/#file-test-txt-ps1-L4",
},
{
name: "link gen - file percent in path",
args: args{
repo: "https://github.com/GeekMasher/tree-sitter-hcl.git",
commit: "a7f23cc5795769262f5515e52902f86c1b768994",
file: "example/real_world_stuff/coreos/coreos%tectonic-installer%installer%frontend%ui-tests%output%metal.tfvars",
line: int64(1),
},
want: "https://github.com/GeekMasher/tree-sitter-hcl/blob/a7f23cc5795769262f5515e52902f86c1b768994/example/real_world_stuff/coreos/coreos%25tectonic-installer%25installer%25frontend%25ui-tests%25output%25metal.tfvars#L1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 22ae6a7

Please sign in to comment.