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_label.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_label.go')
-rw-r--r-- | models/issue_label.go | 93 |
1 files changed, 73 insertions, 20 deletions
diff --git a/models/issue_label.go b/models/issue_label.go index 4f814821f8..ed9b616011 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -11,6 +11,8 @@ import ( "strings" "github.com/go-xorm/xorm" + + "github.com/gogits/gogs/modules/base" ) // Label represents a label of repository for issues. @@ -56,48 +58,65 @@ func NewLabel(l *Label) error { return err } -func getLabelByID(e Engine, id int64) (*Label, error) { - if id <= 0 { - return nil, ErrLabelNotExist{id} +// getLabelInRepoByID returns a label by ID in given repository. +// If pass repoID as 0, then ORM will ignore limitation of repository +// and can return arbitrary label with any valid ID. +func getLabelInRepoByID(e Engine, repoID, labelID int64) (*Label, error) { + if labelID <= 0 { + return nil, ErrLabelNotExist{labelID, repoID} } - l := &Label{ID: id} + l := &Label{ + ID: labelID, + RepoID: repoID, + } has, err := x.Get(l) if err != nil { return nil, err } else if !has { - return nil, ErrLabelNotExist{l.ID} + return nil, ErrLabelNotExist{l.ID, l.RepoID} } return l, nil } // GetLabelByID returns a label by given ID. func GetLabelByID(id int64) (*Label, error) { - return getLabelByID(x, id) + return getLabelInRepoByID(x, 0, id) +} + +// GetLabelInRepoByID returns a label by ID in given repository. +func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) { + return getLabelInRepoByID(x, repoID, labelID) +} + +// GetLabelsInRepoByIDs returns a list of labels by IDs in given repository, +// it silently ignores label IDs that are not belong to the repository. +func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) { + labels := make([]*Label, 0, len(labelIDs)) + return labels, x.Where("repo_id = ?", repoID).In("id", base.Int64sToStrings(labelIDs)).Find(&labels) } // GetLabelsByRepoID returns all labels that belong to given repository by ID. func GetLabelsByRepoID(repoID int64) ([]*Label, error) { labels := make([]*Label, 0, 10) - return labels, x.Where("repo_id=?", repoID).Find(&labels) + return labels, x.Where("repo_id = ?", repoID).Find(&labels) } func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) { issueLabels, err := getIssueLabels(e, issueID) if err != nil { return nil, fmt.Errorf("getIssueLabels: %v", err) + } else if len(issueLabels) == 0 { + return []*Label{}, nil } - var label *Label - labels := make([]*Label, 0, len(issueLabels)) - for idx := range issueLabels { - label, err = getLabelByID(e, issueLabels[idx].LabelID) - if err != nil && !IsErrLabelNotExist(err) { - return nil, fmt.Errorf("getLabelByID: %v", err) - } - labels = append(labels, label) + labelIDs := make([]int64, len(issueLabels)) + for i := range issueLabels { + labelIDs[i] = issueLabels[i].LabelID } - return labels, nil + + labels := make([]*Label, 0, len(labelIDs)) + return labels, e.Where("id > 0").In("id", base.Int64sToStrings(labelIDs)).Find(&labels) } // GetLabelsByIssueID returns all labels that belong to given issue by ID. @@ -117,7 +136,7 @@ func UpdateLabel(l *Label) error { // DeleteLabel delete a label of given repository. func DeleteLabel(repoID, labelID int64) error { - l, err := GetLabelByID(labelID) + _, err := GetLabelInRepoByID(repoID, labelID) if err != nil { if IsErrLabelNotExist(err) { return nil @@ -131,11 +150,12 @@ func DeleteLabel(repoID, labelID int64) error { return err } - if _, err = x.Where("label_id=?", labelID).Delete(new(IssueLabel)); err != nil { + if _, err = sess.Id(labelID).Delete(new(Label)); err != nil { return err - } else if _, err = sess.Delete(l); err != nil { + } else if _, err = sess.Where("label_id = ?", labelID).Delete(new(IssueLabel)); err != nil { return err } + return sess.Commit() } @@ -154,7 +174,7 @@ type IssueLabel struct { } func hasIssueLabel(e Engine, issueID, labelID int64) bool { - has, _ := e.Where("issue_id=? AND label_id=?", issueID, labelID).Get(new(IssueLabel)) + has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel)) return has } @@ -180,6 +200,10 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) { // NewIssueLabel creates a new issue-label relation. func NewIssueLabel(issue *Issue, label *Label) (err error) { + if HasIssueLabel(issue.ID, label.ID) { + return nil + } + sess := x.NewSession() defer sessionRelease(sess) if err = sess.Begin(); err != nil { @@ -193,6 +217,35 @@ func NewIssueLabel(issue *Issue, label *Label) (err error) { return sess.Commit() } +func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label) (err error) { + for i := range labels { + if hasIssueLabel(e, issue.ID, labels[i].ID) { + continue + } + + if err = newIssueLabel(e, issue, labels[i]); err != nil { + return fmt.Errorf("newIssueLabel: %v", err) + } + } + + return nil +} + +// NewIssueLabels creates a list of issue-label relations. +func NewIssueLabels(issue *Issue, labels []*Label) (err error) { + sess := x.NewSession() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { + return err + } + + if err = newIssueLabels(sess, issue, labels); err != nil { + return err + } + + return sess.Commit() +} + func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) { issueLabels := make([]*IssueLabel, 0, 10) return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels) |