Skip to content

Commit 024871a

Browse files
adelowotechknowlogick
authored andcommitted
Add label names as filter in issue search api (#5946)
1 parent f21ae12 commit 024871a

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

models/issue_label.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,26 @@ func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
203203
return getLabelInRepoByName(x, repoID, labelName)
204204
}
205205

206+
// GetLabelIDsInRepoByNames returns a list of labelIDs by names in a given
207+
// repository.
208+
// it silently ignores label names that do not belong to the repository.
209+
func GetLabelIDsInRepoByNames(repoID int64, labelNames []string) ([]int64, error) {
210+
labelIDs := make([]int64, 0, len(labelNames))
211+
return labelIDs, x.Table("label").
212+
Where("repo_id = ?", repoID).
213+
In("name", labelNames).
214+
Asc("name").
215+
Cols("id").
216+
Find(&labelIDs)
217+
}
218+
206219
// GetLabelInRepoByID returns a label by ID in given repository.
207220
func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
208221
return getLabelInRepoByID(x, repoID, labelID)
209222
}
210223

211224
// GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
212-
// it silently ignores label IDs that are not belong to the repository.
225+
// it silently ignores label IDs that do not belong to the repository.
213226
func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
214227
labels := make([]*Label, 0, len(labelIDs))
215228
return labels, x.

models/issue_label_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ func TestGetLabelInRepoByName(t *testing.T) {
8181
assert.True(t, IsErrLabelNotExist(err))
8282
}
8383

84+
func TestGetLabelInRepoByNames(t *testing.T) {
85+
assert.NoError(t, PrepareTestDatabase())
86+
labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2"})
87+
assert.NoError(t, err)
88+
89+
assert.Len(t, labelIDs, 2)
90+
91+
assert.Equal(t, int64(1), labelIDs[0])
92+
assert.Equal(t, int64(2), labelIDs[1])
93+
}
94+
95+
func TestGetLabelInRepoByNamesDiscardsNonExistentLabels(t *testing.T) {
96+
assert.NoError(t, PrepareTestDatabase())
97+
// label3 doesn't exists.. See labels.yml
98+
labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2", "label3"})
99+
assert.NoError(t, err)
100+
101+
assert.Len(t, labelIDs, 2)
102+
103+
assert.Equal(t, int64(1), labelIDs[0])
104+
assert.Equal(t, int64(2), labelIDs[1])
105+
assert.NoError(t, err)
106+
}
107+
84108
func TestGetLabelInRepoByID(t *testing.T) {
85109
assert.NoError(t, PrepareTestDatabase())
86110
label, err := GetLabelInRepoByID(1, 1)

routers/api/v1/repo/issue.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func ListIssues(ctx *context.APIContext) {
4343
// in: query
4444
// description: whether issue is open or closed
4545
// type: string
46+
// - name: labels
47+
// in: query
48+
// description: comma separated list of labels. Fetch only issues that have any of this labels. Non existent labels are discarded
49+
// type: string
4650
// - name: page
4751
// in: query
4852
// description: page number of requested issues
@@ -71,20 +75,30 @@ func ListIssues(ctx *context.APIContext) {
7175
keyword = ""
7276
}
7377
var issueIDs []int64
78+
var labelIDs []int64
7479
var err error
7580
if len(keyword) > 0 {
7681
issueIDs, err = indexer.SearchIssuesByKeyword(ctx.Repo.Repository.ID, keyword)
7782
}
7883

84+
if splitted := strings.Split(ctx.Query("labels"), ","); len(splitted) > 0 {
85+
labelIDs, err = models.GetLabelIDsInRepoByNames(ctx.Repo.Repository.ID, splitted)
86+
if err != nil {
87+
ctx.Error(500, "GetLabelIDsInRepoByNames", err)
88+
return
89+
}
90+
}
91+
7992
// Only fetch the issues if we either don't have a keyword or the search returned issues
8093
// This would otherwise return all issues if no issues were found by the search.
81-
if len(keyword) == 0 || len(issueIDs) > 0 {
94+
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
8295
issues, err = models.Issues(&models.IssuesOptions{
8396
RepoIDs: []int64{ctx.Repo.Repository.ID},
8497
Page: ctx.QueryInt("page"),
8598
PageSize: setting.UI.IssuePagingNum,
8699
IsClosed: isClosed,
87100
IssueIDs: issueIDs,
101+
LabelIDs: labelIDs,
88102
})
89103
}
90104

templates/swagger/v1_json.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,12 @@
20592059
"name": "state",
20602060
"in": "query"
20612061
},
2062+
{
2063+
"type": "string",
2064+
"description": "comma separated list of labels. Fetch only issues that have any of this labels. Non existent labels are discarded",
2065+
"name": "labels",
2066+
"in": "query"
2067+
},
20622068
{
20632069
"type": "integer",
20642070
"description": "page number of requested issues",

0 commit comments

Comments
 (0)