diff options
author | Giteabot <teabot@gitea.io> | 2024-05-13 14:28:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-13 14:28:11 +0800 |
commit | f806bbb8151e8f3a76ffc2d47e991cc2d99d34c6 (patch) | |
tree | 7bade14b38692fc62a01c8eb93e3ca28c7f7c584 /routers | |
parent | ebe8e63dfdcaba71ef4448b7def8a0b051e476cb (diff) | |
download | gitea-f806bbb8151e8f3a76ffc2d47e991cc2d99d34c6.tar.gz gitea-f806bbb8151e8f3a76ffc2d47e991cc2d99d34c6.zip |
Support using label names when changing issue labels (#30943) (#30958)
Backport #30943 by @Zettat123
Resolve #30917
Make the APIs for adding labels and replacing labels support both label
IDs and label names so the
[`actions/labeler`](https://github.com/actions/labeler) action can work
in Gitea.
<img width="600px"
src="https://github.com/go-gitea/gitea/assets/15528715/7835c771-f637-4c57-9ce5-e4fbf56fa0d3"
/>
Co-authored-by: Zettat123 <zettat123@gmail.com>
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo/issue_label.go | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go index 7d9f85d2aa..413693c5ed 100644 --- a/routers/api/v1/repo/issue_label.go +++ b/routers/api/v1/repo/issue_label.go @@ -5,7 +5,9 @@ package repo import ( + "fmt" "net/http" + "reflect" issues_model "code.gitea.io/gitea/models/issues" api "code.gitea.io/gitea/modules/structs" @@ -317,7 +319,32 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption) return nil, nil, err } - labels, err := issues_model.GetLabelsByIDs(ctx, form.Labels, "id", "repo_id", "org_id", "name", "exclusive") + var ( + labelIDs []int64 + labelNames []string + ) + for _, label := range form.Labels { + rv := reflect.ValueOf(label) + switch rv.Kind() { + case reflect.Float64: + labelIDs = append(labelIDs, int64(rv.Float())) + case reflect.String: + labelNames = append(labelNames, rv.String()) + } + } + if len(labelIDs) > 0 && len(labelNames) > 0 { + ctx.Error(http.StatusBadRequest, "InvalidLabels", "labels should be an array of strings or integers") + return nil, nil, fmt.Errorf("invalid labels") + } + if len(labelNames) > 0 { + labelIDs, err = issues_model.GetLabelIDsInRepoByNames(ctx, ctx.Repo.Repository.ID, labelNames) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetLabelIDsInRepoByNames", err) + return nil, nil, err + } + } + + labels, err := issues_model.GetLabelsByIDs(ctx, labelIDs, "id", "repo_id", "org_id", "name", "exclusive") if err != nil { ctx.Error(http.StatusInternalServerError, "GetLabelsByIDs", err) return nil, nil, err |