mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-02 22:18:29 +01:00
fix(Registries/Gitlab): Added support for regular expression search, so as not to request all repositories from gitlab remove * at the end and beginning and do a search, also fixed the tag name check
Signed-off-by: lxShaDoWxl <lxshadowxkingxl@gmail.com>
This commit is contained in:
parent
69740fc396
commit
73b69e0fa7
@ -98,31 +98,19 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
var resources []*model.Resource
|
||||
var projects []*Project
|
||||
var err error
|
||||
pattern := ""
|
||||
nameFilter := ""
|
||||
tagFilter := ""
|
||||
for _, filter := range filters {
|
||||
if filter.Type == model.FilterTypeName {
|
||||
pattern = filter.Value.(string)
|
||||
nameFilter = filter.Value.(string)
|
||||
break
|
||||
} else if filter.Type == model.FilterTypeTag {
|
||||
tagFilter = filter.Value.(string)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(pattern) > 0 {
|
||||
substrings := strings.Split(pattern, "/")
|
||||
projectPattern := substrings[1]
|
||||
names, ok := util.IsSpecificPathComponent(projectPattern)
|
||||
if ok {
|
||||
for _, name := range names {
|
||||
var projectsByName, err = a.clientGitlabAPI.getProjectsByName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if projectsByName == nil {
|
||||
continue
|
||||
}
|
||||
projects = append(projects, projectsByName...)
|
||||
}
|
||||
}
|
||||
}
|
||||
projects = a.searchByPattern(nameFilter)
|
||||
if len(projects) == 0 {
|
||||
projects, err = a.clientGitlabAPI.getProjects()
|
||||
if err != nil {
|
||||
@ -131,8 +119,10 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
}
|
||||
var pathPatterns []string
|
||||
|
||||
if paths, ok := util.IsSpecificPath(pattern); ok {
|
||||
if paths, ok := util.IsSpecificPath(nameFilter); ok {
|
||||
pathPatterns = paths
|
||||
} else {
|
||||
pathPatterns = append(pathPatterns, nameFilter)
|
||||
}
|
||||
|
||||
for _, project := range projects {
|
||||
@ -159,8 +149,10 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
}
|
||||
tags := []string{}
|
||||
for _, vTag := range vTags {
|
||||
if !existPatterns(vTag.Path, pathPatterns) {
|
||||
continue
|
||||
if len(tagFilter) > 0 {
|
||||
if ok, _ := util.Match(strings.ToLower(vTag.Name), strings.ToLower(tagFilter)); !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
tags = append(tags, vTag.Name)
|
||||
}
|
||||
@ -184,11 +176,54 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
return resources, nil
|
||||
}
|
||||
|
||||
func (a *adapter) searchByPattern(pattern string) []*Project {
|
||||
var projects []*Project
|
||||
var err error
|
||||
if len(pattern) > 0 {
|
||||
|
||||
names, ok := util.IsSpecificPath(pattern)
|
||||
if ok {
|
||||
for _, name := range names {
|
||||
substrings := strings.Split(name, "/")
|
||||
if len(substrings) != 2 {
|
||||
continue
|
||||
}
|
||||
var projectsByName, err = a.clientGitlabAPI.getProjectsByName(substrings[1])
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if projectsByName == nil {
|
||||
continue
|
||||
}
|
||||
projects = append(projects, projectsByName...)
|
||||
}
|
||||
} else {
|
||||
substrings := strings.Split(pattern, "/")
|
||||
if len(substrings) != 2 {
|
||||
return projects
|
||||
}
|
||||
projectName := substrings[1]
|
||||
if projectName == "*" {
|
||||
return projects
|
||||
}
|
||||
projectName = strings.Trim(projectName, "*")
|
||||
|
||||
if strings.Contains(projectName, "*") {
|
||||
return projects
|
||||
}
|
||||
projects, err = a.clientGitlabAPI.getProjectsByName(projectName)
|
||||
if err != nil {
|
||||
return projects
|
||||
}
|
||||
}
|
||||
}
|
||||
return projects
|
||||
}
|
||||
func existPatterns(path string, patterns []string) bool {
|
||||
correct := false
|
||||
if len(patterns) > 0 {
|
||||
for _, pathPattern := range patterns {
|
||||
if strings.HasPrefix(strings.ToLower(path), strings.ToLower(pathPattern)) {
|
||||
if ok, _ := util.Match(strings.ToLower(pathPattern), strings.ToLower(path)); ok {
|
||||
correct = true
|
||||
break
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import (
|
||||
)
|
||||
|
||||
func TestProjects(t *testing.T) {
|
||||
// chart museum enabled
|
||||
server := test.NewServer(&test.RequestHandlerMapping{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/api/v4/projects",
|
||||
|
Loading…
Reference in New Issue
Block a user