diff options
author | Unknwon <u@gogs.io> | 2016-08-03 11:51:22 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-08-03 11:51:22 -0700 |
commit | e7fd65f0cf748201e70f7a8de089275099890f63 (patch) | |
tree | 4158ce20ccc3106ea84656678bf062bd164ad9b3 /models/issue.go | |
parent | 2eeb0ec9b032ea02d315922feb762637ecd9ddc8 (diff) | |
download | gitea-e7fd65f0cf748201e70f7a8de089275099890f63.tar.gz gitea-e7fd65f0cf748201e70f7a8de089275099890f63.zip |
#3290 better code structure and batch minor improvements
Diffstat (limited to 'models/issue.go')
-rw-r--r-- | models/issue.go | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/models/issue.go b/models/issue.go index 9d0f58186e..4a6f9b278a 100644 --- a/models/issue.go +++ b/models/issue.go @@ -181,7 +181,7 @@ func (i *Issue) addLabel(e *xorm.Session, label *Label) error { return newIssueLabel(e, i, label) } -// AddLabel adds new label to issue by given ID. +// AddLabel adds a new label to the issue. func (i *Issue) AddLabel(label *Label) (err error) { sess := x.NewSession() defer sessionRelease(sess) @@ -196,52 +196,76 @@ func (i *Issue) AddLabel(label *Label) (err error) { return sess.Commit() } -func (i *Issue) getLabels(e Engine) (err error) { - if len(i.Labels) > 0 { +func (issue *Issue) addLabels(e *xorm.Session, labels []*Label) error { + return newIssueLabels(e, issue, labels) +} + +// AddLabels adds a list of new labels to the issue. +func (issue *Issue) AddLabels(labels []*Label) error { + return NewIssueLabels(issue, labels) +} + +func (issue *Issue) getLabels(e Engine) (err error) { + if len(issue.Labels) > 0 { return nil } - i.Labels, err = getLabelsByIssueID(e, i.ID) + issue.Labels, err = getLabelsByIssueID(e, issue.ID) if err != nil { return fmt.Errorf("getLabelsByIssueID: %v", err) } return nil } -func (i *Issue) removeLabel(e *xorm.Session, label *Label) error { - return deleteIssueLabel(e, i, label) +func (issue *Issue) removeLabel(e *xorm.Session, label *Label) error { + return deleteIssueLabel(e, issue, label) } // RemoveLabel removes a label from issue by given ID. -func (i *Issue) RemoveLabel(label *Label) (err error) { +func (issue *Issue) RemoveLabel(label *Label) (err error) { + return DeleteIssueLabel(issue, label) +} + +func (issue *Issue) clearLabels(e *xorm.Session) (err error) { + if err = issue.getLabels(e); err != nil { + return fmt.Errorf("getLabels: %v", err) + } + + for i := range issue.Labels { + if err = issue.removeLabel(e, issue.Labels[i]); err != nil { + return fmt.Errorf("removeLabel: %v", err) + } + } + + return nil +} + +func (issue *Issue) ClearLabels() (err error) { sess := x.NewSession() defer sessionRelease(sess) if err = sess.Begin(); err != nil { return err } - if err = i.removeLabel(sess, label); err != nil { + if err = issue.clearLabels(sess); err != nil { return err } return sess.Commit() } -func (i *Issue) ClearLabels() (err error) { +// ReplaceLabels removes all current labels and add new labels to the issue. +func (issue *Issue) ReplaceLabels(labels []*Label) (err error) { sess := x.NewSession() defer sessionRelease(sess) if err = sess.Begin(); err != nil { return err } - if err = i.getLabels(sess); err != nil { - return err - } - - for idx := range i.Labels { - if err = i.removeLabel(sess, i.Labels[idx]); err != nil { - return err - } + if err = issue.clearLabels(sess); err != nil { + return fmt.Errorf("clearLabels: %v", err) + } else if err = issue.addLabels(sess, labels); err != nil { + return fmt.Errorf("addLabels: %v", err) } return sess.Commit() |