Merge pull request #5070 from ywk253100/180601_label_fuzzy_match

Fix #4742: fuzzy match label name
This commit is contained in:
Daniel Jiang 2018-06-12 14:39:35 +08:00 committed by GitHub
commit 255a6d6f95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 11 deletions

View File

@ -70,8 +70,12 @@ func ListLabels(query *models.LabelQuery) ([]*models.Label, error) {
func getLabelQuerySetter(query *models.LabelQuery) orm.QuerySeter { func getLabelQuerySetter(query *models.LabelQuery) orm.QuerySeter {
qs := GetOrmer().QueryTable(&models.Label{}) qs := GetOrmer().QueryTable(&models.Label{})
if len(query.Name) > 0 { if len(query.Name) > 0 {
if query.FuzzyMatchName {
qs = qs.Filter("Name__icontains", query.Name)
} else {
qs = qs.Filter("Name", query.Name) qs = qs.Filter("Name", query.Name)
} }
}
if len(query.Level) > 0 { if len(query.Level) > 0 {
qs = qs.Filter("Level", query.Level) qs = qs.Filter("Level", query.Level)
} }

View File

@ -72,7 +72,7 @@ func TestMethodsOfLabel(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, int64(1), total) assert.Equal(t, int64(1), total)
// list // list: exact match
labels, err := ListLabels(&models.LabelQuery{ labels, err := ListLabels(&models.LabelQuery{
Scope: common.LabelScopeProject, Scope: common.LabelScopeProject,
ProjectID: 1, ProjectID: 1,
@ -81,11 +81,21 @@ func TestMethodsOfLabel(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, 1, len(labels)) assert.Equal(t, 1, len(labels))
// list // list: fuzzy match
labels, err = ListLabels(&models.LabelQuery{ labels, err = ListLabels(&models.LabelQuery{
Scope: common.LabelScopeProject, Scope: common.LabelScopeProject,
ProjectID: 1, ProjectID: 1,
Name: "not_exist_label", Name: label.Name[:1],
FuzzyMatchName: true,
})
require.Nil(t, err)
assert.Equal(t, 1, len(labels))
// list: not exist
labels, err = ListLabels(&models.LabelQuery{
Scope: common.LabelScopeProject,
ProjectID: 1,
Name: label.Name[:1],
}) })
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, 0, len(labels)) assert.Equal(t, 0, len(labels))

View File

@ -44,6 +44,7 @@ func (l *Label) TableName() string {
// LabelQuery : query parameters for labels // LabelQuery : query parameters for labels
type LabelQuery struct { type LabelQuery struct {
Name string Name string
FuzzyMatchName bool // the property is used to determine the query for lable name is fuzzy matching or exaxt matching
Level string Level string
Scope string Scope string
ProjectID int64 ProjectID int64

View File

@ -163,6 +163,7 @@ func (l *LabelAPI) Get() {
func (l *LabelAPI) List() { func (l *LabelAPI) List() {
query := &models.LabelQuery{ query := &models.LabelQuery{
Name: l.GetString("name"), Name: l.GetString("name"),
FuzzyMatchName: true,
Level: common.LabelLevelUser, Level: common.LabelLevelUser,
} }

View File

@ -243,7 +243,7 @@ func TestLabelAPIList(t *testing.T) {
}{ }{
Scope: "p", Scope: "p",
ProjectID: 1, ProjectID: 1,
Name: "test", Name: "tes",
}, },
}, &labels) }, &labels)
require.Nil(t, err) require.Nil(t, err)