Add the ability to use multiple labels as filters(#5786)

This commit is contained in:
Lauris BH 2019-01-23 06:10:38 +02:00 committed by techknowlogick
parent 6a949af8ca
commit 075649572d
9 changed files with 74 additions and 24 deletions

View file

@ -1210,7 +1210,7 @@ type IssuesOptions struct {
PageSize int
IsClosed util.OptionalBool
IsPull util.OptionalBool
Labels string
LabelIDs []int64
SortType string
IssueIDs []int64
}
@ -1289,15 +1289,10 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) error {
sess.And("issue.is_pull=?", false)
}
if len(opts.Labels) > 0 && opts.Labels != "0" {
labelIDs, err := base.StringsToInt64s(strings.Split(opts.Labels, ","))
if err != nil {
return err
}
if len(labelIDs) > 0 {
sess.
Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
In("issue_label.label_id", labelIDs)
if opts.LabelIDs != nil {
for i, labelID := range opts.LabelIDs {
sess.Join("INNER", fmt.Sprintf("issue_label il%d", i),
fmt.Sprintf("issue.id = il%[1]d.issue_id AND il%[1]d.label_id = %[2]d", i, labelID))
}
}
return nil
@ -1475,9 +1470,11 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
labelIDs, err := base.StringsToInt64s(strings.Split(opts.Labels, ","))
if err != nil {
log.Warn("Malformed Labels argument: %s", opts.Labels)
} else if len(labelIDs) > 0 {
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
In("issue_label.label_id", labelIDs)
} else {
for i, labelID := range labelIDs {
sess.Join("INNER", fmt.Sprintf("issue_label il%d", i),
fmt.Sprintf("issue.id = il%[1]d.issue_id AND il%[1]d.label_id = %[2]d", i, labelID))
}
}
}

View file

@ -69,6 +69,8 @@ type Label struct {
NumClosedIssues int
NumOpenIssues int `xorm:"-"`
IsChecked bool `xorm:"-"`
QueryString string
IsSelected bool
}
// APIFormat converts a Label to the api.Label format
@ -85,6 +87,25 @@ func (label *Label) CalOpenIssues() {
label.NumOpenIssues = label.NumIssues - label.NumClosedIssues
}
// 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
} else if s > 0 {
labelQuerySlice = append(labelQuerySlice, strconv.FormatInt(s, 10))
}
}
if !labelSelected {
labelQuerySlice = append(labelQuerySlice, labelID)
}
label.IsSelected = labelSelected
label.QueryString = strings.Join(labelQuerySlice, ",")
}
// ForegroundColor calculates the text color for labels based
// on their background color.
func (label *Label) ForegroundColor() template.CSS {

View file

@ -193,11 +193,19 @@ func TestIssues(t *testing.T) {
},
{
IssuesOptions{
Labels: "1,2",
LabelIDs: []int64{1},
Page: 1,
PageSize: 4,
},
[]int64{5, 2, 1},
[]int64{2, 1},
},
{
IssuesOptions{
LabelIDs: []int64{1, 2},
Page: 1,
PageSize: 4,
},
[]int64{}, // issues with **both** label 1 and 2, none of these issues matches, TODO: add more tests
},
} {
issues, err := Issues(&test.Opts)