Skip to content

Commit

Permalink
support label names
Browse files Browse the repository at this point in the history
  • Loading branch information
Zettat123 committed May 11, 2024
1 parent e94723f commit 9471502
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
5 changes: 3 additions & 2 deletions modules/structs/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ type EditLabelOption struct {

// IssueLabelsOption a collection of labels
type IssueLabelsOption struct {
// list of label IDs
Labels []int64 `json:"labels"`
// Labels can be a list of intergers representing label IDs
// or a list of strings representing label names
Labels []any `json:"labels"`
}

// LabelTemplate info of a Label template
Expand Down
29 changes: 28 additions & 1 deletion routers/api/v1/repo/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9471502

Please sign in to comment.