mirror of
https://github.com/go-gitea/gitea
synced 2025-07-03 09:07:19 +00:00
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>
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user