-
Notifications
You must be signed in to change notification settings - Fork 365
feat: Gitea pull request label support #6101
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 1 commit
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 |
|---|---|---|
|
|
@@ -62,12 +62,25 @@ type giteaClient interface { | |
| opts gitea.ListPullRequestsOptions, | ||
| ) ([]*gitea.PullRequest, *gitea.Response, error) | ||
|
|
||
| ListRepoLabels( | ||
| owner string, | ||
| repo string, | ||
| opts gitea.ListLabelsOptions, | ||
| ) ([]*gitea.Label, *gitea.Response, error) | ||
|
|
||
| GetPullRequest( | ||
| owner string, | ||
| repo string, | ||
| number int64, | ||
| ) (*gitea.PullRequest, *gitea.Response, error) | ||
|
|
||
| AddIssueLabels( | ||
| owner string, | ||
| repo string, | ||
| number int64, | ||
| opts gitea.IssueLabelsOption, | ||
| ) ([]*gitea.Label, *gitea.Response, error) | ||
|
|
||
| MergePullRequest( | ||
| owner string, | ||
| repo string, | ||
|
|
@@ -133,6 +146,10 @@ func (p *provider) CreatePullRequest( | |
| if opts == nil { | ||
| opts = &gitprovider.CreatePullRequestOpts{} | ||
| } | ||
| labelIDs, err := p.resolveLabelIDs(opts.Labels) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| giteaPR, _, err := p.client.CreatePullRequest( | ||
| p.owner, | ||
| p.repo, | ||
|
|
@@ -149,15 +166,73 @@ func (p *provider) CreatePullRequest( | |
| if giteaPR == nil { | ||
| return nil, fmt.Errorf("unexpected nil pull request") | ||
| } | ||
| // TODO(krancour): Add label support. The Gitea SDK's AddIssueLabels expects | ||
| // label IDs ([]int64), but Kargo's CreatePullRequestOpts.Labels are names | ||
| // ([]string). A previous implementation attempted this but silently discarded | ||
| // the labels entirely. To fix properly: list repo labels, match by name to | ||
| // get IDs, then call AddIssueLabels. | ||
| if len(labelIDs) > 0 { | ||
| if _, _, err := p.client.AddIssueLabels( | ||
| p.owner, | ||
| p.repo, | ||
| giteaPR.Index, | ||
| gitea.IssueLabelsOption{Labels: labelIDs}, | ||
| ); err != nil { | ||
| return nil, fmt.Errorf( | ||
| "error adding labels to pull request %d: %w", | ||
| giteaPR.Index, | ||
| err, | ||
| ) | ||
| } | ||
| } | ||
| pr := convertGiteaPR(*giteaPR) | ||
| return &pr, nil | ||
| } | ||
|
|
||
| func (p *provider) resolveLabelIDs(labelNames []string) ([]int64, error) { | ||
| if len(labelNames) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| repoLabels, _, err := p.client.ListRepoLabels( | ||
| p.owner, | ||
| p.repo, | ||
| gitea.ListLabelsOptions{}, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error listing repository labels: %w", err) | ||
| } | ||
|
Member
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.
If a repository has more labels than the default, the function will not see them and will incorrectly report them as missing. One solution for making sure we are listing all labels here could be incrementing I'd like to see a test for this too.
Author
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. Paged label lookup now walks |
||
|
|
||
| labelIDsByName := make(map[string]int64, len(repoLabels)) | ||
| for _, repoLabel := range repoLabels { | ||
| if repoLabel == nil { | ||
| continue | ||
| } | ||
| labelIDsByName[repoLabel.Name] = repoLabel.ID | ||
| } | ||
|
Comment on lines
+191
to
+197
Member
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. This loop is unnecessary — you can build the map directly inside the pagination loop, eliminating the intermediate |
||
|
|
||
| labelIDs := make([]int64, 0, len(labelNames)) | ||
| seen := make(map[int64]struct{}, len(labelNames)) | ||
| missing := make([]string, 0) | ||
| for _, labelName := range labelNames { | ||
| labelID, ok := labelIDsByName[labelName] | ||
| if !ok { | ||
| missing = append(missing, labelName) | ||
| continue | ||
| } | ||
| if _, ok := seen[labelID]; ok { | ||
| continue | ||
| } | ||
| seen[labelID] = struct{}{} | ||
| labelIDs = append(labelIDs, labelID) | ||
| } | ||
| if len(missing) > 0 { | ||
| return nil, fmt.Errorf( | ||
| "labels not found in repository %s/%s: %s", | ||
| p.owner, | ||
| p.repo, | ||
| strings.Join(missing, ", "), | ||
| ) | ||
| } | ||
|
|
||
| return labelIDs, nil | ||
| } | ||
|
|
||
| // GetPullRequest implements gitprovider.Interface. | ||
| func (p *provider) GetPullRequest( | ||
| _ context.Context, | ||
|
|
||
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.
So if we get here it means we successfully created a PR but because we failed to add labels we report an error.
We could improve the error here e.g.
successfully created pr number %d but failed to add labels: %w.Although, I think a better solution would be to not have this be a separate call at all.
It looks like
CreatePullRequestOptionhas aLabelsfield: https://gitea.com/gitea/go-sdk/src/tag/gitea/v0.20.0/gitea/pull.go#L159 and since you already resolvelabelIDsbefore theCreatePullRequestcall it makes more sense to just add yourlabelIDsthere.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.
The separate
AddIssueLabelscall is gone. Resolved label IDs are now passed directly inCreatePullRequestOption.Labels, so label application stays part of PR creation instead of becoming a second failure point after the PR already exists. The updated test asserts that path, and I also validated it end-to-end against a live OrbStack-backed Gitea instance with a real paginated label set.