Skip to content

Commit

Permalink
Changed logic search projects in gitlab adapter for 2.7.0 (#18784)
Browse files Browse the repository at this point in the history
* fix(gitlab): change logic search projects usage search_namespaces

Signed-off-by: lxShaDoWxl <[email protected]>

* tests(gitlab): remove old data and actualization test

Signed-off-by: lxShaDoWxl <[email protected]>

* refactor(gitlab): added debug log

Signed-off-by: lxShaDoWxl <[email protected]>
(cherry picked from commit 7328062)

* lint(gitlab): fix import order

Signed-off-by: lxShaDoWxl <[email protected]>

---------

Signed-off-by: lxShaDoWxl <[email protected]>
  • Loading branch information
lxShaDoWxl committed Jun 7, 2023
1 parent 51a4c93 commit 1661406
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 225 deletions.
37 changes: 16 additions & 21 deletions src/pkg/reg/adapter/gitlab/adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitlab

import (
"net/url"
"strings"

"github.com/goharbor/harbor/src/lib/log"
Expand Down Expand Up @@ -113,6 +114,7 @@ func (a *adapter) FetchArtifacts(filters []*model.Filter) ([]*model.Resource, er
} else {
pathPatterns = append(pathPatterns, nameFilter)
}
log.Debugf("Patterns: %v", pathPatterns)

for _, project := range projects {
if !project.RegistryEnabled {
Expand All @@ -129,8 +131,10 @@ func (a *adapter) FetchArtifacts(filters []*model.Filter) ([]*model.Resource, er
}
for _, repository := range repositories {
if !existPatterns(repository.Path, pathPatterns) {
log.Debugf("Skipping repository path=%s and id=%d", repository.Path, repository.ID)
continue
}
log.Debugf("Search tags repository path=%s and id=%d", repository.Path, repository.ID)
vTags, err := a.clientGitlabAPI.getTags(project.ID, repository.ID)
if err != nil {
return nil, err
Expand Down Expand Up @@ -169,21 +173,12 @@ func (a *adapter) FetchArtifacts(filters []*model.Filter) ([]*model.Resource, er

func (a *adapter) getProjectsByPattern(pattern string) ([]*Project, error) {
var projects []*Project
projectset := make(map[string]bool)
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
}
if _, ok := projectset[substrings[1]]; ok {
continue
}
projectset[substrings[1]] = true
var projectsByName, err = a.clientGitlabAPI.getProjectsByName(substrings[1])
var projectsByName, err = a.clientGitlabAPI.getProjectsByName(url.QueryEscape(name))
if err != nil {
return nil, err
}
Expand All @@ -193,20 +188,20 @@ func (a *adapter) getProjectsByPattern(pattern string) ([]*Project, error) {
projects = append(projects, projectsByName...)
}
} else {
substrings := strings.Split(pattern, "/")
if len(substrings) < 2 {
return projects, nil
}
projectName := substrings[1]
if projectName == "*" {
return projects, nil
projectName := ""
for i, substring := range strings.Split(pattern, "/") {
if strings.Contains(substring, "*") {
if i != 0 {
break
}
} else {
projectName += substring + "/"
}
}
projectName = strings.Trim(projectName, "*")

if strings.Contains(projectName, "*") {
if projectName == "" {
return projects, nil
}
projects, err = a.clientGitlabAPI.getProjectsByName(projectName)
projects, err = a.clientGitlabAPI.getProjectsByName(url.QueryEscape(projectName))
if err != nil {
return nil, err
}
Expand Down
10 changes: 2 additions & 8 deletions src/pkg/reg/adapter/gitlab/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,10 @@ func getServer(t *testing.T) *httptest.Server {
w.Header().Set("X-Next-Page", "")

switch search {
case "dev-docker":
case "library/dev-docker", "library", "library/", "dev-docker/", "dev-docker":
mustWriteHTTPResponse(t, w, "testdata/projects/dev-docker.json")
break
case "dev-":
mustWriteHTTPResponse(t, w, "testdata/projects/dev-docker.json")
break
case "-docker":
mustWriteHTTPResponse(t, w, "testdata/projects/-docker.json")
break
case "":
case "", "library/dockers":
mustWriteHTTPResponse(t, w, "testdata/projects/all.json")
break
default:
Expand Down
8 changes: 5 additions & 3 deletions src/pkg/reg/adapter/gitlab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

common_http "github.com/goharbor/harbor/src/common/http"
liberrors "github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/reg/model"
"github.com/goharbor/harbor/src/pkg/reg/util"
)
Expand Down Expand Up @@ -68,7 +69,7 @@ func (c *Client) getProjects() ([]*Project, error) {

func (c *Client) getProjectsByName(name string) ([]*Project, error) {
var projects []*Project
urlAPI := fmt.Sprintf("%s/api/v4/projects?search=%s&membership=1&per_page=50", c.url, name)
urlAPI := fmt.Sprintf("%s/api/v4/projects?search=%s&membership=true&search_namespaces=true&per_page=50", c.url, name)
if err := c.GetAndIteratePagination(urlAPI, &projects); err != nil {
return nil, err
}
Expand All @@ -80,6 +81,7 @@ func (c *Client) getRepositories(projectID int64) ([]*Repository, error) {
if err := c.GetAndIteratePagination(urlAPI, &repositories); err != nil {
return nil, err
}
log.Debugf("Count repositories %d in project %d", len(repositories), projectID)
return repositories, nil
}

Expand All @@ -89,6 +91,7 @@ func (c *Client) getTags(projectID int64, repositoryID int64) ([]*Tag, error) {
if err := c.GetAndIteratePagination(urlAPI, &tags); err != nil {
return nil, err
}
log.Debugf("Count tags %d in repository %d, and project %d", len(tags), repositoryID, projectID)
return tags, nil
}

Expand All @@ -99,7 +102,6 @@ func (c *Client) GetAndIteratePagination(endpoint string, v interface{}) error {
if err != nil {
return err
}

rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
return errors.New("v should be a pointer to a slice")
Expand All @@ -108,7 +110,7 @@ func (c *Client) GetAndIteratePagination(endpoint string, v interface{}) error {
if elemType.Kind() != reflect.Slice {
return errors.New("v should be a pointer to a slice")
}

log.Debugf("Gitlab request %s", urlAPI)
resources := reflect.Indirect(reflect.New(elemType))
for len(endpoint) > 0 {
req, err := c.newRequest(http.MethodGet, endpoint, nil)
Expand Down
97 changes: 0 additions & 97 deletions src/pkg/reg/adapter/gitlab/testdata/projects/-docker.json

This file was deleted.

96 changes: 0 additions & 96 deletions src/pkg/reg/adapter/gitlab/testdata/projects/dev-.json

This file was deleted.

0 comments on commit 1661406

Please sign in to comment.