2016-02-27 19:31:24 +03:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2020-01-24 22:00:29 +03:00
|
|
|
// Copyright 2020 The Gitea Authors.
|
2016-02-27 19:31:24 +03:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2016-08-30 06:00:06 +03:00
|
|
|
"regexp"
|
2016-02-27 19:31:24 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-09-24 16:22:39 +03:00
|
|
|
"xorm.io/builder"
|
2019-10-17 12:26:49 +03:00
|
|
|
"xorm.io/xorm"
|
2016-02-27 19:31:24 +03:00
|
|
|
)
|
|
|
|
|
2020-02-09 17:33:03 +03:00
|
|
|
// LabelColorPattern is a regexp witch can validate LabelColor
|
|
|
|
var LabelColorPattern = regexp.MustCompile("^#[0-9a-fA-F]{6}$")
|
|
|
|
|
|
|
|
// Label represents a label of repository for issues.
|
|
|
|
type Label struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"INDEX"`
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Color string `xorm:"VARCHAR(7)"`
|
|
|
|
NumIssues int
|
|
|
|
NumClosedIssues int
|
|
|
|
NumOpenIssues int `xorm:"-"`
|
|
|
|
IsChecked bool `xorm:"-"`
|
|
|
|
QueryString string `xorm:"-"`
|
|
|
|
IsSelected bool `xorm:"-"`
|
|
|
|
IsExcluded bool `xorm:"-"`
|
|
|
|
}
|
|
|
|
|
2016-08-30 06:00:06 +03:00
|
|
|
// GetLabelTemplateFile loads the label template file by given name,
|
2018-03-13 05:03:55 +03:00
|
|
|
// then parses and returns a list of name-color pairs and optionally description.
|
|
|
|
func GetLabelTemplateFile(name string) ([][3]string, error) {
|
2020-01-12 15:11:17 +03:00
|
|
|
data, err := GetRepoInitFile("label", name)
|
2016-08-30 06:00:06 +03:00
|
|
|
if err != nil {
|
2020-01-12 15:11:17 +03:00
|
|
|
return nil, fmt.Errorf("GetRepoInitFile: %v", err)
|
2016-08-30 06:00:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(data), "\n")
|
2018-03-13 05:03:55 +03:00
|
|
|
list := make([][3]string, 0, len(lines))
|
2016-08-30 06:00:06 +03:00
|
|
|
for i := 0; i < len(lines); i++ {
|
|
|
|
line := strings.TrimSpace(lines[i])
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-03-13 05:03:55 +03:00
|
|
|
parts := strings.SplitN(line, ";", 2)
|
|
|
|
|
|
|
|
fields := strings.SplitN(parts[0], " ", 2)
|
2016-08-30 06:00:06 +03:00
|
|
|
if len(fields) != 2 {
|
|
|
|
return nil, fmt.Errorf("line is malformed: %s", line)
|
|
|
|
}
|
|
|
|
|
2020-02-09 17:33:03 +03:00
|
|
|
color := strings.Trim(fields[0], " ")
|
|
|
|
if len(color) == 6 {
|
|
|
|
color = "#" + color
|
|
|
|
}
|
|
|
|
if !LabelColorPattern.MatchString(color) {
|
2016-08-30 06:00:06 +03:00
|
|
|
return nil, fmt.Errorf("bad HTML color code in line: %s", line)
|
|
|
|
}
|
|
|
|
|
2018-03-13 05:03:55 +03:00
|
|
|
var description string
|
|
|
|
|
|
|
|
if len(parts) > 1 {
|
|
|
|
description = strings.TrimSpace(parts[1])
|
|
|
|
}
|
|
|
|
|
2016-08-30 06:00:06 +03:00
|
|
|
fields[1] = strings.TrimSpace(fields[1])
|
2020-02-09 17:33:03 +03:00
|
|
|
list = append(list, [3]string{fields[1], color, description})
|
2016-08-30 06:00:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 19:31:24 +03:00
|
|
|
// CalOpenIssues calculates the open issues of label.
|
2016-08-14 13:32:24 +03:00
|
|
|
func (label *Label) CalOpenIssues() {
|
|
|
|
label.NumOpenIssues = label.NumIssues - label.NumClosedIssues
|
2016-02-27 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 07:10:38 +03:00
|
|
|
// LoadSelectedLabelsAfterClick calculates the set of selected labels when a label is clicked
|
|
|
|
func (label *Label) LoadSelectedLabelsAfterClick(currentSelectedLabels []int64) {
|
|
|
|
var labelQuerySlice []string
|
|
|
|
labelSelected := false
|
|
|
|
labelID := strconv.FormatInt(label.ID, 10)
|
|
|
|
for _, s := range currentSelectedLabels {
|
|
|
|
if s == label.ID {
|
|
|
|
labelSelected = true
|
2019-10-23 19:29:14 +03:00
|
|
|
} else if -s == label.ID {
|
|
|
|
labelSelected = true
|
|
|
|
label.IsExcluded = true
|
|
|
|
} else if s != 0 {
|
2019-01-23 07:10:38 +03:00
|
|
|
labelQuerySlice = append(labelQuerySlice, strconv.FormatInt(s, 10))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !labelSelected {
|
|
|
|
labelQuerySlice = append(labelQuerySlice, labelID)
|
|
|
|
}
|
|
|
|
label.IsSelected = labelSelected
|
|
|
|
label.QueryString = strings.Join(labelQuerySlice, ",")
|
|
|
|
}
|
|
|
|
|
2016-02-27 19:31:24 +03:00
|
|
|
// ForegroundColor calculates the text color for labels based
|
|
|
|
// on their background color.
|
2016-11-25 11:11:12 +03:00
|
|
|
func (label *Label) ForegroundColor() template.CSS {
|
|
|
|
if strings.HasPrefix(label.Color, "#") {
|
|
|
|
if color, err := strconv.ParseUint(label.Color[1:], 16, 64); err == nil {
|
2016-02-27 19:31:24 +03:00
|
|
|
r := float32(0xFF & (color >> 16))
|
|
|
|
g := float32(0xFF & (color >> 8))
|
|
|
|
b := float32(0xFF & color)
|
|
|
|
luminance := (0.2126*r + 0.7152*g + 0.0722*b) / 255
|
|
|
|
|
2016-08-27 22:44:39 +03:00
|
|
|
if luminance < 0.66 {
|
2016-02-27 19:31:24 +03:00
|
|
|
return template.CSS("#fff")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// default to black
|
|
|
|
return template.CSS("#000")
|
|
|
|
}
|
|
|
|
|
2019-12-07 05:13:19 +03:00
|
|
|
func loadLabels(labelTemplate string) ([]string, error) {
|
|
|
|
list, err := GetLabelTemplateFile(labelTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrIssueLabelTemplateLoad{labelTemplate, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := make([]string, len(list))
|
|
|
|
for i := 0; i < len(list); i++ {
|
|
|
|
labels[i] = list[i][0]
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadLabelsFormatted loads the labels' list of a template file as a string separated by comma
|
|
|
|
func LoadLabelsFormatted(labelTemplate string) (string, error) {
|
|
|
|
labels, err := loadLabels(labelTemplate)
|
|
|
|
return strings.Join(labels, ", "), err
|
|
|
|
}
|
|
|
|
|
2020-02-09 17:33:03 +03:00
|
|
|
func initializeLabels(e Engine, repoID int64, labelTemplate string) error {
|
2019-09-08 11:28:40 +03:00
|
|
|
list, err := GetLabelTemplateFile(labelTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return ErrIssueLabelTemplateLoad{labelTemplate, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := make([]*Label, len(list))
|
|
|
|
for i := 0; i < len(list); i++ {
|
|
|
|
labels[i] = &Label{
|
|
|
|
RepoID: repoID,
|
|
|
|
Name: list[i][0],
|
|
|
|
Description: list[i][2],
|
|
|
|
Color: list[i][1],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, label := range labels {
|
|
|
|
if err = newLabel(e, label); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 17:33:03 +03:00
|
|
|
// InitializeLabels adds a label set to a repository using a template
|
|
|
|
func InitializeLabels(ctx DBContext, repoID int64, labelTemplate string) error {
|
|
|
|
return initializeLabels(ctx.e, repoID, labelTemplate)
|
2019-09-08 11:28:40 +03:00
|
|
|
}
|
|
|
|
|
2017-06-25 09:15:09 +03:00
|
|
|
func newLabel(e Engine, label *Label) error {
|
|
|
|
_, err := e.Insert(label)
|
2016-02-27 19:31:24 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-25 09:15:09 +03:00
|
|
|
// NewLabel creates a new label for a repository
|
|
|
|
func NewLabel(label *Label) error {
|
2020-02-09 17:33:03 +03:00
|
|
|
if !LabelColorPattern.MatchString(label.Color) {
|
|
|
|
return fmt.Errorf("bad color code: %s", label.Color)
|
|
|
|
}
|
2017-06-25 09:15:09 +03:00
|
|
|
return newLabel(x, label)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLabels creates new labels for a repository.
|
|
|
|
func NewLabels(labels ...*Label) error {
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, label := range labels {
|
2020-02-09 17:33:03 +03:00
|
|
|
if !LabelColorPattern.MatchString(label.Color) {
|
|
|
|
return fmt.Errorf("bad color code: %s", label.Color)
|
|
|
|
}
|
2017-06-25 09:15:09 +03:00
|
|
|
if err := newLabel(sess, label); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
2016-10-07 20:17:27 +03:00
|
|
|
// getLabelInRepoByName returns a label by Name 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 getLabelInRepoByName(e Engine, repoID int64, labelName string) (*Label, error) {
|
2018-10-21 00:25:14 +03:00
|
|
|
if len(labelName) == 0 {
|
2016-10-07 20:17:27 +03:00
|
|
|
return nil, ErrLabelNotExist{0, repoID}
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &Label{
|
|
|
|
Name: labelName,
|
|
|
|
RepoID: repoID,
|
|
|
|
}
|
2017-02-01 04:31:35 +03:00
|
|
|
has, err := e.Get(l)
|
2016-10-07 20:17:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrLabelNotExist{0, l.RepoID}
|
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2016-08-03 21:51:22 +03:00
|
|
|
// 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}
|
2016-02-27 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 21:51:22 +03:00
|
|
|
l := &Label{
|
|
|
|
ID: labelID,
|
|
|
|
RepoID: repoID,
|
|
|
|
}
|
2017-02-01 04:31:35 +03:00
|
|
|
has, err := e.Get(l)
|
2016-02-27 19:31:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
2016-08-03 21:51:22 +03:00
|
|
|
return nil, ErrLabelNotExist{l.ID, l.RepoID}
|
2016-02-27 19:31:24 +03:00
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabelByID returns a label by given ID.
|
|
|
|
func GetLabelByID(id int64) (*Label, error) {
|
2016-08-03 21:51:22 +03:00
|
|
|
return getLabelInRepoByID(x, 0, id)
|
|
|
|
}
|
|
|
|
|
2016-12-02 11:28:45 +03:00
|
|
|
// GetLabelInRepoByName returns a label by name in given repository.
|
2016-10-07 20:17:27 +03:00
|
|
|
func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
|
|
|
|
return getLabelInRepoByName(x, repoID, labelName)
|
|
|
|
}
|
|
|
|
|
2019-02-04 18:20:44 +03:00
|
|
|
// GetLabelIDsInRepoByNames returns a list of labelIDs by names in a given
|
|
|
|
// repository.
|
|
|
|
// it silently ignores label names that do not belong to the repository.
|
|
|
|
func GetLabelIDsInRepoByNames(repoID int64, labelNames []string) ([]int64, error) {
|
|
|
|
labelIDs := make([]int64, 0, len(labelNames))
|
|
|
|
return labelIDs, x.Table("label").
|
|
|
|
Where("repo_id = ?", repoID).
|
|
|
|
In("name", labelNames).
|
|
|
|
Asc("name").
|
|
|
|
Cols("id").
|
|
|
|
Find(&labelIDs)
|
|
|
|
}
|
|
|
|
|
2020-03-30 08:30:39 +03:00
|
|
|
// BuildLabelNamesIssueIDsCondition returns a builder where get issue ids match label names
|
|
|
|
func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
|
|
|
|
return builder.Select("issue_label.issue_id").
|
|
|
|
From("issue_label").
|
|
|
|
InnerJoin("label", "label.id = issue_label.label_id").
|
|
|
|
Where(
|
|
|
|
builder.In("label.name", labelNames),
|
|
|
|
).
|
|
|
|
GroupBy("issue_label.issue_id")
|
|
|
|
}
|
|
|
|
|
Allow cross-repository dependencies on issues (#7901)
* in progress changes for #7405, added ability to add cross-repo dependencies
* removed unused repolink var
* fixed query that was breaking ci tests; fixed check in issue dependency add so that the id of the issue and dependency is checked rather than the indexes
* reverted removal of string in local files becasue these are done via crowdin, not updated manually
* removed 'Select("issue.*")' from getBlockedByDependencies and getBlockingDependencies based on comments in PR review
* changed getBlockedByDependencies and getBlockingDependencies to use a more xorm-like query, also updated the sidebar as a result
* simplified the getBlockingDependencies and getBlockedByDependencies methods; changed the sidebar to show the dependencies in a different format where you can see the name of the repository
* made some changes to the issue view in the dependencies (issue name on top, repo full name on separate line). Change view of issue in the dependency search results (also showing the full repo name on separate line)
* replace call to FindUserAccessibleRepoIDs with SearchRepositoryByName. The former was hardcoded to use isPrivate = false on the repo search, but this code needed it to be true. The SearchRepositoryByName method is used more in the code including on the user's dashboard
* some more tweaks to the layout of the issues when showing dependencies and in the search box when you add new dependencies
* added Name to the RepositoryMeta struct
* updated swagger doc
* fixed total count for link header on SearchIssues
* fixed indentation
* fixed aligment of remove icon on dependencies in issue sidebar
* removed unnecessary nil check (unnecessary because issue.loadRepo is called prior to this block)
* reverting .css change, somehow missed or forgot that less is used
* updated less file and generated css; updated sidebar template with styles to line up delete and issue index
* added ordering to the blocked by/depends on queries
* fixed sorting in issue dependency search and the depends on/blocks views to show issues from the current repo first, then by created date descending; added a "all cross repository dependencies" setting to allow this feature to be turned off, if turned off, the issue dependency search will work the way it did before (restricted to the current repository)
* re-applied my swagger changes after merge
* fixed split string condition in issue search
* changed ALLOW_CROSS_REPOSITORY_DEPENDENCIES description to sound more global than just the issue dependency search; returning 400 in the cross repo issue search api method if not enabled; fixed bug where the issue count did not respect the state parameter
* when adding a dependency to an issue, added a check to make sure the issue and dependency are in the same repo if cross repo dependencies is not enabled
* updated sortIssuesSession call in PullRequests, another commit moved this method from pull.go to pull_list.go so I had to re-apply my change here
* fixed incorrect setting of user id parameter in search repos call
2019-10-31 08:06:10 +03:00
|
|
|
// GetLabelIDsInReposByNames returns a list of labelIDs by names in one of the given
|
|
|
|
// repositories.
|
|
|
|
// it silently ignores label names that do not belong to the repository.
|
|
|
|
func GetLabelIDsInReposByNames(repoIDs []int64, labelNames []string) ([]int64, error) {
|
|
|
|
labelIDs := make([]int64, 0, len(labelNames))
|
|
|
|
return labelIDs, x.Table("label").
|
|
|
|
In("repo_id", repoIDs).
|
|
|
|
In("name", labelNames).
|
|
|
|
Asc("name").
|
|
|
|
Cols("id").
|
|
|
|
Find(&labelIDs)
|
|
|
|
}
|
|
|
|
|
2016-08-03 21:51:22 +03:00
|
|
|
// 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,
|
2019-02-04 18:20:44 +03:00
|
|
|
// it silently ignores label IDs that do not belong to the repository.
|
2016-08-03 21:51:22 +03:00
|
|
|
func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
|
|
|
|
labels := make([]*Label, 0, len(labelIDs))
|
2016-11-10 18:16:32 +03:00
|
|
|
return labels, x.
|
|
|
|
Where("repo_id = ?", repoID).
|
2016-11-12 11:29:18 +03:00
|
|
|
In("id", labelIDs).
|
2016-11-10 18:16:32 +03:00
|
|
|
Asc("name").
|
|
|
|
Find(&labels)
|
2016-02-27 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
func getLabelsByRepoID(e Engine, repoID int64, sortType string, listOptions ListOptions) ([]*Label, error) {
|
2016-02-27 19:31:24 +03:00
|
|
|
labels := make([]*Label, 0, 10)
|
2019-11-25 08:17:51 +03:00
|
|
|
sess := e.Where("repo_id = ?", repoID)
|
2016-12-24 17:41:09 +03:00
|
|
|
|
|
|
|
switch sortType {
|
|
|
|
case "reversealphabetically":
|
|
|
|
sess.Desc("name")
|
|
|
|
case "leastissues":
|
|
|
|
sess.Asc("num_issues")
|
|
|
|
case "mostissues":
|
|
|
|
sess.Desc("num_issues")
|
|
|
|
default:
|
|
|
|
sess.Asc("name")
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:00:29 +03:00
|
|
|
if listOptions.Page != 0 {
|
|
|
|
sess = listOptions.setSessionPagination(sess)
|
|
|
|
}
|
|
|
|
|
2016-12-24 17:41:09 +03:00
|
|
|
return labels, sess.Find(&labels)
|
2016-02-27 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
2019-11-25 08:17:51 +03:00
|
|
|
// GetLabelsByRepoID returns all labels that belong to given repository by ID.
|
2020-01-24 22:00:29 +03:00
|
|
|
func GetLabelsByRepoID(repoID int64, sortType string, listOptions ListOptions) ([]*Label, error) {
|
|
|
|
return getLabelsByRepoID(x, repoID, sortType, listOptions)
|
2019-11-25 08:17:51 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 19:31:24 +03:00
|
|
|
func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
|
2017-02-11 15:01:33 +03:00
|
|
|
var labels []*Label
|
|
|
|
return labels, e.Where("issue_label.issue_id = ?", issueID).
|
|
|
|
Join("LEFT", "issue_label", "issue_label.label_id = label.id").
|
|
|
|
Asc("label.name").
|
2016-11-10 18:16:32 +03:00
|
|
|
Find(&labels)
|
2016-02-27 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabelsByIssueID returns all labels that belong to given issue by ID.
|
|
|
|
func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
|
|
|
|
return getLabelsByIssueID(x, issueID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateLabel(e Engine, l *Label) error {
|
2019-09-24 16:22:39 +03:00
|
|
|
_, err := e.ID(l.ID).
|
|
|
|
SetExpr("num_issues",
|
|
|
|
builder.Select("count(*)").From("issue_label").
|
|
|
|
Where(builder.Eq{"label_id": l.ID}),
|
|
|
|
).
|
|
|
|
SetExpr("num_closed_issues",
|
|
|
|
builder.Select("count(*)").From("issue_label").
|
|
|
|
InnerJoin("issue", "issue_label.issue_id = issue.id").
|
|
|
|
Where(builder.Eq{
|
|
|
|
"issue_label.label_id": l.ID,
|
|
|
|
"issue.is_closed": true,
|
|
|
|
}),
|
|
|
|
).
|
|
|
|
AllCols().Update(l)
|
2016-02-27 19:31:24 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateLabel updates label information.
|
|
|
|
func UpdateLabel(l *Label) error {
|
2020-02-09 17:33:03 +03:00
|
|
|
if !LabelColorPattern.MatchString(l.Color) {
|
|
|
|
return fmt.Errorf("bad color code: %s", l.Color)
|
|
|
|
}
|
2016-02-27 19:31:24 +03:00
|
|
|
return updateLabel(x, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLabel delete a label of given repository.
|
|
|
|
func DeleteLabel(repoID, labelID int64) error {
|
2016-08-03 21:51:22 +03:00
|
|
|
_, err := GetLabelInRepoByID(repoID, labelID)
|
2016-02-27 19:31:24 +03:00
|
|
|
if err != nil {
|
|
|
|
if IsErrLabelNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sess := x.NewSession()
|
2017-06-21 03:57:05 +03:00
|
|
|
defer sess.Close()
|
2016-02-27 19:31:24 +03:00
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-05 07:43:04 +03:00
|
|
|
if _, err = sess.ID(labelID).Delete(new(Label)); err != nil {
|
2016-02-27 19:31:24 +03:00
|
|
|
return err
|
2016-11-10 18:16:32 +03:00
|
|
|
} else if _, err = sess.
|
|
|
|
Where("label_id = ?", labelID).
|
|
|
|
Delete(new(IssueLabel)); err != nil {
|
2016-02-27 19:31:24 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-08-03 21:51:22 +03:00
|
|
|
|
2017-02-11 15:56:57 +03:00
|
|
|
// Clear label id in comment table
|
|
|
|
if _, err = sess.Where("label_id = ?", labelID).Cols("label_id").Update(&Comment{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-27 19:31:24 +03:00
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// .___ .____ ___. .__
|
|
|
|
// | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
|
|
|
|
// | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
|
|
|
|
// | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
|
|
|
|
// |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
|
|
|
|
// \/ \/ \/ \/ \/ \/ \/
|
|
|
|
|
2017-01-05 03:50:34 +03:00
|
|
|
// IssueLabel represents an issue-label relation.
|
2016-02-27 19:31:24 +03:00
|
|
|
type IssueLabel struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
IssueID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
LabelID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasIssueLabel(e Engine, issueID, labelID int64) bool {
|
2016-08-03 21:51:22 +03:00
|
|
|
has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
|
2016-02-27 19:31:24 +03:00
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasIssueLabel returns true if issue has been labeled.
|
|
|
|
func HasIssueLabel(issueID, labelID int64) bool {
|
|
|
|
return hasIssueLabel(x, issueID, labelID)
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:46:45 +03:00
|
|
|
func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
|
2016-02-27 19:31:24 +03:00
|
|
|
if _, err = e.Insert(&IssueLabel{
|
|
|
|
IssueID: issue.ID,
|
|
|
|
LabelID: label.ID,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:46:45 +03:00
|
|
|
if err = issue.loadRepo(e); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-01 05:44:39 +03:00
|
|
|
var opts = &CreateCommentOptions{
|
2019-11-15 21:18:09 +03:00
|
|
|
Type: CommentTypeLabel,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Issue: issue,
|
|
|
|
Label: label,
|
|
|
|
Content: "1",
|
2019-12-01 05:44:39 +03:00
|
|
|
}
|
2019-12-16 06:54:24 +03:00
|
|
|
if _, err = createComment(e, opts); err != nil {
|
2017-01-30 15:46:45 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-27 19:31:24 +03:00
|
|
|
return updateLabel(e, label)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIssueLabel creates a new issue-label relation.
|
2017-01-30 15:46:45 +03:00
|
|
|
func NewIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
|
2016-08-03 21:51:22 +03:00
|
|
|
if HasIssueLabel(issue.ID, label.ID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 19:31:24 +03:00
|
|
|
sess := x.NewSession()
|
2017-06-21 03:57:05 +03:00
|
|
|
defer sess.Close()
|
2016-02-27 19:31:24 +03:00
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:46:45 +03:00
|
|
|
if err = newIssueLabel(sess, issue, label, doer); err != nil {
|
2016-02-27 19:31:24 +03:00
|
|
|
return err
|
2016-08-03 21:51:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:46:45 +03:00
|
|
|
func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label, doer *User) (err error) {
|
2016-08-03 21:51:22 +03:00
|
|
|
for i := range labels {
|
|
|
|
if hasIssueLabel(e, issue.ID, labels[i].ID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:46:45 +03:00
|
|
|
if err = newIssueLabel(e, issue, labels[i], doer); err != nil {
|
2016-08-03 21:51:22 +03:00
|
|
|
return fmt.Errorf("newIssueLabel: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIssueLabels creates a list of issue-label relations.
|
2017-01-30 15:46:45 +03:00
|
|
|
func NewIssueLabels(issue *Issue, labels []*Label, doer *User) (err error) {
|
2016-08-03 21:51:22 +03:00
|
|
|
sess := x.NewSession()
|
2017-06-21 03:57:05 +03:00
|
|
|
defer sess.Close()
|
2016-08-03 21:51:22 +03:00
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:46:45 +03:00
|
|
|
if err = newIssueLabels(sess, issue, labels, doer); err != nil {
|
2016-08-03 21:51:22 +03:00
|
|
|
return err
|
2016-02-27 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
2017-02-01 04:31:35 +03:00
|
|
|
func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
|
|
|
|
if count, err := e.Delete(&IssueLabel{
|
2016-02-27 19:31:24 +03:00
|
|
|
IssueID: issue.ID,
|
|
|
|
LabelID: label.ID,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
2017-02-01 04:31:35 +03:00
|
|
|
} else if count == 0 {
|
|
|
|
return nil
|
2016-02-27 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-30 15:46:45 +03:00
|
|
|
if err = issue.loadRepo(e); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-01 05:44:39 +03:00
|
|
|
var opts = &CreateCommentOptions{
|
2019-11-15 21:18:09 +03:00
|
|
|
Type: CommentTypeLabel,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Issue: issue,
|
|
|
|
Label: label,
|
2019-12-01 05:44:39 +03:00
|
|
|
}
|
2019-12-16 06:54:24 +03:00
|
|
|
if _, err = createComment(e, opts); err != nil {
|
2017-01-30 15:46:45 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-27 19:31:24 +03:00
|
|
|
return updateLabel(e, label)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteIssueLabel deletes issue-label relation.
|
2017-02-01 04:31:35 +03:00
|
|
|
func DeleteIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
|
2016-02-27 19:31:24 +03:00
|
|
|
sess := x.NewSession()
|
2017-06-21 03:57:05 +03:00
|
|
|
defer sess.Close()
|
2016-02-27 19:31:24 +03:00
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-01 04:31:35 +03:00
|
|
|
if err = deleteIssueLabel(sess, issue, label, doer); err != nil {
|
2016-02-27 19:31:24 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|