Skip to content

Commit 4bafa1a

Browse files
committed
Merge branch 'main' into lunny/improve_get_treepath_latest_commit
2 parents 421011f + df98452 commit 4bafa1a

File tree

7 files changed

+776
-10
lines changed

7 files changed

+776
-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
@@ -349,6 +349,17 @@ func GetLabelIDsInRepoByNames(ctx context.Context, repoID int64, labelNames []st
349349
Find(&labelIDs)
350350
}
351351

352+
// GetLabelIDsInOrgByNames returns a list of labelIDs by names in a given org.
353+
func GetLabelIDsInOrgByNames(ctx context.Context, orgID int64, labelNames []string) ([]int64, error) {
354+
labelIDs := make([]int64, 0, len(labelNames))
355+
return labelIDs, db.GetEngine(ctx).Table("label").
356+
Where("org_id = ?", orgID).
357+
In("name", labelNames).
358+
Asc("name").
359+
Cols("id").
360+
Find(&labelIDs)
361+
}
362+
352363
// BuildLabelNamesIssueIDsCondition returns a builder where get issue ids match label names
353364
func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
354365
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
@@ -335,18 +335,30 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption)
335335
labelIDs = append(labelIDs, int64(rv.Float()))
336336
case reflect.String:
337337
labelNames = append(labelNames, rv.String())
338+
default:
339+
ctx.Error(http.StatusBadRequest, "InvalidLabel", "a label must be an integer or a string")
340+
return nil, nil, fmt.Errorf("invalid label")
338341
}
339342
}
340343
if len(labelIDs) > 0 && len(labelNames) > 0 {
341344
ctx.Error(http.StatusBadRequest, "InvalidLabels", "labels should be an array of strings or integers")
342345
return nil, nil, fmt.Errorf("invalid labels")
343346
}
344347
if len(labelNames) > 0 {
345-
labelIDs, err = issues_model.GetLabelIDsInRepoByNames(ctx, ctx.Repo.Repository.ID, labelNames)
348+
repoLabelIDs, err := issues_model.GetLabelIDsInRepoByNames(ctx, ctx.Repo.Repository.ID, labelNames)
346349
if err != nil {
347350
ctx.Error(http.StatusInternalServerError, "GetLabelIDsInRepoByNames", err)
348351
return nil, nil, err
349352
}
353+
labelIDs = append(labelIDs, repoLabelIDs...)
354+
if ctx.Repo.Owner.IsOrganization() {
355+
orgLabelIDs, err := issues_model.GetLabelIDsInOrgByNames(ctx, ctx.Repo.Owner.ID, labelNames)
356+
if err != nil {
357+
ctx.Error(http.StatusInternalServerError, "GetLabelIDsInOrgByNames", err)
358+
return nil, nil, err
359+
}
360+
labelIDs = append(labelIDs, orgLabelIDs...)
361+
}
350362
}
351363

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

0 commit comments

Comments
 (0)