Skip to content

Commit

Permalink
Handle branch name with slash
Browse files Browse the repository at this point in the history
We need to handle branch name with slash in the name, eg: feature/branch
The user can url encode it with the %2F character, and we are able to parse it
properly.

Fixes #1395

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Aug 29, 2023
1 parent 03bdd33 commit e229601
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
9 changes: 5 additions & 4 deletions docs/content/docs/guide/resolver.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ and the remote HTTP URLs is a referenced GitHub "blob" URL:

<https://github.com/organization/repository/blob/mainbranch/path/file>

or a GitHub rawURL (rawurl reference is only working on public GitHub):
if the remote HTTP url has a slash (/) in the branch name you will need to html encode with the %20F character, eg:

<https://raw.githubusercontent.com/organization/repository/mainbranch/path/file>

It will be able to fetch the files from that private repository with the GitHub app token.
```text
https://github.com/organization/repository/blob/feature%20Fmainbranch/path/file
```

It will be use the GitHub API with the generated token to fetch that file.
This allows you to reference a task or a pipeline from a private repository easily.

GitHub app token are scoped to the owner or organization where the repository is located.
Expand Down
22 changes: 21 additions & 1 deletion pkg/provider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ func detectGHERawURL(event *info.Event, taskHost string) bool {
// splitGithubURL Take a Github url and split it with org/repo path ref, supports rawURL
func splitGithubURL(event *info.Event, uri string) (string, string, string, string, error) {
pURL, err := url.Parse(uri)
splitted := strings.Split(pURL.Path, "/")
if err != nil {
return "", "", "", "", fmt.Errorf("URL %s does not seem to be a proper provider url: %w", uri, err)
}
path := pURL.Path
if pURL.RawPath != "" {
path = pURL.RawPath
}
splitted := strings.Split(path, "/")
if len(splitted) <= 3 {
return "", "", "", "", fmt.Errorf("URL %s does not seem to be a proper provider url: %w", uri, err)
}
Expand All @@ -96,6 +103,19 @@ func splitGithubURL(event *info.Event, uri string) (string, string, string, stri
default:
return "", "", "", "", fmt.Errorf("cannot recognize task as a Github URL to fetch: %s", uri)
}
// url decode the org, repo, ref and path
if spRef, err = url.QueryUnescape(spRef); err != nil {
return "", "", "", "", fmt.Errorf("cannot decode ref: %w", err)
}
if spPath, err = url.QueryUnescape(spPath); err != nil {
return "", "", "", "", fmt.Errorf("cannot decode path: %w", err)
}
if spOrg, err = url.QueryUnescape(spOrg); err != nil {
return "", "", "", "", fmt.Errorf("cannot decode org: %w", err)
}
if spRepo, err = url.QueryUnescape(spRepo); err != nil {
return "", "", "", "", fmt.Errorf("cannot decode repo: %w", err)
}
return spOrg, spRepo, spPath, spRef, nil
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/provider/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ func TestGithubSplitURL(t *testing.T) {
wantRef: "main",
wantPath: "testdatas/remote_task.yaml",
},
{
name: "Split URL with slash in branch",
url: "https://github.com/openshift-pipelines/pipelines-as-code/blob/feature%2Fbranch/testdatas/remote_task.yaml",
wantOrg: "openshift-pipelines",
wantRepo: "pipelines-as-code",
wantRef: "feature/branch",
wantPath: "testdatas/remote_task.yaml",
},
{
name: "Split URL with encoding emoji in branch",
url: "https://github.com/openshift-pipelines/pipelines-as-code/blob/%F0%9F%99%83/filename.yaml",
wantOrg: "openshift-pipelines",
wantRepo: "pipelines-as-code",
wantRef: "🙃",
wantPath: "filename.yaml",
},
{
name: "Split URL with url encoding emoji in filename",
url: "https://github.com/openshift-pipelines/pipelines-as-code/blob/branch/anemoji%F0%9F%99%83.yaml",
wantOrg: "openshift-pipelines",
wantRepo: "pipelines-as-code",
wantRef: "branch",
wantPath: "anemoji🙃.yaml",
},
{
name: "Split raw URL",
url: "https://raw.githubusercontent.com/openshift-pipelines/pipelines-as-code/main/testdatas/remote_task.yaml",
Expand Down

0 comments on commit e229601

Please sign in to comment.