From 14f8eb757b5431a96d13fcdadbcc3e518daa63c7 Mon Sep 17 00:00:00 2001 From: Benji Visser Date: Wed, 28 Jun 2023 22:51:21 -0600 Subject: [PATCH] [improvement] better handling of project name (#67) Signed-off-by: Benji Visser --- cmd/root.go | 6 +++++- internal/config/project_name.go | 14 +++++++++++++- test/integration/match_by_image_test.go | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index bdc5a7bc..f07ce5ce 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -72,6 +72,10 @@ func validateRootArgs(cmd *cobra.Command, args []string) error { return fmt.Errorf("an image/directory argument is required") } + if appConfig.APIKey != "" && appConfig.ProjectName == "" { + return fmt.Errorf("err: couldn't automatically detect a project name. Please set the project name using --project-name flag when using --api-key flag with xeol.io") + } + return cobra.MaximumNArgs(1)(cmd, args) } @@ -114,7 +118,7 @@ func setRootFlags(flags *pflag.FlagSet) { flags.StringP( "project-name", "", "", - "set the name of the project being analyzed for xeol.io.", + "manually set the name of the project being analyzed for xeol.io. If you are running xeol inside a git repository, this will be automatically detected.", ) flags.StringP( diff --git a/internal/config/project_name.go b/internal/config/project_name.go index dd04d600..ad5705f9 100644 --- a/internal/config/project_name.go +++ b/internal/config/project_name.go @@ -29,12 +29,24 @@ func NewProject(repo *git.Repository) *Project { } func (p *Project) GetRemoteURL() string { + // try to get the origin remote origin, err := p.Repo.Remote("origin") + if err == nil { + return origin.Config().URLs[0] + } + + // if origin is not found, get the list of remotes + remotes, err := p.Repo.Remotes() if err != nil { return "" } - return origin.Config().URLs[0] + if len(remotes) == 0 { + return "" + } + + // return the URL of the first remote found + return remotes[0].Config().URLs[0] } func (p *Project) GetDefaultProjectName() string { diff --git a/test/integration/match_by_image_test.go b/test/integration/match_by_image_test.go index 8b216ea5..240a8342 100644 --- a/test/integration/match_by_image_test.go +++ b/test/integration/match_by_image_test.go @@ -220,6 +220,21 @@ func addFedora29Matches(t *testing.T, theResult *match.Matches) { Eol: "2019-11-26", }, }) + theResult.Add(match.Match{ + Package: pkg.Package{ + Name: "python", + ID: "2ba17cf1680ce4f2", + Version: "3.7.2", + Type: syftPkg.BinaryPkg, + Language: "", + PURL: "pkg:generic/python@3.7.2", + }, + Cycle: eol.Cycle{ + ProductName: "Python", + ReleaseCycle: "3.7", + Eol: "2023-06-27", + }, + }) } func TestMatchByImage(t *testing.T) {