Skip to content

Commit

Permalink
return IDs instead of full structs
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed Feb 4, 2019
1 parent 0af1d29 commit cc61ce1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
11 changes: 6 additions & 5 deletions models/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,17 @@ func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
return getLabelInRepoByName(x, repoID, labelName)
}

// GetLabelsInRepoByNames returns a list of labels by names in a given
// 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 GetLabelsInRepoByNames(repoID int64, labelNames []string) ([]*Label, error) {
labels := make([]*Label, 0, len(labelNames))
return labels, x.
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").
Find(&labels)
Cols("id").
Find(&labelIDs)
}

// GetLabelInRepoByID returns a label by ID in given repository.
Expand Down
17 changes: 9 additions & 8 deletions models/issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,26 @@ func TestGetLabelInRepoByName(t *testing.T) {

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

assert.Len(t, labels, 2)
assert.Len(t, labelIDs, 2)

assert.Equal(t, "label1", labels[0].Name)
assert.Equal(t, "label2", labels[1].Name)
assert.Equal(t, int64(1), labelIDs[0])
assert.Equal(t, int64(2), labelIDs[1])
}

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

assert.Len(t, labels, 2)
assert.Len(t, labelIDs, 2)

assert.Equal(t, "label1", labels[0].Name)
assert.Equal(t, "label2", labels[1].Name)
assert.Equal(t, int64(1), labelIDs[0])
assert.Equal(t, int64(2), labelIDs[1])
assert.NoError(t, err)
}

func TestGetLabelInRepoByID(t *testing.T) {
Expand Down
14 changes: 5 additions & 9 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,31 @@ func ListIssues(ctx *context.APIContext) {
keyword = ""
}
var issueIDs []int64
var labelsID []int64
var labelIDs []int64
var err error
if len(keyword) > 0 {
issueIDs, err = indexer.SearchIssuesByKeyword(ctx.Repo.Repository.ID, keyword)
}

if splitted := strings.Split(ctx.Query("labels"), ","); len(splitted) > 0 {
labels, err := models.GetLabelsInRepoByNames(ctx.Repo.Repository.ID, splitted)
var err error
labelIDs, err = models.GetLabelIDsInRepoByNames(ctx.Repo.Repository.ID, splitted)
if err != nil {
ctx.Error(500, "GetLabelsInRepoByNames", err)
return
}

labelsID = make([]int64, 0, len(labels))
for i := range labels {
labelsID = append(labelsID, labels[i].ID)
}
}

// Only fetch the issues if we either don't have a keyword or the search returned issues
// This would otherwise return all issues if no issues were found by the search.
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelsID) > 0 {
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
issues, err = models.Issues(&models.IssuesOptions{
RepoIDs: []int64{ctx.Repo.Repository.ID},
Page: ctx.QueryInt("page"),
PageSize: setting.UI.IssuePagingNum,
IsClosed: isClosed,
IssueIDs: issueIDs,
LabelIDs: labelsID,
LabelIDs: labelIDs,
})
}

Expand Down

0 comments on commit cc61ce1

Please sign in to comment.