diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-10-19 17:48:29 +0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-10-19 12:48:29 +0300 |
commit | 280f4bebbf5883eef94d8a84b21575393ae5e0e1 (patch) | |
tree | 578594a27dbe25185ec5ce70f692e2f392d9b947 /models | |
parent | 240f46a4220d9b8d38d084dca4ff47d442c12f9c (diff) | |
download | gitea-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 'models')
-rw-r--r-- | models/issue.go | 88 | ||||
-rw-r--r-- | models/issue_test.go | 47 |
2 files changed, 0 insertions, 135 deletions
diff --git a/models/issue.go b/models/issue.go index 525152552c..688a412d8c 100644 --- a/models/issue.go +++ b/models/issue.go @@ -428,52 +428,6 @@ func (issue *Issue) HasLabel(labelID int64) bool { return issue.hasLabel(x, labelID) } -func (issue *Issue) sendLabelUpdatedWebhook(doer *User) { - var err error - - if err = issue.loadRepo(x); err != nil { - log.Error("loadRepo: %v", err) - return - } - - if err = issue.loadPoster(x); err != nil { - log.Error("loadPoster: %v", err) - return - } - - mode, _ := AccessLevel(issue.Poster, issue.Repo) - if issue.IsPull { - if err = issue.loadPullRequest(x); err != nil { - log.Error("loadPullRequest: %v", err) - return - } - if err = issue.PullRequest.LoadIssue(); err != nil { - log.Error("LoadIssue: %v", err) - return - } - err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{ - Action: api.HookIssueLabelUpdated, - Index: issue.Index, - PullRequest: issue.PullRequest.APIFormat(), - Repository: issue.Repo.APIFormat(AccessModeNone), - Sender: doer.APIFormat(), - }) - } else { - err = PrepareWebhooks(issue.Repo, 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 HookQueue.Add(issue.RepoID) - } -} - // ReplyReference returns tokenized address to use for email reply headers func (issue *Issue) ReplyReference() string { var path string @@ -490,30 +444,10 @@ func (issue *Issue) addLabel(e *xorm.Session, label *Label, doer *User) error { return newIssueLabel(e, issue, label, doer) } -// AddLabel adds a new label to the issue. -func (issue *Issue) AddLabel(doer *User, label *Label) error { - if err := NewIssueLabel(issue, label, doer); err != nil { - return err - } - - issue.sendLabelUpdatedWebhook(doer) - return nil -} - func (issue *Issue) addLabels(e *xorm.Session, labels []*Label, doer *User) error { return newIssueLabels(e, issue, labels, doer) } -// AddLabels adds a list of new labels to the issue. -func (issue *Issue) AddLabels(doer *User, labels []*Label) error { - if err := NewIssueLabels(issue, labels, doer); err != nil { - return err - } - - issue.sendLabelUpdatedWebhook(doer) - return nil -} - func (issue *Issue) getLabels(e Engine) (err error) { if len(issue.Labels) > 0 { return nil @@ -530,28 +464,6 @@ func (issue *Issue) removeLabel(e *xorm.Session, doer *User, label *Label) error return deleteIssueLabel(e, issue, label, doer) } -// RemoveLabel removes a label from issue by given ID. -func (issue *Issue) RemoveLabel(doer *User, label *Label) error { - if err := issue.loadRepo(x); err != nil { - return err - } - - perm, err := GetUserRepoPermission(issue.Repo, doer) - if err != nil { - return err - } - if !perm.CanWriteIssuesOrPulls(issue.IsPull) { - return ErrLabelNotExist{} - } - - if err := DeleteIssueLabel(issue, label, doer); err != nil { - return err - } - - issue.sendLabelUpdatedWebhook(doer) - return nil -} - func (issue *Issue) clearLabels(e *xorm.Session, doer *User) (err error) { if err = issue.getLabels(e); err != nil { return fmt.Errorf("getLabels: %v", err) diff --git a/models/issue_test.go b/models/issue_test.go index 0be3f68808..7614c2f554 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -84,53 +84,6 @@ func TestGetParticipantsByIssueID(t *testing.T) { checkParticipants(1, []int{5}) } -func TestIssue_AddLabel(t *testing.T) { - var tests = []struct { - issueID int64 - labelID int64 - doerID int64 - }{ - {1, 2, 2}, // non-pull-request, not-already-added label - {1, 1, 2}, // non-pull-request, already-added label - {2, 2, 2}, // pull-request, not-already-added label - {2, 1, 2}, // pull-request, already-added label - } - for _, test := range tests { - assert.NoError(t, PrepareTestDatabase()) - issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue) - label := AssertExistsAndLoadBean(t, &Label{ID: test.labelID}).(*Label) - doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User) - assert.NoError(t, issue.AddLabel(doer, label)) - AssertExistsAndLoadBean(t, &IssueLabel{IssueID: test.issueID, LabelID: test.labelID}) - } -} - -func TestIssue_AddLabels(t *testing.T) { - var tests = []struct { - issueID int64 - labelIDs []int64 - doerID int64 - }{ - {1, []int64{1, 2}, 2}, // non-pull-request - {1, []int64{}, 2}, // non-pull-request, empty - {2, []int64{1, 2}, 2}, // pull-request - {2, []int64{}, 1}, // pull-request, empty - } - for _, test := range tests { - assert.NoError(t, PrepareTestDatabase()) - issue := AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue) - labels := make([]*Label, len(test.labelIDs)) - for i, labelID := range test.labelIDs { - labels[i] = AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) - } - doer := AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User) - assert.NoError(t, issue.AddLabels(doer, labels)) - for _, labelID := range test.labelIDs { - AssertExistsAndLoadBean(t, &IssueLabel{IssueID: test.issueID, LabelID: labelID}) - } - } -} - func TestIssue_ClearLabels(t *testing.T) { var tests = []struct { issueID int64 |