Skip to content

Commit

Permalink
Don't let API add 2 exclusive labels from same scope (go-gitea#27433) (
Browse files Browse the repository at this point in the history
…go-gitea#27460)

Backport go-gitea#27433 by @JakobDev

Fixes go-gitea#27380

Co-authored-by: JakobDev <[email protected]>
  • Loading branch information
GiteaBot and JakobDev authored Oct 5, 2023
1 parent aaf35ee commit 44aca6a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
11 changes: 11 additions & 0 deletions models/issues/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func newIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *user_m
return err
}

issue.Labels = append(issue.Labels, label)

return updateLabelCols(ctx, label, "num_issues", "num_closed_issue")
}

Expand Down Expand Up @@ -122,13 +124,22 @@ func newIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *us
if err = issue.LoadRepo(ctx); err != nil {
return err
}

if err = issue.LoadLabels(ctx); err != nil {
return err
}

for _, l := range labels {
// Don't add already present labels and invalid labels
if HasIssueLabel(ctx, issue.ID, l.ID) ||
(l.RepoID != issue.RepoID && l.OrgID != issue.Repo.OwnerID) {
continue
}

if err = RemoveDuplicateExclusiveIssueLabels(ctx, issue, l, doer); err != nil {
return err
}

if err = newIssueLabel(ctx, issue, l, doer); err != nil {
return fmt.Errorf("newIssueLabel: %w", err)
}
Expand Down
28 changes: 28 additions & 0 deletions models/issues/issue_label_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion routers/api/v1/repo/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ 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")
labels, err := issues_model.GetLabelsByIDs(ctx, form.Labels, "id", "repo_id", "org_id", "name", "exclusive")
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelsByIDs", err)
return nil, nil, err
Expand Down

0 comments on commit 44aca6a

Please sign in to comment.