diff --git a/artifact/jfrog/artifactory.go b/artifact/jfrog/artifactory.go index 649dea63..bd8624a9 100644 --- a/artifact/jfrog/artifactory.go +++ b/artifact/jfrog/artifactory.go @@ -35,7 +35,7 @@ import ( "github.com/nabbar/golib/errors" ) -func NewArtifactory(ctx context.Context, Do func(req *http.Request) (*http.Response, error), uri, containName, regexName string, reposPath ...string) (artifact.Client, errors.Error) { +func NewArtifactory(ctx context.Context, Do func(req *http.Request) (*http.Response, error), uri, releaseRegex string, releaseGroup int, reposPath ...string) (artifact.Client, errors.Error) { if u, e := url.Parse(uri); e != nil { return nil, ErrorURLParse.ErrorParent(e) } else { @@ -45,8 +45,8 @@ func NewArtifactory(ctx context.Context, Do func(req *http.Request) (*http.Respo ctx: ctx, endpoint: u, path: reposPath, - name: containName, - regex: regexName, + group: releaseGroup, + regex: releaseRegex, } a.ClientHelper.F = a.ListReleases diff --git a/artifact/jfrog/model.go b/artifact/jfrog/model.go index 850de20b..87fc0c4b 100644 --- a/artifact/jfrog/model.go +++ b/artifact/jfrog/model.go @@ -51,7 +51,7 @@ type artifactoryModel struct { ctx context.Context endpoint *url.URL path []string - name string + group int regex string } @@ -177,8 +177,17 @@ func (a *artifactoryModel) request(uri string, bodyResponse interface{}) liberr. func (a *artifactoryModel) getStorageList() (sto []ResponseStorage, err liberr.Error) { var ( lst = ResponseReposStorage{} + reg = regexp.MustCompile(a.regex) ) + if a.regex == "" { + return nil, ErrorParamsEmpty.ErrorParent(fmt.Errorf("regex is empty: %s", a.regex)) + } + + if a.group < 1 { + return nil, ErrorParamsEmpty.ErrorParent(fmt.Errorf("group extracted from regex is empty: %s - %v", a.regex, a.group)) + } + if err = a.request("", &lst); err != nil { return nil, err } else if len(lst.Children) < 1 { @@ -193,6 +202,14 @@ func (a *artifactoryModel) getStorageList() (sto []ResponseStorage, err liberr.E res = ResponseStorage{} ) + if c.Folder { + continue + } + + if !reg.MatchString(c.Uri) { + continue + } + if err = a.request(c.Uri, &res); err != nil { return nil, err } @@ -219,28 +236,22 @@ func (a *artifactoryModel) releasesAppendNotExist(releases version.Collection, v func (a *artifactoryModel) ListReleases() (releases version.Collection, err liberr.Error) { var ( - r *regexp.Regexp + reg = regexp.MustCompile(a.regex) sto []ResponseStorage ) - if a.regex != "" { - r = regexp.MustCompile(a.regex) - } - if sto, err = a.getStorageList(); err != nil { return nil, err } for _, f := range sto { - if a.name != "" && !strings.Contains(f.Path, a.name) { - continue - } + grp := reg.FindStringSubmatch(f.Path) - if r != nil && !r.MatchString(f.Path) { + if len(grp) < a.group { continue } - if v, e := version.NewVersion(f.Path); e != nil { + if v, e := version.NewVersion(grp[a.group]); e != nil { continue } else if !libart.ValidatePreRelease(v) { continue @@ -254,18 +265,14 @@ func (a *artifactoryModel) ListReleases() (releases version.Collection, err libe func (a *artifactoryModel) getArtifact(containName string, regexName string, release *version.Version) (art *ResponseStorage, err liberr.Error) { var ( - r1 *regexp.Regexp - r2 *regexp.Regexp + reg = regexp.MustCompile(a.regex) + rg2 *regexp.Regexp sto []ResponseStorage ) - if a.regex != "" { - r1 = regexp.MustCompile(a.regex) - } - if regexName != "" { - r2 = regexp.MustCompile(regexName) + rg2 = regexp.MustCompile(regexName) } if sto, err = a.getStorageList(); err != nil { @@ -273,15 +280,13 @@ func (a *artifactoryModel) getArtifact(containName string, regexName string, rel } for _, f := range sto { - if a.name != "" && !strings.Contains(f.Path, a.name) { - continue - } + grp := reg.FindStringSubmatch(f.Path) - if r1 != nil && !r1.MatchString(f.Path) { + if len(grp) < a.group { continue } - if v, e := version.NewVersion(f.Path); e != nil { + if v, e := version.NewVersion(grp[a.group]); e != nil { continue } else if !libart.ValidatePreRelease(v) { continue @@ -289,7 +294,7 @@ func (a *artifactoryModel) getArtifact(containName string, regexName string, rel continue } else if containName != "" && !strings.Contains(f.Path, containName) { continue - } else if r2 != nil && !r2.MatchString(f.Path) { + } else if rg2 != nil && !rg2.MatchString(f.Path) { continue } else { return &f, nil