Skip to content

Commit 92ac337

Browse files
Zettat123earl-warren
authored andcommitted
Support org labels when adding labels by label names (go-gitea#32988)
Fix go-gitea#32891 (cherry picked from commit 44b4fb2)
1 parent 2ffa9a5 commit 92ac337

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

models/fixtures/label.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,14 @@
9696
num_issues: 0
9797
num_closed_issues: 0
9898
archived_unix: 0
99+
100+
-
101+
id: 10
102+
repo_id: 3
103+
org_id: 0
104+
name: repo3label1
105+
color: '#112233'
106+
exclusive: false
107+
num_issues: 0
108+
num_closed_issues: 0
109+
archived_unix: 0

models/issues/label.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ func GetLabelIDsInRepoByNames(ctx context.Context, repoID int64, labelNames []st
353353
Find(&labelIDs)
354354
}
355355

356+
// GetLabelIDsInOrgByNames returns a list of labelIDs by names in a given org.
357+
func GetLabelIDsInOrgByNames(ctx context.Context, orgID int64, labelNames []string) ([]int64, error) {
358+
labelIDs := make([]int64, 0, len(labelNames))
359+
return labelIDs, db.GetEngine(ctx).Table("label").
360+
Where("org_id = ?", orgID).
361+
In("name", labelNames).
362+
Asc("name").
363+
Cols("id").
364+
Find(&labelIDs)
365+
}
366+
356367
// BuildLabelNamesIssueIDsCondition returns a builder where get issue ids match label names
357368
func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
358369
return builder.Select("issue_label.issue_id").

routers/api/v1/repo/issue_label.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,30 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption)
350350
labelIDs = append(labelIDs, int64(rv.Float()))
351351
case reflect.String:
352352
labelNames = append(labelNames, rv.String())
353+
default:
354+
ctx.Error(http.StatusBadRequest, "InvalidLabel", "a label must be an integer or a string")
355+
return nil, nil, fmt.Errorf("invalid label")
353356
}
354357
}
355358
if len(labelIDs) > 0 && len(labelNames) > 0 {
356359
ctx.Error(http.StatusBadRequest, "InvalidLabels", "labels should be an array of strings or integers")
357360
return nil, nil, fmt.Errorf("invalid labels")
358361
}
359362
if len(labelNames) > 0 {
360-
labelIDs, err = issues_model.GetLabelIDsInRepoByNames(ctx, ctx.Repo.Repository.ID, labelNames)
363+
repoLabelIDs, err := issues_model.GetLabelIDsInRepoByNames(ctx, ctx.Repo.Repository.ID, labelNames)
361364
if err != nil {
362365
ctx.Error(http.StatusInternalServerError, "GetLabelIDsInRepoByNames", err)
363366
return nil, nil, err
364367
}
368+
labelIDs = append(labelIDs, repoLabelIDs...)
369+
if ctx.Repo.Owner.IsOrganization() {
370+
orgLabelIDs, err := issues_model.GetLabelIDsInOrgByNames(ctx, ctx.Repo.Owner.ID, labelNames)
371+
if err != nil {
372+
ctx.Error(http.StatusInternalServerError, "GetLabelIDsInOrgByNames", err)
373+
return nil, nil, err
374+
}
375+
labelIDs = append(labelIDs, orgLabelIDs...)
376+
}
365377
}
366378

367379
labels, err := issues_model.GetLabelsByIDs(ctx, labelIDs, "id", "repo_id", "org_id", "name", "exclusive")

tests/integration/api_issue_label_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,33 @@ func TestAPIAddIssueLabels(t *testing.T) {
120120
func TestAPIAddIssueLabelsWithLabelNames(t *testing.T) {
121121
require.NoError(t, unittest.LoadFixtures())
122122

123-
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
124-
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
123+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
124+
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 6, RepoID: repo.ID})
125125
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
126+
repoLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 10, RepoID: repo.ID})
127+
orgLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 4, OrgID: owner.ID})
126128

127-
session := loginUser(t, owner.Name)
128-
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
129-
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels",
130-
repo.OwnerName, repo.Name, issue.Index)
129+
user1Session := loginUser(t, "user1")
130+
token := getTokenForLoggedInUser(t, user1Session, auth_model.AccessTokenScopeWriteIssue)
131+
132+
// add the org label and the repo label to the issue
133+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels", owner.Name, repo.Name, issue.Index)
131134
req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{
132-
Labels: []any{"label1", "label2"},
135+
Labels: []any{repoLabel.Name, orgLabel.Name},
133136
}).AddTokenAuth(token)
134137
resp := MakeRequest(t, req, http.StatusOK)
135138
var apiLabels []*api.Label
136139
DecodeJSON(t, resp, &apiLabels)
137140
assert.Len(t, apiLabels, unittest.GetCount(t, &issues_model.IssueLabel{IssueID: issue.ID}))
138-
139141
var apiLabelNames []string
140142
for _, label := range apiLabels {
141143
apiLabelNames = append(apiLabelNames, label.Name)
142144
}
143-
assert.ElementsMatch(t, apiLabelNames, []string{"label1", "label2"})
145+
assert.ElementsMatch(t, apiLabelNames, []string{repoLabel.Name, orgLabel.Name})
146+
147+
// delete labels
148+
req = NewRequest(t, "DELETE", urlStr).AddTokenAuth(token)
149+
MakeRequest(t, req, http.StatusNoContent)
144150
}
145151

146152
func TestAPIAddIssueLabelsAutoDate(t *testing.T) {

0 commit comments

Comments
 (0)