|
| 1 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package webhook |
| 6 | + |
| 7 | +import ( |
| 8 | + "code.gitea.io/gitea/models" |
| 9 | + "code.gitea.io/gitea/modules/log" |
| 10 | + "code.gitea.io/gitea/modules/notification/base" |
| 11 | + api "code.gitea.io/gitea/modules/structs" |
| 12 | +) |
| 13 | + |
| 14 | +type webhookNotifier struct { |
| 15 | + base.NullNotifier |
| 16 | +} |
| 17 | + |
| 18 | +var ( |
| 19 | + _ base.Notifier = &webhookNotifier{} |
| 20 | +) |
| 21 | + |
| 22 | +// NewNotifier create a new webhookNotifier notifier |
| 23 | +func NewNotifier() base.Notifier { |
| 24 | + return &webhookNotifier{} |
| 25 | +} |
| 26 | + |
| 27 | +func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *models.Issue) { |
| 28 | + if err := issue.LoadPoster(); err != nil { |
| 29 | + log.Error("loadPoster: %v", err) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + if err := issue.LoadRepo(); err != nil { |
| 34 | + log.Error("LoadRepo: %v", err) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + mode, _ := models.AccessLevel(issue.Poster, issue.Repo) |
| 39 | + var err error |
| 40 | + if issue.IsPull { |
| 41 | + if err = issue.LoadPullRequest(); err != nil { |
| 42 | + log.Error("LoadPullRequest: %v", err) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{ |
| 47 | + Action: api.HookIssueLabelCleared, |
| 48 | + Index: issue.Index, |
| 49 | + PullRequest: issue.PullRequest.APIFormat(), |
| 50 | + Repository: issue.Repo.APIFormat(mode), |
| 51 | + Sender: doer.APIFormat(), |
| 52 | + }) |
| 53 | + } else { |
| 54 | + err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ |
| 55 | + Action: api.HookIssueLabelCleared, |
| 56 | + Index: issue.Index, |
| 57 | + Issue: issue.APIFormat(), |
| 58 | + Repository: issue.Repo.APIFormat(mode), |
| 59 | + Sender: doer.APIFormat(), |
| 60 | + }) |
| 61 | + } |
| 62 | + if err != nil { |
| 63 | + log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err) |
| 64 | + } else { |
| 65 | + go models.HookQueue.Add(issue.RepoID) |
| 66 | + } |
| 67 | +} |
0 commit comments