|
|
@ -11,6 +11,8 @@ import ( |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/go-xorm/xorm" |
|
|
|
"github.com/go-xorm/xorm" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gogits/gogs/modules/base" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Label represents a label of repository for issues.
|
|
|
|
// Label represents a label of repository for issues.
|
|
|
@ -56,48 +58,65 @@ func NewLabel(l *Label) error { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getLabelByID(e Engine, id int64) (*Label, error) { |
|
|
|
// getLabelInRepoByID returns a label by ID in given repository.
|
|
|
|
if id <= 0 { |
|
|
|
// If pass repoID as 0, then ORM will ignore limitation of repository
|
|
|
|
return nil, ErrLabelNotExist{id} |
|
|
|
// 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) |
|
|
|
has, err := x.Get(l) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} else if !has { |
|
|
|
} else if !has { |
|
|
|
return nil, ErrLabelNotExist{l.ID} |
|
|
|
return nil, ErrLabelNotExist{l.ID, l.RepoID} |
|
|
|
} |
|
|
|
} |
|
|
|
return l, nil |
|
|
|
return l, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetLabelByID returns a label by given ID.
|
|
|
|
// GetLabelByID returns a label by given ID.
|
|
|
|
func GetLabelByID(id int64) (*Label, error) { |
|
|
|
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.
|
|
|
|
// GetLabelsByRepoID returns all labels that belong to given repository by ID.
|
|
|
|
func GetLabelsByRepoID(repoID int64) ([]*Label, error) { |
|
|
|
func GetLabelsByRepoID(repoID int64) ([]*Label, error) { |
|
|
|
labels := make([]*Label, 0, 10) |
|
|
|
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) { |
|
|
|
func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) { |
|
|
|
issueLabels, err := getIssueLabels(e, issueID) |
|
|
|
issueLabels, err := getIssueLabels(e, issueID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("getIssueLabels: %v", err) |
|
|
|
return nil, fmt.Errorf("getIssueLabels: %v", err) |
|
|
|
|
|
|
|
} else if len(issueLabels) == 0 { |
|
|
|
|
|
|
|
return []*Label{}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var label *Label |
|
|
|
labelIDs := make([]int64, len(issueLabels)) |
|
|
|
labels := make([]*Label, 0, len(issueLabels)) |
|
|
|
for i := range issueLabels { |
|
|
|
for idx := range issueLabels { |
|
|
|
labelIDs[i] = issueLabels[i].LabelID |
|
|
|
label, err = getLabelByID(e, issueLabels[idx].LabelID) |
|
|
|
|
|
|
|
if err != nil && !IsErrLabelNotExist(err) { |
|
|
|
|
|
|
|
return nil, fmt.Errorf("getLabelByID: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
labels = append(labels, label) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
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.
|
|
|
|
// 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.
|
|
|
|
// DeleteLabel delete a label of given repository.
|
|
|
|
func DeleteLabel(repoID, labelID int64) error { |
|
|
|
func DeleteLabel(repoID, labelID int64) error { |
|
|
|
l, err := GetLabelByID(labelID) |
|
|
|
_, err := GetLabelInRepoByID(repoID, labelID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
if IsErrLabelNotExist(err) { |
|
|
|
if IsErrLabelNotExist(err) { |
|
|
|
return nil |
|
|
|
return nil |
|
|
@ -131,11 +150,12 @@ func DeleteLabel(repoID, labelID int64) error { |
|
|
|
return err |
|
|
|
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 |
|
|
|
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 err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return sess.Commit() |
|
|
|
return sess.Commit() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -154,7 +174,7 @@ type IssueLabel struct { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func hasIssueLabel(e Engine, issueID, labelID int64) bool { |
|
|
|
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 |
|
|
|
return has |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -180,6 +200,10 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) { |
|
|
|
|
|
|
|
|
|
|
|
// NewIssueLabel creates a new issue-label relation.
|
|
|
|
// NewIssueLabel creates a new issue-label relation.
|
|
|
|
func NewIssueLabel(issue *Issue, label *Label) (err error) { |
|
|
|
func NewIssueLabel(issue *Issue, label *Label) (err error) { |
|
|
|
|
|
|
|
if HasIssueLabel(issue.ID, label.ID) { |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sess := x.NewSession() |
|
|
|
sess := x.NewSession() |
|
|
|
defer sessionRelease(sess) |
|
|
|
defer sessionRelease(sess) |
|
|
|
if err = sess.Begin(); err != nil { |
|
|
|
if err = sess.Begin(); err != nil { |
|
|
@ -193,6 +217,35 @@ func NewIssueLabel(issue *Issue, label *Label) (err error) { |
|
|
|
return sess.Commit() |
|
|
|
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) { |
|
|
|
func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) { |
|
|
|
issueLabels := make([]*IssueLabel, 0, 10) |
|
|
|
issueLabels := make([]*IssueLabel, 0, 10) |
|
|
|
return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels) |
|
|
|
return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels) |
|
|
|