1. Add IsSpecificRepositoryName util method. (#7444)

2. Add IsSpecificRepositoryName unit test case.
3. Add Copyright header comment.

Signed-off-by: 慕薇疯魔 <kfanjian@gmail.com>
This commit is contained in:
疯魔慕薇 2019-04-23 13:37:40 +08:00 committed by Wang Yan
parent 1323fbfa14
commit d022ad4cd4
5 changed files with 103 additions and 4 deletions

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package native package native
import ( import (

View File

@ -15,8 +15,6 @@
package native package native
import ( import (
"strings"
adp "github.com/goharbor/harbor/src/replication/adapter" adp "github.com/goharbor/harbor/src/replication/adapter"
"github.com/goharbor/harbor/src/replication/model" "github.com/goharbor/harbor/src/replication/model"
"github.com/goharbor/harbor/src/replication/util" "github.com/goharbor/harbor/src/replication/util"
@ -65,9 +63,9 @@ func (n native) FetchImages(filters []*model.Filter) ([]*model.Resource, error)
} }
func (n native) filterRepositories(pattern string) ([]string, error) { func (n native) filterRepositories(pattern string) ([]string, error) {
// if the pattern contains no "*" and "?", it is a specific repository name // if is a specific repository name
// just to make sure the repository exists // just to make sure the repository exists
if len(pattern) > 0 && !strings.ContainsAny(pattern, "*?") { if len(pattern) > 0 && util.IsSpecificRepositoryName(pattern) {
// check is repository exist later at filterTags. // check is repository exist later at filterTags.
return []string{pattern}, nil return []string{pattern}, nil
} }

View File

@ -1,3 +1,17 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package native package native
import ( import (

View File

@ -30,6 +30,12 @@ func Match(pattern, str string) (bool, error) {
return doublestar.Match(pattern, str) return doublestar.Match(pattern, str)
} }
// IsSpecificRepositoryName if the name not contains any char of "*?[{\\]}^,",
// it is a specific repository name.
func IsSpecificRepositoryName(name string) bool {
return !strings.ContainsAny(name, "*?[{\\]}^,")
}
// GetHTTPTransport can be used to share the common HTTP transport // GetHTTPTransport can be used to share the common HTTP transport
func GetHTTPTransport(insecure bool) *http.Transport { func GetHTTPTransport(insecure bool) *http.Transport {
return registry.GetHTTPTransport(insecure) return registry.GetHTTPTransport(insecure)

View File

@ -104,3 +104,70 @@ func TestParseRepository(t *testing.T) {
assert.Equal(t, "a/b", namespace) assert.Equal(t, "a/b", namespace)
assert.Equal(t, "c", rest) assert.Equal(t, "c", rest)
} }
func TestIsSpecificRepositoryName(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"Is Specific", "a", true},
{"Is Specific", "abc", true},
{"Is Specific", "a/b", true},
{"Is Specific", "a/b/c", true},
{"Not Specific", "*", false},
{"Not Specific", "?", false},
{"Not Specific", "*c", false},
{"Not Specific", "a*", false},
{"Not Specific", "a*b*c*d*e*", false},
{"Not Specific", "a*b?c*x", false},
{"Not Specific", "ab[c]", false},
{"Not Specific", "ab[b-d]", false},
{"Not Specific", "ab[e-g]", false},
{"Not Specific", "ab[^c]", false},
{"Not Specific", "ab[^b-d]", false},
{"Not Specific", "ab[^e-g]", false},
{"Not Specific", "a\\*b", false},
{"Not Specific", "a?b", false},
{"Not Specific", "a[^a]b", false},
{"Not Specific", "a???b", false},
{"Not Specific", "a[^a][^a][^a]b", false},
{"Not Specific", "[a-ζ]*", false},
{"Not Specific", "*[a-ζ]", false},
{"Not Specific", "a?b", false},
{"Not Specific", "a*b", false},
{"Not Specific", "[\\-]", false},
{"Not Specific", "[x\\-]", false},
{"Not Specific", "[x\\-]", false},
{"Not Specific", "[x\\-]", false},
{"Not Specific", "[\\-x]", false},
{"Not Specific", "[\\-x]", false},
{"Not Specific", "[\\-x]", false},
{"Not Specific", "[a-b-c]", false},
{"Not Specific", "*x", false},
{"Not Specific", "[abc]", false},
{"Not Specific", "**", false},
{"Not Specific", "ab{c,d}", false},
{"Not Specific", "ab{c,d,*}", false},
{"Not Specific", "abc**", false},
{"Not Specific", "[]a]", false},
{"Not Specific", "[-]", false},
{"Not Specific", "[x-]", false},
{"Not Specific", "[-x]", false},
{"Not Specific", "\\", false},
{"Not Specific", "[a-b-c]", false},
{"Not Specific", "[]", false},
{"Not Specific", "[", false},
{"Not Specific", "[^", false},
{"Not Specific", "^", false},
{"Not Specific", "]", false},
{"Not Specific", "[^bc", false},
{"Not Specific", "a[", false},
{"Not Specific", "ab{c,d}[", false},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
var got = IsSpecificRepositoryName(tt.input)
assert.Equal(t, tt.want, got)
})
}
}