Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-12-01 14:26:10 +01:00

Don't let API add 2 exclusive labels from same scope (#27433) (#27460)

Backport #27433 by @JakobDev

Fixes #27380

Co-authored-by: JakobDev <jakobdev@gmx.de>
This commit is contained in:
Giteabot 2023-10-06 05:06:31 +08:00 committed by GitHub
parent aaf35ee49c
commit 44aca6a65a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 1 deletions

View file

@ -54,6 +54,8 @@ func newIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *user_m
return err return err
} }
issue.Labels = append(issue.Labels, label)
return updateLabelCols(ctx, label, "num_issues", "num_closed_issue") return updateLabelCols(ctx, label, "num_issues", "num_closed_issue")
} }
@ -122,6 +124,11 @@ func newIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *us
if err = issue.LoadRepo(ctx); err != nil { if err = issue.LoadRepo(ctx); err != nil {
return err return err
} }
if err = issue.LoadLabels(ctx); err != nil {
return err
}
for _, l := range labels { for _, l := range labels {
// Don't add already present labels and invalid labels // Don't add already present labels and invalid labels
if HasIssueLabel(ctx, issue.ID, l.ID) || if HasIssueLabel(ctx, issue.ID, l.ID) ||
@ -129,6 +136,10 @@ func newIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *us
continue continue
} }
if err = RemoveDuplicateExclusiveIssueLabels(ctx, issue, l, doer); err != nil {
return err
}
if err = newIssueLabel(ctx, issue, l, doer); err != nil { if err = newIssueLabel(ctx, issue, l, doer); err != nil {
return fmt.Errorf("newIssueLabel: %w", err) return fmt.Errorf("newIssueLabel: %w", err)
} }

View file

@ -0,0 +1,28 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package issues_test
import (
"testing"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"github.com/stretchr/testify/assert"
)
func TestNewIssueLabelsScope(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 18})
label1 := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 7})
label2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 8})
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
assert.NoError(t, issues_model.NewIssueLabels(issue, []*issues_model.Label{label1, label2}, doer))
assert.Len(t, issue.Labels, 1)
assert.Equal(t, label2.ID, issue.Labels[0].ID)
}

View file

@ -317,7 +317,7 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption)
return nil, nil, err return nil, nil, err
} }
labels, err := issues_model.GetLabelsByIDs(ctx, form.Labels, "id", "repo_id", "org_id") labels, err := issues_model.GetLabelsByIDs(ctx, form.Labels, "id", "repo_id", "org_id", "name", "exclusive")
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelsByIDs", err) ctx.Error(http.StatusInternalServerError, "GetLabelsByIDs", err)
return nil, nil, err return nil, nil, err