Skip to content

Commit cc61ce1

Browse files
committed
return IDs instead of full structs
1 parent 0af1d29 commit cc61ce1

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

models/issue_label.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,17 @@ func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
203203
return getLabelInRepoByName(x, repoID, labelName)
204204
}
205205

206-
// GetLabelsInRepoByNames returns a list of labels by names in a given
206+
// GetLabelIDsInRepoByNames returns a list of labelIDs by names in a given
207207
// repository.
208208
// it silently ignores label names that do not belong to the repository.
209-
func GetLabelsInRepoByNames(repoID int64, labelNames []string) ([]*Label, error) {
210-
labels := make([]*Label, 0, len(labelNames))
211-
return labels, x.
209+
func GetLabelIDsInRepoByNames(repoID int64, labelNames []string) ([]int64, error) {
210+
labelIDs := make([]int64, 0, len(labelNames))
211+
return labelIDs, x.Table("label").
212212
Where("repo_id = ?", repoID).
213213
In("name", labelNames).
214214
Asc("name").
215-
Find(&labels)
215+
Cols("id").
216+
Find(&labelIDs)
216217
}
217218

218219
// GetLabelInRepoByID returns a label by ID in given repository.

models/issue_label_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,26 @@ func TestGetLabelInRepoByName(t *testing.T) {
8383

8484
func TestGetLabelInRepoByNames(t *testing.T) {
8585
assert.NoError(t, PrepareTestDatabase())
86-
labels, err := GetLabelsInRepoByNames(1, []string{"label1", "label2"})
86+
labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2"})
8787
assert.NoError(t, err)
8888

89-
assert.Len(t, labels, 2)
89+
assert.Len(t, labelIDs, 2)
9090

91-
assert.Equal(t, "label1", labels[0].Name)
92-
assert.Equal(t, "label2", labels[1].Name)
91+
assert.Equal(t, int64(1), labelIDs[0])
92+
assert.Equal(t, int64(2), labelIDs[1])
9393
}
9494

9595
func TestGetLabelInRepoByNamesDiscardsNonExistentLabels(t *testing.T) {
9696
assert.NoError(t, PrepareTestDatabase())
9797
// label3 doesn't exists.. See labels.yml
98-
labels, err := GetLabelsInRepoByNames(1, []string{"label1", "label2", "label3"})
98+
labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2", "label3"})
9999
assert.NoError(t, err)
100100

101-
assert.Len(t, labels, 2)
101+
assert.Len(t, labelIDs, 2)
102102

103-
assert.Equal(t, "label1", labels[0].Name)
104-
assert.Equal(t, "label2", labels[1].Name)
103+
assert.Equal(t, int64(1), labelIDs[0])
104+
assert.Equal(t, int64(2), labelIDs[1])
105+
assert.NoError(t, err)
105106
}
106107

107108
func TestGetLabelInRepoByID(t *testing.T) {

routers/api/v1/repo/issue.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,35 +75,31 @@ func ListIssues(ctx *context.APIContext) {
7575
keyword = ""
7676
}
7777
var issueIDs []int64
78-
var labelsID []int64
78+
var labelIDs []int64
7979
var err error
8080
if len(keyword) > 0 {
8181
issueIDs, err = indexer.SearchIssuesByKeyword(ctx.Repo.Repository.ID, keyword)
8282
}
8383

8484
if splitted := strings.Split(ctx.Query("labels"), ","); len(splitted) > 0 {
85-
labels, err := models.GetLabelsInRepoByNames(ctx.Repo.Repository.ID, splitted)
85+
var err error
86+
labelIDs, err = models.GetLabelIDsInRepoByNames(ctx.Repo.Repository.ID, splitted)
8687
if err != nil {
8788
ctx.Error(500, "GetLabelsInRepoByNames", err)
8889
return
8990
}
90-
91-
labelsID = make([]int64, 0, len(labels))
92-
for i := range labels {
93-
labelsID = append(labelsID, labels[i].ID)
94-
}
9591
}
9692

9793
// Only fetch the issues if we either don't have a keyword or the search returned issues
9894
// This would otherwise return all issues if no issues were found by the search.
99-
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelsID) > 0 {
95+
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
10096
issues, err = models.Issues(&models.IssuesOptions{
10197
RepoIDs: []int64{ctx.Repo.Repository.ID},
10298
Page: ctx.QueryInt("page"),
10399
PageSize: setting.UI.IssuePagingNum,
104100
IsClosed: isClosed,
105101
IssueIDs: issueIDs,
106-
LabelIDs: labelsID,
102+
LabelIDs: labelIDs,
107103
})
108104
}
109105

0 commit comments

Comments
 (0)