aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLanre Adelowo <adelowomailbox@gmail.com>2019-02-04 16:20:44 +0100
committertechknowlogick <matti@mdranta.net>2019-02-04 10:20:44 -0500
commit024871ade60c619302430a2852018dcbd1b35b79 (patch)
treed8a78311e8c40b063ef63424786bcbd2e00ef205 /models
parentf21ae12abb2529ea6e8ab113706f11d848c74f65 (diff)
downloadgitea-024871ade60c619302430a2852018dcbd1b35b79.tar.gz
gitea-024871ade60c619302430a2852018dcbd1b35b79.zip
Add label names as filter in issue search api (#5946)
Diffstat (limited to 'models')
-rw-r--r--models/issue_label.go15
-rw-r--r--models/issue_label_test.go24
2 files changed, 38 insertions, 1 deletions
diff --git a/models/issue_label.go b/models/issue_label.go
index 6adb4eedcb..3c593e72f9 100644
--- a/models/issue_label.go
+++ b/models/issue_label.go
@@ -203,13 +203,26 @@ func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
return getLabelInRepoByName(x, repoID, labelName)
}
+// 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)
+}
+
// 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,
-// it silently ignores label IDs that are not belong to the repository.
+// it silently ignores label IDs that do not belong to the repository.
func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs))
return labels, x.
diff --git a/models/issue_label_test.go b/models/issue_label_test.go
index aef3f70c03..d103778df3 100644
--- a/models/issue_label_test.go
+++ b/models/issue_label_test.go
@@ -81,6 +81,30 @@ func TestGetLabelInRepoByName(t *testing.T) {
assert.True(t, IsErrLabelNotExist(err))
}
+func TestGetLabelInRepoByNames(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+ labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2"})
+ assert.NoError(t, err)
+
+ assert.Len(t, labelIDs, 2)
+
+ 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
+ labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2", "label3"})
+ assert.NoError(t, err)
+
+ assert.Len(t, labelIDs, 2)
+
+ assert.Equal(t, int64(1), labelIDs[0])
+ assert.Equal(t, int64(2), labelIDs[1])
+ assert.NoError(t, err)
+}
+
func TestGetLabelInRepoByID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
label, err := GetLabelInRepoByID(1, 1)