From 8282449c65a3d0a79016a3ce9fb8434916f2662b Mon Sep 17 00:00:00 2001 From: arorasoham9 <68672198+arorasoham9@users.noreply.github.com> Date: Fri, 23 Jun 2023 09:24:37 -0400 Subject: [PATCH] moved vcs.go and vcs_test.go to misc (#962) * moved vcs.go and vcs_test.go to misc Signed-off-by: Soham Arora * Fixed problem with helper Signed-off-by: Soham Arora * Fixed problem with helper Signed-off-by: Soham Arora * collector now uses Vcs functions Signed-off-by: Soham Arora * removing misc/vcs for now Signed-off-by: Soham Arora * fix w rebase Signed-off-by: Soham Arora --------- Signed-off-by: Soham Arora --- pkg/assembler/helpers/vcs.go | 2 +- pkg/handler/collector/github/github.go | 45 ++++++++++++-------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/pkg/assembler/helpers/vcs.go b/pkg/assembler/helpers/vcs.go index cc48919fec..5ca0140236 100644 --- a/pkg/assembler/helpers/vcs.go +++ b/pkg/assembler/helpers/vcs.go @@ -58,8 +58,8 @@ func VcsToSrc(vcsUri string) (*model.SourceInputSpec, error) { m.Type = schemeSp[0] } m.Namespace = u.Host - idx := strings.LastIndex(u.Path, "/") + if idx > 0 { m.Name = u.Path[idx+1:] m.Namespace += u.Path[:idx] diff --git a/pkg/handler/collector/github/github.go b/pkg/handler/collector/github/github.go index 822402117b..9872a47403 100644 --- a/pkg/handler/collector/github/github.go +++ b/pkg/handler/collector/github/github.go @@ -24,6 +24,7 @@ import ( "github.com/guacsec/guac/internal/client" "github.com/guacsec/guac/internal/client/githubclient" + "github.com/guacsec/guac/pkg/assembler/helpers" "github.com/guacsec/guac/pkg/collectsub/datasource" "github.com/guacsec/guac/pkg/handler/processor" "github.com/guacsec/guac/pkg/logging" @@ -284,6 +285,12 @@ func ParseGithubReleaseDataSource(source datasource.Source) (*client.Repo, TagOr // URL should be in the form: // +://[/][@][#] func ParseGitDataSource(source datasource.Source) (*client.Repo, TagOrLatest, error) { + //using vcs.go helper functions + var tol TagOrLatest + var r *client.Repo + /* + Lines 294-301 are repetitive, this check can be done in VcsToSrc function + */ u, err := url.Parse(source.Value) if err != nil { return nil, "", err @@ -292,37 +299,25 @@ func ParseGitDataSource(source datasource.Source) (*client.Repo, TagOrLatest, er return nil, "", fmt.Errorf("invalid github host: %v", u.Host) } - // The below split path should look something like: - // [ "orgName" "repoName" ] or - // [ "orgName" "repoName@tag" ] - // [1:] to ignore leading slash path := strings.Split(u.Path, "/")[1:] - if len(path) != 2 { return nil, "", fmt.Errorf("invalid github uri path: %v invalid number of subpaths: %v", u.Path, len(path)) } - - // If the final element has a tag it gets split out like: - // [ "repoName" "tag" ] - final := strings.Split(path[len(path)-1], "@") - if len(final) > 2 { - return nil, "", fmt.Errorf("invalid tag path, only expected one @: %v", u.Path) + m, err := helpers.VcsToSrc(source.Value) + if err != nil { + return nil, "", err } - var tol TagOrLatest - var r *client.Repo - if len(final) == 2 { - tol = final[1] - r = &client.Repo{ - Owner: path[0], - Repo: final[0], - } - } else { + + if m.Tag == nil && m.Commit == nil { tol = Latest - r = &client.Repo{ - Owner: path[0], - Repo: path[1], - } + } else if m.Tag != nil { + tol = *m.Tag + } else if m.Commit != nil { + tol = *m.Commit + } + r = &client.Repo{ + Owner: m.GetNamespace()[strings.Index(m.GetNamespace(), "/")+1:], + Repo: m.GetName(), } - return r, tol, nil }