-
Notifications
You must be signed in to change notification settings - Fork 128
feat(gitea): implement GetTaskURI for remote task resolution #2732
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/url" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "path" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "regexp" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -149,9 +150,68 @@ func (v *Provider) SetPacInfo(pacInfo *info.PacOpts) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| v.pacInfo = pacInfo | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // GetTaskURI TODO: Implement ME. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| func (v *Provider) GetTaskURI(_ context.Context, _ *info.Event, _ string) (bool, string, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return false, "", nil | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // splitGiteaURL parses a Gitea/Forgejo URL and returns org, repo, path, and ref. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| func splitGiteaURL(uri string) (string, string, string, string, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| pURL, err := url.Parse(uri) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "", "", "", "", fmt.Errorf("URL %s is not a valid provider URL: %w", uri, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| uriPath := pURL.Path | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if pURL.RawPath != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| uriPath = pURL.RawPath | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| split := strings.Split(uriPath, "/") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // minimum: /owner/repo/{src|raw}/{branch|tag|commit}/ref/filepath β 7 segments (split[0] is empty) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(split) < 7 { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "", "", "", "", fmt.Errorf("URL %s does not seem to be a proper Gitea URL: not enough path segments", uri) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| spOrg := split[1] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| spRepo := split[2] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| action := split[3] // "src" or "raw" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| refType := split[4] // "branch", "tag", or "commit" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if action != "src" && action != "raw" { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "", "", "", "", fmt.Errorf("cannot recognize URL as a Gitea URL to fetch: %s (expected 'src' or 'raw' in path)", uri) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if refType != "branch" && refType != "tag" && refType != "commit" { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "", "", "", "", fmt.Errorf("cannot recognize URL as a Gitea URL to fetch: %s (expected 'branch', 'tag', or 'commit' in path)", uri) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| spRef := split[5] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| spPath := strings.Join(split[6:], "/") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+184
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When decoding path segments,
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return spOrg, spRepo, spPath, spRef, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func (v *Provider) GetTaskURI(_ context.Context, event *info.Event, uri string) (bool, string, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if ret := provider.CompareHostOfURLS(uri, event.URL); !ret { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return false, "", nil | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| spOrg, spRepo, spPath, spRef, err := splitGiteaURL(uri) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return false, "", err | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| data, _, err := v.Client().GetFile(spOrg, spRepo, spRef, spPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return false, "", err | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return true, string(data), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+210
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func (v *Provider) SetLogger(logger *zap.SugaredLogger) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
pURL.Pathfor splitting is problematic because it is already unescaped byurl.Parse. If a path segment (such as a branch name or a directory) contains an encoded slash (%2F),pURL.Pathwill contain a literal/, causingstrings.Splitto break the segment incorrectly. UsingpURL.EscapedPath()ensures you split only on the intended delimiters.