summaryrefslogtreecommitdiffstats
path: root/services/issue/label.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-10-19 17:48:29 +0800
committerLauris BH <lauris@nix.lv>2019-10-19 12:48:29 +0300
commit280f4bebbf5883eef94d8a84b21575393ae5e0e1 (patch)
tree578594a27dbe25185ec5ce70f692e2f392d9b947 /services/issue/label.go
parent240f46a4220d9b8d38d084dca4ff47d442c12f9c (diff)
downloadgitea-280f4bebbf5883eef94d8a84b21575393ae5e0e1.tar.gz
gitea-280f4bebbf5883eef94d8a84b21575393ae5e0e1.zip
Move issue label operations to issue service package (#8553)
* Move issue label operations to issue service package * fix test * fix fmt
Diffstat (limited to 'services/issue/label.go')
-rw-r--r--services/issue/label.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/services/issue/label.go b/services/issue/label.go
index 4af8c2b97e..b393e5d38c 100644
--- a/services/issue/label.go
+++ b/services/issue/label.go
@@ -6,9 +6,57 @@ package issue
import (
"code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
+ api "code.gitea.io/gitea/modules/structs"
)
+func sendLabelUpdatedWebhook(issue *models.Issue, doer *models.User) {
+ var err error
+
+ if err = issue.LoadRepo(); err != nil {
+ log.Error("LoadRepo: %v", err)
+ return
+ }
+
+ if err = issue.LoadPoster(); err != nil {
+ log.Error("LoadPoster: %v", err)
+ return
+ }
+
+ mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
+ if issue.IsPull {
+ if err = issue.LoadPullRequest(); err != nil {
+ log.Error("loadPullRequest: %v", err)
+ return
+ }
+ if err = issue.PullRequest.LoadIssue(); err != nil {
+ log.Error("LoadIssue: %v", err)
+ return
+ }
+ err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
+ Action: api.HookIssueLabelUpdated,
+ Index: issue.Index,
+ PullRequest: issue.PullRequest.APIFormat(),
+ Repository: issue.Repo.APIFormat(models.AccessModeNone),
+ Sender: doer.APIFormat(),
+ })
+ } else {
+ err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
+ Action: api.HookIssueLabelUpdated,
+ Index: issue.Index,
+ Issue: issue.APIFormat(),
+ Repository: issue.Repo.APIFormat(mode),
+ Sender: doer.APIFormat(),
+ })
+ }
+ if err != nil {
+ log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
+ } else {
+ go models.HookQueue.Add(issue.RepoID)
+ }
+}
+
// ClearLabels clears all of an issue's labels
func ClearLabels(issue *models.Issue, doer *models.User) (err error) {
if err = issue.ClearLabels(doer); err != nil {
@@ -19,3 +67,45 @@ func ClearLabels(issue *models.Issue, doer *models.User) (err error) {
return nil
}
+
+// AddLabel adds a new label to the issue.
+func AddLabel(issue *models.Issue, doer *models.User, label *models.Label) error {
+ if err := models.NewIssueLabel(issue, label, doer); err != nil {
+ return err
+ }
+
+ sendLabelUpdatedWebhook(issue, doer)
+ return nil
+}
+
+// AddLabels adds a list of new labels to the issue.
+func AddLabels(issue *models.Issue, doer *models.User, labels []*models.Label) error {
+ if err := models.NewIssueLabels(issue, labels, doer); err != nil {
+ return err
+ }
+
+ sendLabelUpdatedWebhook(issue, doer)
+ return nil
+}
+
+// RemoveLabel removes a label from issue by given ID.
+func RemoveLabel(issue *models.Issue, doer *models.User, label *models.Label) error {
+ if err := issue.LoadRepo(); err != nil {
+ return err
+ }
+
+ perm, err := models.GetUserRepoPermission(issue.Repo, doer)
+ if err != nil {
+ return err
+ }
+ if !perm.CanWriteIssuesOrPulls(issue.IsPull) {
+ return models.ErrLabelNotExist{}
+ }
+
+ if err := models.DeleteIssueLabel(issue, label, doer); err != nil {
+ return err
+ }
+
+ sendLabelUpdatedWebhook(issue, doer)
+ return nil
+}