-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GetTaskURI for GitLab provider
This will be using the token when fetching tasks if the fetched URL is on the same host of where the Repository URL is. Signed-off-by: Chmouel Boudjnah <[email protected]>
- Loading branch information
1 parent
f80c589
commit afeee53
Showing
4 changed files
with
164 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package gitlab | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"regexp" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" | ||
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider" | ||
) | ||
|
||
type gitLabInfo struct { | ||
Host string | ||
GroupOrUser string | ||
Repository string | ||
Revision string | ||
FilePath string | ||
} | ||
|
||
// extractGitLabInfo generated with chatGPT https://chatgpt.com/share/e3c06a7e-3f16-4891-85c7-832b3e7f25c5 | ||
func extractGitLabInfo(gitlabURL string) (*gitLabInfo, error) { | ||
parsedURL, err := url.Parse(gitlabURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Regular expression to match the specific GitLab URL pattern | ||
re := regexp.MustCompile(`^/([^/]+(?:/[^/]+)*)/([^/]+)/-/blob/([^/]+)(/.*)?|^/([^/]+(?:/[^/]+)*)/([^/]+)/-/raw/([^/]+)(/.*)?`) | ||
matches := re.FindStringSubmatch(parsedURL.Path) | ||
|
||
if len(matches) == 0 { | ||
return nil, fmt.Errorf("URL does not match the expected GitLab pattern") | ||
} | ||
|
||
groupOrUser := "" | ||
repoName := "" | ||
revision := "" | ||
filePath := "" | ||
|
||
if matches[1] != "" { // For /blob/ URLs | ||
groupOrUser = matches[1] | ||
repoName = matches[2] | ||
revision = matches[3] | ||
if len(matches) >= 5 && matches[4] != "" { | ||
filePath = matches[4][1:] // Remove initial slash | ||
} | ||
} else if matches[5] != "" { // For /raw/ URLs | ||
groupOrUser = matches[5] | ||
repoName = matches[6] | ||
revision = matches[7] | ||
if len(matches) >= 9 && matches[8] != "" { | ||
filePath = matches[8][1:] // Remove initial slash | ||
} | ||
} | ||
|
||
return &gitLabInfo{ | ||
Host: parsedURL.Host, | ||
GroupOrUser: groupOrUser, | ||
Repository: repoName, | ||
Revision: revision, | ||
FilePath: filePath, | ||
}, nil | ||
} | ||
|
||
// GetTaskURI if we are getting a URL from the same URL where the provider is, | ||
// it means we can try to get the file with the provider token. | ||
func (v *Provider) GetTaskURI(ctx context.Context, event *info.Event, uri string) (bool, string, error) { | ||
if ret := provider.CompareHostOfURLS(uri, event.URL); !ret { | ||
return false, "", nil | ||
} | ||
extracted, err := extractGitLabInfo(uri) | ||
if err != nil { | ||
return false, "", err | ||
} | ||
|
||
nEvent := info.NewEvent() | ||
nEvent.Organization = extracted.GroupOrUser | ||
nEvent.Repository = extracted.Repository | ||
nEvent.BaseBranch = extracted.Revision | ||
ret, err := v.GetFileInsideRepo(ctx, nEvent, extracted.FilePath, extracted.Revision) | ||
if err != nil { | ||
return false, "", err | ||
} | ||
return true, ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package gitlab | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestExtractGitLabInfo(t *testing.T) { | ||
tests := []struct { | ||
url string | ||
name string | ||
expected *gitLabInfo | ||
}{ | ||
{ | ||
name: "custom host", | ||
url: "https://gitlab.chmouel.com/group/subgroup/repo/-/blob/main/README.md?ref_type=heads", | ||
expected: &gitLabInfo{ | ||
Host: "gitlab.chmouel.com", | ||
GroupOrUser: "group/subgroup", | ||
Repository: "repo", | ||
Revision: "main", | ||
FilePath: "README.md", | ||
}, | ||
}, | ||
{ | ||
name: "org repo", | ||
url: "https://gitlab.com/org/repo/-/blob/main/README.md", | ||
expected: &gitLabInfo{ | ||
Host: "gitlab.com", | ||
GroupOrUser: "org", | ||
Repository: "repo", | ||
Revision: "main", | ||
FilePath: "README.md", | ||
}, | ||
}, | ||
{ | ||
name: "long group and subgroups", | ||
url: "https://gitlab.com/gitlab-com/partners/alliance/corp/sandbox/another/foo-foo/-/raw/main/hello.txt?ref_type=heads", | ||
expected: &gitLabInfo{ | ||
Host: "gitlab.com", | ||
GroupOrUser: "gitlab-com/partners/alliance/corp/sandbox/another", | ||
Repository: "foo-foo", | ||
Revision: "main", | ||
FilePath: "hello.txt", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
info, err := extractGitLabInfo(tt.url) | ||
assert.NilError(t, err) | ||
assert.DeepEqual(t, info, tt.expected) | ||
}) | ||
} | ||
} |