summaryrefslogtreecommitdiffstats
path: root/services/issue/label.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /services/issue/label.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to '')
-rw-r--r--services/issue/label.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/services/issue/label.go b/services/issue/label.go
new file mode 100644
index 0000000..6b8070d
--- /dev/null
+++ b/services/issue/label.go
@@ -0,0 +1,95 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package issue
+
+import (
+ "context"
+
+ "code.gitea.io/gitea/models/db"
+ issues_model "code.gitea.io/gitea/models/issues"
+ access_model "code.gitea.io/gitea/models/perm/access"
+ user_model "code.gitea.io/gitea/models/user"
+ notify_service "code.gitea.io/gitea/services/notify"
+)
+
+// ClearLabels clears all of an issue's labels
+func ClearLabels(ctx context.Context, issue *issues_model.Issue, doer *user_model.User) error {
+ if err := issues_model.ClearIssueLabels(ctx, issue, doer); err != nil {
+ return err
+ }
+
+ notify_service.IssueClearLabels(ctx, doer, issue)
+
+ return nil
+}
+
+// AddLabel adds a new label to the issue.
+func AddLabel(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, label *issues_model.Label) error {
+ if err := issues_model.NewIssueLabel(ctx, issue, label, doer); err != nil {
+ return err
+ }
+
+ notify_service.IssueChangeLabels(ctx, doer, issue, []*issues_model.Label{label}, nil)
+ return nil
+}
+
+// AddLabels adds a list of new labels to the issue.
+func AddLabels(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, labels []*issues_model.Label) error {
+ if err := issues_model.NewIssueLabels(ctx, issue, labels, doer); err != nil {
+ return err
+ }
+
+ notify_service.IssueChangeLabels(ctx, doer, issue, labels, nil)
+ return nil
+}
+
+// RemoveLabel removes a label from issue by given ID.
+func RemoveLabel(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, label *issues_model.Label) error {
+ dbCtx, committer, err := db.TxContext(ctx)
+ if err != nil {
+ return err
+ }
+ defer committer.Close()
+
+ if err := issue.LoadRepo(dbCtx); err != nil {
+ return err
+ }
+
+ perm, err := access_model.GetUserRepoPermission(dbCtx, issue.Repo, doer)
+ if err != nil {
+ return err
+ }
+ if !perm.CanWriteIssuesOrPulls(issue.IsPull) {
+ if label.OrgID > 0 {
+ return issues_model.ErrOrgLabelNotExist{}
+ }
+ return issues_model.ErrRepoLabelNotExist{}
+ }
+
+ if err := issues_model.DeleteIssueLabel(dbCtx, issue, label, doer); err != nil {
+ return err
+ }
+
+ if err := committer.Commit(); err != nil {
+ return err
+ }
+
+ notify_service.IssueChangeLabels(ctx, doer, issue, nil, []*issues_model.Label{label})
+ return nil
+}
+
+// ReplaceLabels removes all current labels and add new labels to the issue.
+func ReplaceLabels(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, labels []*issues_model.Label) error {
+ old, err := issues_model.GetLabelsByIssueID(ctx, issue.ID)
+ if err != nil {
+ return err
+ }
+
+ if err := issues_model.ReplaceIssueLabels(ctx, issue, labels, doer); err != nil {
+ return err
+ }
+
+ notify_service.IssueChangeLabels(ctx, doer, issue, labels, old)
+ return nil
+}