]> source.dussan.org Git - gitea.git/commitdiff
Support using label names when changing issue labels (#30943) (#30958)
authorGiteabot <teabot@gitea.io>
Mon, 13 May 2024 06:28:11 +0000 (14:28 +0800)
committerGitHub <noreply@github.com>
Mon, 13 May 2024 06:28:11 +0000 (14:28 +0800)
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>
modules/structs/issue_label.go
routers/api/v1/repo/issue_label.go
templates/swagger/v1_json.tmpl
tests/integration/api_issue_label_test.go

index bf68726d79379a61e985701bb58d4acaedbadc04..942cc0b3a1e5b6cb704d5522753a603dfdc4b8be 100644 (file)
@@ -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 integers representing label IDs
+       // or a list of strings representing label names
+       Labels []any `json:"labels"`
 }
 
 // LabelTemplate info of a Label template
index 7d9f85d2aa2580f327ec6fc49e88715be0aea551..413693c5ede9e3a7d8cd4224cfdbcc9031c9047d 100644 (file)
@@ -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
index e10018bba7b6aa48250e9ce6abe4ea600812da12..1634901086f4aeb5cb53c65bae25c48be84728bc 100644 (file)
       "type": "object",
       "properties": {
         "labels": {
-          "description": "list of label IDs",
+          "description": "Labels can be a list of integers representing label IDs\nor a list of strings representing label names",
           "type": "array",
-          "items": {
-            "type": "integer",
-            "format": "int64"
-          },
+          "items": {},
           "x-go-name": "Labels"
         }
       },
index 35c07182634e2824464cfab8df155d51b44c9608..0e4cd8243bce3c89e7f4daa8331a4329b51131cf 100644 (file)
@@ -104,7 +104,7 @@ func TestAPIAddIssueLabels(t *testing.T) {
        urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels",
                repo.OwnerName, repo.Name, issue.Index)
        req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{
-               Labels: []int64{1, 2},
+               Labels: []any{1, 2},
        }).AddTokenAuth(token)
        resp := MakeRequest(t, req, http.StatusOK)
        var apiLabels []*api.Label
@@ -114,6 +114,32 @@ func TestAPIAddIssueLabels(t *testing.T) {
        unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: 2})
 }
 
+func TestAPIAddIssueLabelsWithLabelNames(t *testing.T) {
+       assert.NoError(t, unittest.LoadFixtures())
+
+       repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
+       issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
+       owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
+
+       session := loginUser(t, owner.Name)
+       token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
+       urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels",
+               repo.OwnerName, repo.Name, issue.Index)
+       req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{
+               Labels: []any{"label1", "label2"},
+       }).AddTokenAuth(token)
+       resp := MakeRequest(t, req, http.StatusOK)
+       var apiLabels []*api.Label
+       DecodeJSON(t, resp, &apiLabels)
+       assert.Len(t, apiLabels, unittest.GetCount(t, &issues_model.IssueLabel{IssueID: issue.ID}))
+
+       var apiLabelNames []string
+       for _, label := range apiLabels {
+               apiLabelNames = append(apiLabelNames, label.Name)
+       }
+       assert.ElementsMatch(t, apiLabelNames, []string{"label1", "label2"})
+}
+
 func TestAPIReplaceIssueLabels(t *testing.T) {
        assert.NoError(t, unittest.LoadFixtures())
 
@@ -127,7 +153,7 @@ func TestAPIReplaceIssueLabels(t *testing.T) {
        urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels",
                owner.Name, repo.Name, issue.Index)
        req := NewRequestWithJSON(t, "PUT", urlStr, &api.IssueLabelsOption{
-               Labels: []int64{label.ID},
+               Labels: []any{label.ID},
        }).AddTokenAuth(token)
        resp := MakeRequest(t, req, http.StatusOK)
        var apiLabels []*api.Label
@@ -140,6 +166,29 @@ func TestAPIReplaceIssueLabels(t *testing.T) {
        unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
 }
 
+func TestAPIReplaceIssueLabelsWithLabelNames(t *testing.T) {
+       assert.NoError(t, unittest.LoadFixtures())
+
+       repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
+       issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
+       label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{RepoID: repo.ID})
+       owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
+
+       session := loginUser(t, owner.Name)
+       token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
+       urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels",
+               owner.Name, repo.Name, issue.Index)
+       req := NewRequestWithJSON(t, "PUT", urlStr, &api.IssueLabelsOption{
+               Labels: []any{label.Name},
+       }).AddTokenAuth(token)
+       resp := MakeRequest(t, req, http.StatusOK)
+       var apiLabels []*api.Label
+       DecodeJSON(t, resp, &apiLabels)
+       if assert.Len(t, apiLabels, 1) {
+               assert.EqualValues(t, label.Name, apiLabels[0].Name)
+       }
+}
+
 func TestAPIModifyOrgLabels(t *testing.T) {
        assert.NoError(t, unittest.LoadFixtures())