From 3fda9d38f63a9a2195f4b4148b2c40892b09cd59 Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 15 Jul 2024 15:32:15 +0300 Subject: [PATCH 01/10] Fix missing root module id --- artifactory/commands/npm/npmcommand.go | 14 +++-- artifactory/commands/npm/npmcommand_test.go | 60 +++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/artifactory/commands/npm/npmcommand.go b/artifactory/commands/npm/npmcommand.go index f0f63d1db..669e61668 100644 --- a/artifactory/commands/npm/npmcommand.go +++ b/artifactory/commands/npm/npmcommand.go @@ -19,6 +19,7 @@ import ( "github.com/jfrog/jfrog-client-go/utils/log" "github.com/spf13/viper" "os" + "path" "path/filepath" "strconv" "strings" @@ -338,9 +339,7 @@ func (nc *NpmCommand) prepareBuildInfoModule() error { return errorutils.CheckError(err) } nc.buildInfoModule.SetCollectBuildInfo(nc.collectBuildInfo) - if nc.buildConfiguration.GetModule() != "" { - nc.buildInfoModule.SetName(nc.buildConfiguration.GetModule()) - } + nc.buildInfoModule.SetName(nc.resolveModuleName()) return nil } @@ -402,6 +401,15 @@ func (nc *NpmCommand) GetRepo() string { return nc.repo } +// Resolves to the base directory name if a module name is not provided in package.json or CLI command. +func (nc *NpmCommand) resolveModuleName() string { + moduleName := nc.buildConfiguration.GetModule() + if moduleName == "" { + moduleName = path.Base(nc.workingDirectory) + } + return moduleName +} + // Creates an .npmrc file in the project's directory in order to configure the provided Artifactory server as a resolution server func SetArtifactoryAsResolutionServer(serverDetails *config.ServerDetails, depsRepo string) (clearResolutionServerFunc func() error, err error) { npmCmd := NewNpmInstallCommand().SetServerDetails(serverDetails) diff --git a/artifactory/commands/npm/npmcommand_test.go b/artifactory/commands/npm/npmcommand_test.go index 57898f2b9..5cfd7b7f9 100644 --- a/artifactory/commands/npm/npmcommand_test.go +++ b/artifactory/commands/npm/npmcommand_test.go @@ -2,8 +2,10 @@ package npm import ( "fmt" + "github.com/jfrog/build-info-go/build" biutils "github.com/jfrog/build-info-go/utils" "github.com/jfrog/gofrog/version" + buildUtils "github.com/jfrog/jfrog-cli-core/v2/common/build" commonTests "github.com/jfrog/jfrog-cli-core/v2/common/tests" "github.com/jfrog/jfrog-cli-core/v2/utils/tests" testsUtils "github.com/jfrog/jfrog-client-go/utils/tests" @@ -137,3 +139,61 @@ func TestSetArtifactoryAsResolutionServer(t *testing.T) { assert.FileExists(t, filepath.Join(tmpDir, ".npmrc")) } + +func TestResolveModuleName(t *testing.T) { + testCases := []struct { + name string + // Module which is set inside the package.json or as a parameter to the CLI command + moduleFromConfig string + workingDirectory string + expectedModuleName string + }{ + { + name: "Module name provided in config", + moduleFromConfig: "custom-module", + workingDirectory: "/path/to/project", + expectedModuleName: "custom-module", + }, + { + name: "Module name derived from working directory", + moduleFromConfig: "", + workingDirectory: "/path/to/project/my-module", + expectedModuleName: "my-module", + }, + { + name: "Working directory is root", + moduleFromConfig: "", + workingDirectory: "/", + expectedModuleName: "/", + }, + { + name: "Module name with special characters", + moduleFromConfig: "module@1.0.0", + workingDirectory: "/path/to/project", + expectedModuleName: "module@1.0.0", + }, + { + name: "Working directory ends with slash", + moduleFromConfig: "", + workingDirectory: "/path/to/project/my-module/", + expectedModuleName: "my-module", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + nc := setupNpmCommand(tc.moduleFromConfig, tc.workingDirectory) + actualModuleName := nc.resolveModuleName() + assert.Equal(t, tc.expectedModuleName, actualModuleName) + }) + } +} + +func setupNpmCommand(moduleFromConfig, workingDirectory string) *NpmCommand { + nc := NewNpmCommand("Test", true) + nc.buildConfiguration = &buildUtils.BuildConfiguration{} + nc.buildConfiguration.SetModule(moduleFromConfig) + nc.buildInfoModule = &build.NpmModule{} + nc.workingDirectory = workingDirectory + return nc +} From f35be003c847c713ef16d0ccab2fc4c5371f4ed9 Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 15 Jul 2024 16:52:32 +0300 Subject: [PATCH 02/10] Make the solution generic --- artifactory/commands/golang/go.go | 5 +- artifactory/commands/golang/gopublish.go | 5 +- artifactory/commands/npm/npmcommand.go | 12 +---- artifactory/commands/npm/npmcommand_test.go | 60 --------------------- artifactory/commands/npm/publish.go | 5 +- artifactory/commands/python/poetry.go | 5 +- artifactory/commands/python/python.go | 5 +- common/build/buildutils.go | 14 ++++- 8 files changed, 24 insertions(+), 87 deletions(-) diff --git a/artifactory/commands/golang/go.go b/artifactory/commands/golang/go.go index 4847855a5..dbb1e285b 100644 --- a/artifactory/commands/golang/go.go +++ b/artifactory/commands/golang/go.go @@ -197,9 +197,8 @@ func (gc *GoCommand) run() (err error) { if errorutils.CheckError(err) != nil { return } - if gc.buildConfiguration.GetModule() != "" { - goModule.SetName(gc.buildConfiguration.GetModule()) - } + goModule.SetName(gc.buildConfiguration.GetModule()) + err = errorutils.CheckError(goModule.CalcDependencies()) } diff --git a/artifactory/commands/golang/gopublish.go b/artifactory/commands/golang/gopublish.go index 636067682..1db1f21e1 100644 --- a/artifactory/commands/golang/gopublish.go +++ b/artifactory/commands/golang/gopublish.go @@ -128,9 +128,8 @@ func (gpc *GoPublishCommand) Run() error { if err != nil { return errorutils.CheckError(err) } - if gpc.buildConfiguration.GetModule() != "" { - goModule.SetName(gpc.buildConfiguration.GetModule()) - } + goModule.SetName(gpc.buildConfiguration.GetModule()) + err = goModule.AddArtifacts(artifacts...) if err != nil { return errorutils.CheckError(err) diff --git a/artifactory/commands/npm/npmcommand.go b/artifactory/commands/npm/npmcommand.go index 669e61668..18df58137 100644 --- a/artifactory/commands/npm/npmcommand.go +++ b/artifactory/commands/npm/npmcommand.go @@ -19,7 +19,6 @@ import ( "github.com/jfrog/jfrog-client-go/utils/log" "github.com/spf13/viper" "os" - "path" "path/filepath" "strconv" "strings" @@ -339,7 +338,7 @@ func (nc *NpmCommand) prepareBuildInfoModule() error { return errorutils.CheckError(err) } nc.buildInfoModule.SetCollectBuildInfo(nc.collectBuildInfo) - nc.buildInfoModule.SetName(nc.resolveModuleName()) + nc.buildInfoModule.SetName(nc.buildConfiguration.GetModule()) return nil } @@ -401,15 +400,6 @@ func (nc *NpmCommand) GetRepo() string { return nc.repo } -// Resolves to the base directory name if a module name is not provided in package.json or CLI command. -func (nc *NpmCommand) resolveModuleName() string { - moduleName := nc.buildConfiguration.GetModule() - if moduleName == "" { - moduleName = path.Base(nc.workingDirectory) - } - return moduleName -} - // Creates an .npmrc file in the project's directory in order to configure the provided Artifactory server as a resolution server func SetArtifactoryAsResolutionServer(serverDetails *config.ServerDetails, depsRepo string) (clearResolutionServerFunc func() error, err error) { npmCmd := NewNpmInstallCommand().SetServerDetails(serverDetails) diff --git a/artifactory/commands/npm/npmcommand_test.go b/artifactory/commands/npm/npmcommand_test.go index 5cfd7b7f9..57898f2b9 100644 --- a/artifactory/commands/npm/npmcommand_test.go +++ b/artifactory/commands/npm/npmcommand_test.go @@ -2,10 +2,8 @@ package npm import ( "fmt" - "github.com/jfrog/build-info-go/build" biutils "github.com/jfrog/build-info-go/utils" "github.com/jfrog/gofrog/version" - buildUtils "github.com/jfrog/jfrog-cli-core/v2/common/build" commonTests "github.com/jfrog/jfrog-cli-core/v2/common/tests" "github.com/jfrog/jfrog-cli-core/v2/utils/tests" testsUtils "github.com/jfrog/jfrog-client-go/utils/tests" @@ -139,61 +137,3 @@ func TestSetArtifactoryAsResolutionServer(t *testing.T) { assert.FileExists(t, filepath.Join(tmpDir, ".npmrc")) } - -func TestResolveModuleName(t *testing.T) { - testCases := []struct { - name string - // Module which is set inside the package.json or as a parameter to the CLI command - moduleFromConfig string - workingDirectory string - expectedModuleName string - }{ - { - name: "Module name provided in config", - moduleFromConfig: "custom-module", - workingDirectory: "/path/to/project", - expectedModuleName: "custom-module", - }, - { - name: "Module name derived from working directory", - moduleFromConfig: "", - workingDirectory: "/path/to/project/my-module", - expectedModuleName: "my-module", - }, - { - name: "Working directory is root", - moduleFromConfig: "", - workingDirectory: "/", - expectedModuleName: "/", - }, - { - name: "Module name with special characters", - moduleFromConfig: "module@1.0.0", - workingDirectory: "/path/to/project", - expectedModuleName: "module@1.0.0", - }, - { - name: "Working directory ends with slash", - moduleFromConfig: "", - workingDirectory: "/path/to/project/my-module/", - expectedModuleName: "my-module", - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - nc := setupNpmCommand(tc.moduleFromConfig, tc.workingDirectory) - actualModuleName := nc.resolveModuleName() - assert.Equal(t, tc.expectedModuleName, actualModuleName) - }) - } -} - -func setupNpmCommand(moduleFromConfig, workingDirectory string) *NpmCommand { - nc := NewNpmCommand("Test", true) - nc.buildConfiguration = &buildUtils.BuildConfiguration{} - nc.buildConfiguration.SetModule(moduleFromConfig) - nc.buildInfoModule = &build.NpmModule{} - nc.workingDirectory = workingDirectory - return nc -} diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index ae91b9df6..69fe9025c 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -204,9 +204,8 @@ func (npc *NpmPublishCommand) Run() (err error) { if err != nil { return errorutils.CheckError(err) } - if npc.buildConfiguration.GetModule() != "" { - npmModule.SetName(npc.buildConfiguration.GetModule()) - } + npmModule.SetName(npc.buildConfiguration.GetModule()) + buildArtifacts, err := specutils.ConvertArtifactsDetailsToBuildInfoArtifacts(npc.artifactsDetailsReader) if err != nil { return err diff --git a/artifactory/commands/python/poetry.go b/artifactory/commands/python/poetry.go index 33152c9cf..888b44c6b 100644 --- a/artifactory/commands/python/poetry.go +++ b/artifactory/commands/python/poetry.go @@ -75,9 +75,8 @@ func (pc *PoetryCommand) install(buildConfiguration *buildUtils.BuildConfigurati if err != nil { return } - if buildConfiguration.GetModule() != "" { - pythonModule.SetName(buildConfiguration.GetModule()) - } + pythonModule.SetName(buildConfiguration.GetModule()) + var localDependenciesPath string localDependenciesPath, err = config.GetJfrogDependenciesPath() if err != nil { diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index fa0c37718..454939157 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -58,9 +58,8 @@ func (pc *PythonCommand) Run() (err error) { if err != nil { return } - if buildConfiguration.GetModule() != "" { - pythonModule.SetName(buildConfiguration.GetModule()) - } + pythonModule.SetName(buildConfiguration.GetModule()) + var localDependenciesPath string localDependenciesPath, err = config.GetJfrogDependenciesPath() if err != nil { diff --git a/common/build/buildutils.go b/common/build/buildutils.go index 4184403aa..424581c49 100644 --- a/common/build/buildutils.go +++ b/common/build/buildutils.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "os" + "path" "path/filepath" "strconv" "strings" @@ -400,8 +401,19 @@ func (bc *BuildConfiguration) GetProject() string { return bc.project } +// GetModule returns the module name. +// If the module name is not specified, it falls back to the current working directory name. +// If the working directory cannot be retrieved, it defaults to "module". +// Module name is mandatory to publish build info. func (bc *BuildConfiguration) GetModule() string { - return bc.module + if bc.module != "" { + return bc.module + } + wd, err := os.Getwd() + if err == nil { + return path.Base(wd) + } + return "module" } // Validates: From 7666f1af933d472feba7bbf5f239763a9c8ad005 Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 15 Jul 2024 17:17:10 +0300 Subject: [PATCH 03/10] Extract from getModule function --- artifactory/commands/golang/go.go | 2 +- artifactory/commands/golang/gopublish.go | 2 +- artifactory/commands/npm/npmcommand.go | 2 +- artifactory/commands/npm/publish.go | 2 +- artifactory/commands/python/poetry.go | 2 +- artifactory/commands/python/python.go | 2 +- common/build/buildutils.go | 29 +++++++++++---------- common/build/buildutils_test.go | 33 ++++++++++++++++++++++++ go.mod | 1 + 9 files changed, 56 insertions(+), 19 deletions(-) diff --git a/artifactory/commands/golang/go.go b/artifactory/commands/golang/go.go index dbb1e285b..279cbc6fe 100644 --- a/artifactory/commands/golang/go.go +++ b/artifactory/commands/golang/go.go @@ -197,7 +197,7 @@ func (gc *GoCommand) run() (err error) { if errorutils.CheckError(err) != nil { return } - goModule.SetName(gc.buildConfiguration.GetModule()) + goModule.SetName(gc.buildConfiguration.ResolveBaseModuleName()) err = errorutils.CheckError(goModule.CalcDependencies()) } diff --git a/artifactory/commands/golang/gopublish.go b/artifactory/commands/golang/gopublish.go index 1db1f21e1..1d6fc6cf1 100644 --- a/artifactory/commands/golang/gopublish.go +++ b/artifactory/commands/golang/gopublish.go @@ -128,7 +128,7 @@ func (gpc *GoPublishCommand) Run() error { if err != nil { return errorutils.CheckError(err) } - goModule.SetName(gpc.buildConfiguration.GetModule()) + goModule.SetName(gpc.buildConfiguration.ResolveBaseModuleName()) err = goModule.AddArtifacts(artifacts...) if err != nil { diff --git a/artifactory/commands/npm/npmcommand.go b/artifactory/commands/npm/npmcommand.go index 18df58137..4c91f3928 100644 --- a/artifactory/commands/npm/npmcommand.go +++ b/artifactory/commands/npm/npmcommand.go @@ -338,7 +338,7 @@ func (nc *NpmCommand) prepareBuildInfoModule() error { return errorutils.CheckError(err) } nc.buildInfoModule.SetCollectBuildInfo(nc.collectBuildInfo) - nc.buildInfoModule.SetName(nc.buildConfiguration.GetModule()) + nc.buildInfoModule.SetName(nc.buildConfiguration.ResolveBaseModuleName()) return nil } diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 69fe9025c..3a9b32ac7 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -204,7 +204,7 @@ func (npc *NpmPublishCommand) Run() (err error) { if err != nil { return errorutils.CheckError(err) } - npmModule.SetName(npc.buildConfiguration.GetModule()) + npmModule.SetName(npc.buildConfiguration.ResolveBaseModuleName()) buildArtifacts, err := specutils.ConvertArtifactsDetailsToBuildInfoArtifacts(npc.artifactsDetailsReader) if err != nil { diff --git a/artifactory/commands/python/poetry.go b/artifactory/commands/python/poetry.go index 888b44c6b..4d7bb75c8 100644 --- a/artifactory/commands/python/poetry.go +++ b/artifactory/commands/python/poetry.go @@ -75,7 +75,7 @@ func (pc *PoetryCommand) install(buildConfiguration *buildUtils.BuildConfigurati if err != nil { return } - pythonModule.SetName(buildConfiguration.GetModule()) + pythonModule.SetName(buildConfiguration.ResolveBaseModuleName()) var localDependenciesPath string localDependenciesPath, err = config.GetJfrogDependenciesPath() diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index 454939157..adc08d854 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -58,7 +58,7 @@ func (pc *PythonCommand) Run() (err error) { if err != nil { return } - pythonModule.SetName(buildConfiguration.GetModule()) + pythonModule.SetName(buildConfiguration.ResolveBaseModuleName()) var localDependenciesPath string localDependenciesPath, err = config.GetJfrogDependenciesPath() diff --git a/common/build/buildutils.go b/common/build/buildutils.go index 424581c49..8afaeac06 100644 --- a/common/build/buildutils.go +++ b/common/build/buildutils.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "os" - "path" "path/filepath" "strconv" "strings" @@ -401,19 +400,8 @@ func (bc *BuildConfiguration) GetProject() string { return bc.project } -// GetModule returns the module name. -// If the module name is not specified, it falls back to the current working directory name. -// If the working directory cannot be retrieved, it defaults to "module". -// Module name is mandatory to publish build info. func (bc *BuildConfiguration) GetModule() string { - if bc.module != "" { - return bc.module - } - wd, err := os.Getwd() - if err == nil { - return path.Base(wd) - } - return "module" + return bc.module } // Validates: @@ -473,6 +461,21 @@ func (bc *BuildConfiguration) IsLoadedFromConfigFile() bool { return bc.loadedFromConfigFile } +// ResolveBaseModuleName returns the module name to publish build info. +// If the module name is not specified, it falls back to the current working directory name. +// If the working directory cannot be retrieved, it defaults to "module". +// Module name is mandatory to publish build info. +func (bc *BuildConfiguration) ResolveBaseModuleName() string { + if bc.module != "" { + return bc.module + } + wd, err := os.Getwd() + if err == nil { + return filepath.Base(wd) + } + return "module" +} + func PopulateBuildArtifactsAsPartials(buildArtifacts []buildInfo.Artifact, buildConfiguration *BuildConfiguration, moduleType buildInfo.ModuleType) error { populateFunc := func(partial *buildInfo.Partial) { partial.Artifacts = buildArtifacts diff --git a/common/build/buildutils_test.go b/common/build/buildutils_test.go index 4c54fcc89..6e06915f2 100644 --- a/common/build/buildutils_test.go +++ b/common/build/buildutils_test.go @@ -245,3 +245,36 @@ func TestIsLoadedFromConfigFile(t *testing.T) { assert.Equal(t, buildName, buildNameFile) assert.Equal(t, buildNumber, artclientutils.LatestBuildNumberKey) } + +func TestBuildConfiguration_ResolveModuleName(t *testing.T) { + testCases := []struct { + name string + module string + }{ + { + name: "Module is set", + module: "custom-module", + }, + { + name: "Module not set, GetWd succeeds", + module: "", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // Setup + bc := BuildConfiguration{module: tc.module} + // Execute + result := bc.ResolveBaseModuleName() + // Assert + if tc.module == "" { + wd, err := os.Getwd() + assert.NoError(t, err) + assert.Equal(t, filepath.Base(wd), result) + } else { + assert.Equal(t, tc.module, result) + } + }) + } +} diff --git a/go.mod b/go.mod index 78717428c..1df28b689 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/magiconair/properties v1.8.7 github.com/manifoldco/promptui v0.9.0 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c + github.com/pkg/errors v0.9.1 github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.9.0 github.com/urfave/cli v1.22.15 From ff1910ea97c344cdec18ac254d59c89371b72013 Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 15 Jul 2024 17:18:15 +0300 Subject: [PATCH 04/10] Fix diff --- artifactory/commands/golang/go.go | 1 - artifactory/commands/golang/gopublish.go | 1 - artifactory/commands/npm/publish.go | 1 - artifactory/commands/python/poetry.go | 1 - artifactory/commands/python/python.go | 1 - go.mod | 1 - 6 files changed, 6 deletions(-) diff --git a/artifactory/commands/golang/go.go b/artifactory/commands/golang/go.go index 279cbc6fe..d74201141 100644 --- a/artifactory/commands/golang/go.go +++ b/artifactory/commands/golang/go.go @@ -198,7 +198,6 @@ func (gc *GoCommand) run() (err error) { return } goModule.SetName(gc.buildConfiguration.ResolveBaseModuleName()) - err = errorutils.CheckError(goModule.CalcDependencies()) } diff --git a/artifactory/commands/golang/gopublish.go b/artifactory/commands/golang/gopublish.go index 1d6fc6cf1..6f1937486 100644 --- a/artifactory/commands/golang/gopublish.go +++ b/artifactory/commands/golang/gopublish.go @@ -129,7 +129,6 @@ func (gpc *GoPublishCommand) Run() error { return errorutils.CheckError(err) } goModule.SetName(gpc.buildConfiguration.ResolveBaseModuleName()) - err = goModule.AddArtifacts(artifacts...) if err != nil { return errorutils.CheckError(err) diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 3a9b32ac7..623b989a2 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -205,7 +205,6 @@ func (npc *NpmPublishCommand) Run() (err error) { return errorutils.CheckError(err) } npmModule.SetName(npc.buildConfiguration.ResolveBaseModuleName()) - buildArtifacts, err := specutils.ConvertArtifactsDetailsToBuildInfoArtifacts(npc.artifactsDetailsReader) if err != nil { return err diff --git a/artifactory/commands/python/poetry.go b/artifactory/commands/python/poetry.go index 4d7bb75c8..4ccdc1295 100644 --- a/artifactory/commands/python/poetry.go +++ b/artifactory/commands/python/poetry.go @@ -76,7 +76,6 @@ func (pc *PoetryCommand) install(buildConfiguration *buildUtils.BuildConfigurati return } pythonModule.SetName(buildConfiguration.ResolveBaseModuleName()) - var localDependenciesPath string localDependenciesPath, err = config.GetJfrogDependenciesPath() if err != nil { diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index adc08d854..00f309b3f 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -59,7 +59,6 @@ func (pc *PythonCommand) Run() (err error) { return } pythonModule.SetName(buildConfiguration.ResolveBaseModuleName()) - var localDependenciesPath string localDependenciesPath, err = config.GetJfrogDependenciesPath() if err != nil { diff --git a/go.mod b/go.mod index 1df28b689..78717428c 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,6 @@ require ( github.com/magiconair/properties v1.8.7 github.com/manifoldco/promptui v0.9.0 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c - github.com/pkg/errors v0.9.1 github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.9.0 github.com/urfave/cli v1.22.15 From 2e153b94e059369deca15033459face11b64506e Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 15 Jul 2024 17:19:10 +0300 Subject: [PATCH 05/10] Test comment --- common/build/buildutils_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/build/buildutils_test.go b/common/build/buildutils_test.go index 6e06915f2..ab4c457ba 100644 --- a/common/build/buildutils_test.go +++ b/common/build/buildutils_test.go @@ -256,7 +256,7 @@ func TestBuildConfiguration_ResolveModuleName(t *testing.T) { module: "custom-module", }, { - name: "Module not set, GetWd succeeds", + name: "Module not set, fallback to base directory name", module: "", }, } From c3124db0cb8c7353db3b894a51fcfcfd6b97d90e Mon Sep 17 00:00:00 2001 From: delarea Date: Sun, 21 Jul 2024 10:15:54 +0300 Subject: [PATCH 06/10] Return error if fail to get base dir --- artifactory/commands/golang/go.go | 7 ++++++- artifactory/commands/golang/gopublish.go | 6 +++++- artifactory/commands/npm/publish.go | 8 +++++++- artifactory/commands/python/poetry.go | 6 +++++- artifactory/commands/python/python.go | 6 +++++- common/build/buildutils.go | 12 +++++++----- common/build/buildutils_test.go | 3 ++- 7 files changed, 37 insertions(+), 11 deletions(-) diff --git a/artifactory/commands/golang/go.go b/artifactory/commands/golang/go.go index d74201141..87570c75d 100644 --- a/artifactory/commands/golang/go.go +++ b/artifactory/commands/golang/go.go @@ -197,7 +197,12 @@ func (gc *GoCommand) run() (err error) { if errorutils.CheckError(err) != nil { return } - goModule.SetName(gc.buildConfiguration.ResolveBaseModuleName()) + moduleName, err := gc.buildConfiguration.ResolveBaseModuleName() + if errorutils.CheckError(err) != nil { + return + } + goModule.SetName(moduleName) + err = errorutils.CheckError(goModule.CalcDependencies()) } diff --git a/artifactory/commands/golang/gopublish.go b/artifactory/commands/golang/gopublish.go index 6f1937486..1a671ac52 100644 --- a/artifactory/commands/golang/gopublish.go +++ b/artifactory/commands/golang/gopublish.go @@ -128,7 +128,11 @@ func (gpc *GoPublishCommand) Run() error { if err != nil { return errorutils.CheckError(err) } - goModule.SetName(gpc.buildConfiguration.ResolveBaseModuleName()) + moduleName, err := gpc.buildConfiguration.ResolveBaseModuleName() + if errorutils.CheckError(err) != nil { + return err + } + goModule.SetName(moduleName) err = goModule.AddArtifacts(artifacts...) if err != nil { return errorutils.CheckError(err) diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 623b989a2..9657748f9 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -204,7 +204,13 @@ func (npc *NpmPublishCommand) Run() (err error) { if err != nil { return errorutils.CheckError(err) } - npmModule.SetName(npc.buildConfiguration.ResolveBaseModuleName()) + + moduleName, err := npc.buildConfiguration.ResolveBaseModuleName() + if errorutils.CheckError(err) != nil { + return + } + npmModule.SetName(moduleName) + buildArtifacts, err := specutils.ConvertArtifactsDetailsToBuildInfoArtifacts(npc.artifactsDetailsReader) if err != nil { return err diff --git a/artifactory/commands/python/poetry.go b/artifactory/commands/python/poetry.go index 4ccdc1295..f1b9c0db9 100644 --- a/artifactory/commands/python/poetry.go +++ b/artifactory/commands/python/poetry.go @@ -75,7 +75,11 @@ func (pc *PoetryCommand) install(buildConfiguration *buildUtils.BuildConfigurati if err != nil { return } - pythonModule.SetName(buildConfiguration.ResolveBaseModuleName()) + moduleName, err := buildConfiguration.ResolveBaseModuleName() + if errorutils.CheckError(err) != nil { + return + } + pythonModule.SetName(moduleName) var localDependenciesPath string localDependenciesPath, err = config.GetJfrogDependenciesPath() if err != nil { diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index 00f309b3f..a918020bf 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -58,7 +58,11 @@ func (pc *PythonCommand) Run() (err error) { if err != nil { return } - pythonModule.SetName(buildConfiguration.ResolveBaseModuleName()) + moduleName, err := buildConfiguration.ResolveBaseModuleName() + if errorutils.CheckError(err) != nil { + return + } + pythonModule.SetName(moduleName) var localDependenciesPath string localDependenciesPath, err = config.GetJfrogDependenciesPath() if err != nil { diff --git a/common/build/buildutils.go b/common/build/buildutils.go index 8afaeac06..a928a2b93 100644 --- a/common/build/buildutils.go +++ b/common/build/buildutils.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "os" + "path" "path/filepath" "strconv" "strings" @@ -465,15 +466,16 @@ func (bc *BuildConfiguration) IsLoadedFromConfigFile() bool { // If the module name is not specified, it falls back to the current working directory name. // If the working directory cannot be retrieved, it defaults to "module". // Module name is mandatory to publish build info. -func (bc *BuildConfiguration) ResolveBaseModuleName() string { +func (bc *BuildConfiguration) ResolveBaseModuleName() (moduleName string, err error) { if bc.module != "" { - return bc.module + moduleName = bc.module + return } wd, err := os.Getwd() - if err == nil { - return filepath.Base(wd) + if err != nil { + return } - return "module" + return path.Base(wd), nil } func PopulateBuildArtifactsAsPartials(buildArtifacts []buildInfo.Artifact, buildConfiguration *BuildConfiguration, moduleType buildInfo.ModuleType) error { diff --git a/common/build/buildutils_test.go b/common/build/buildutils_test.go index ab4c457ba..618ec2ef0 100644 --- a/common/build/buildutils_test.go +++ b/common/build/buildutils_test.go @@ -266,7 +266,8 @@ func TestBuildConfiguration_ResolveModuleName(t *testing.T) { // Setup bc := BuildConfiguration{module: tc.module} // Execute - result := bc.ResolveBaseModuleName() + result, err := bc.ResolveBaseModuleName() + assert.NoError(t, err) // Assert if tc.module == "" { wd, err := os.Getwd() From 27b171c425a58915d2306987bf43d6ba3b1da584 Mon Sep 17 00:00:00 2001 From: delarea Date: Sun, 21 Jul 2024 10:17:59 +0300 Subject: [PATCH 07/10] Fix static checks --- artifactory/commands/golang/go.go | 2 +- artifactory/commands/npm/npmcommand.go | 6 +++++- artifactory/commands/python/python.go | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/artifactory/commands/golang/go.go b/artifactory/commands/golang/go.go index 87570c75d..cfff08093 100644 --- a/artifactory/commands/golang/go.go +++ b/artifactory/commands/golang/go.go @@ -199,7 +199,7 @@ func (gc *GoCommand) run() (err error) { } moduleName, err := gc.buildConfiguration.ResolveBaseModuleName() if errorutils.CheckError(err) != nil { - return + return err } goModule.SetName(moduleName) diff --git a/artifactory/commands/npm/npmcommand.go b/artifactory/commands/npm/npmcommand.go index 89bda3b9a..fdae6d70f 100644 --- a/artifactory/commands/npm/npmcommand.go +++ b/artifactory/commands/npm/npmcommand.go @@ -339,7 +339,11 @@ func (nc *NpmCommand) prepareBuildInfoModule() error { return errorutils.CheckError(err) } nc.buildInfoModule.SetCollectBuildInfo(nc.collectBuildInfo) - nc.buildInfoModule.SetName(nc.buildConfiguration.ResolveBaseModuleName()) + moduleName, err := nc.buildConfiguration.ResolveBaseModuleName() + if errorutils.CheckError(err) != nil { + return err + } + nc.buildInfoModule.SetName(moduleName) return nil } diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index a918020bf..7973d0d9b 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -60,7 +60,7 @@ func (pc *PythonCommand) Run() (err error) { } moduleName, err := buildConfiguration.ResolveBaseModuleName() if errorutils.CheckError(err) != nil { - return + return err } pythonModule.SetName(moduleName) var localDependenciesPath string From be876bd9835d28119e9b6204b08eb35b8b6218a9 Mon Sep 17 00:00:00 2001 From: delarea Date: Sun, 21 Jul 2024 10:21:41 +0300 Subject: [PATCH 08/10] Fix inner error declaration --- artifactory/commands/golang/go.go | 5 +++-- artifactory/commands/python/python.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/artifactory/commands/golang/go.go b/artifactory/commands/golang/go.go index cfff08093..1443c0b0a 100644 --- a/artifactory/commands/golang/go.go +++ b/artifactory/commands/golang/go.go @@ -197,9 +197,10 @@ func (gc *GoCommand) run() (err error) { if errorutils.CheckError(err) != nil { return } - moduleName, err := gc.buildConfiguration.ResolveBaseModuleName() + var moduleName string + moduleName, err = gc.buildConfiguration.ResolveBaseModuleName() if errorutils.CheckError(err) != nil { - return err + return } goModule.SetName(moduleName) diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index 7973d0d9b..df8d78111 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -58,9 +58,10 @@ func (pc *PythonCommand) Run() (err error) { if err != nil { return } - moduleName, err := buildConfiguration.ResolveBaseModuleName() + var moduleName string + moduleName, err = buildConfiguration.ResolveBaseModuleName() if errorutils.CheckError(err) != nil { - return err + return } pythonModule.SetName(moduleName) var localDependenciesPath string From bf86521db50f30486a8199ec15cdf612064a5df8 Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 23 Jul 2024 16:25:13 +0300 Subject: [PATCH 09/10] use path for windows --- common/build/buildutils_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/build/buildutils_test.go b/common/build/buildutils_test.go index 618ec2ef0..0cef1089f 100644 --- a/common/build/buildutils_test.go +++ b/common/build/buildutils_test.go @@ -3,6 +3,7 @@ package build import ( biutils "github.com/jfrog/build-info-go/utils" "os" + "path" "path/filepath" "strconv" "testing" @@ -272,7 +273,7 @@ func TestBuildConfiguration_ResolveModuleName(t *testing.T) { if tc.module == "" { wd, err := os.Getwd() assert.NoError(t, err) - assert.Equal(t, filepath.Base(wd), result) + assert.Equal(t, path.Base(wd), result) } else { assert.Equal(t, tc.module, result) } From b71a0192905e7d61db8ac5524e33af91b25b58c0 Mon Sep 17 00:00:00 2001 From: RobiNino Date: Tue, 30 Jul 2024 10:11:06 +0300 Subject: [PATCH 10/10] CR comments --- artifactory/commands/golang/go.go | 2 +- artifactory/commands/golang/gopublish.go | 2 +- artifactory/commands/npm/npmcommand.go | 2 +- artifactory/commands/npm/publish.go | 2 +- artifactory/commands/python/poetry.go | 2 +- artifactory/commands/python/python.go | 2 +- common/build/buildutils.go | 10 +++------- go.mod | 4 ++-- go.sum | 8 ++++---- 9 files changed, 15 insertions(+), 19 deletions(-) diff --git a/artifactory/commands/golang/go.go b/artifactory/commands/golang/go.go index 1443c0b0a..556859b10 100644 --- a/artifactory/commands/golang/go.go +++ b/artifactory/commands/golang/go.go @@ -199,7 +199,7 @@ func (gc *GoCommand) run() (err error) { } var moduleName string moduleName, err = gc.buildConfiguration.ResolveBaseModuleName() - if errorutils.CheckError(err) != nil { + if err != nil { return } goModule.SetName(moduleName) diff --git a/artifactory/commands/golang/gopublish.go b/artifactory/commands/golang/gopublish.go index 1a671ac52..c55f8e3ea 100644 --- a/artifactory/commands/golang/gopublish.go +++ b/artifactory/commands/golang/gopublish.go @@ -129,7 +129,7 @@ func (gpc *GoPublishCommand) Run() error { return errorutils.CheckError(err) } moduleName, err := gpc.buildConfiguration.ResolveBaseModuleName() - if errorutils.CheckError(err) != nil { + if err != nil { return err } goModule.SetName(moduleName) diff --git a/artifactory/commands/npm/npmcommand.go b/artifactory/commands/npm/npmcommand.go index be5045a46..f6cb140f9 100644 --- a/artifactory/commands/npm/npmcommand.go +++ b/artifactory/commands/npm/npmcommand.go @@ -354,7 +354,7 @@ func (nc *NpmCommand) prepareBuildInfoModule() error { } nc.buildInfoModule.SetCollectBuildInfo(nc.collectBuildInfo) moduleName, err := nc.buildConfiguration.ResolveBaseModuleName() - if errorutils.CheckError(err) != nil { + if err != nil { return err } nc.buildInfoModule.SetName(moduleName) diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 9657748f9..36a7afd1b 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -206,7 +206,7 @@ func (npc *NpmPublishCommand) Run() (err error) { } moduleName, err := npc.buildConfiguration.ResolveBaseModuleName() - if errorutils.CheckError(err) != nil { + if err != nil { return } npmModule.SetName(moduleName) diff --git a/artifactory/commands/python/poetry.go b/artifactory/commands/python/poetry.go index f1b9c0db9..ceac5e9bf 100644 --- a/artifactory/commands/python/poetry.go +++ b/artifactory/commands/python/poetry.go @@ -76,7 +76,7 @@ func (pc *PoetryCommand) install(buildConfiguration *buildUtils.BuildConfigurati return } moduleName, err := buildConfiguration.ResolveBaseModuleName() - if errorutils.CheckError(err) != nil { + if err != nil { return } pythonModule.SetName(moduleName) diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index df8d78111..e66988b8a 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -60,7 +60,7 @@ func (pc *PythonCommand) Run() (err error) { } var moduleName string moduleName, err = buildConfiguration.ResolveBaseModuleName() - if errorutils.CheckError(err) != nil { + if err != nil { return } pythonModule.SetName(moduleName) diff --git a/common/build/buildutils.go b/common/build/buildutils.go index a928a2b93..1f7b42f48 100644 --- a/common/build/buildutils.go +++ b/common/build/buildutils.go @@ -466,16 +466,12 @@ func (bc *BuildConfiguration) IsLoadedFromConfigFile() bool { // If the module name is not specified, it falls back to the current working directory name. // If the working directory cannot be retrieved, it defaults to "module". // Module name is mandatory to publish build info. -func (bc *BuildConfiguration) ResolveBaseModuleName() (moduleName string, err error) { +func (bc *BuildConfiguration) ResolveBaseModuleName() (string, error) { if bc.module != "" { - moduleName = bc.module - return + return bc.module, nil } wd, err := os.Getwd() - if err != nil { - return - } - return path.Base(wd), nil + return path.Base(wd), errorutils.CheckError(err) } func PopulateBuildArtifactsAsPartials(buildArtifacts []buildInfo.Artifact, buildConfiguration *BuildConfiguration, moduleType buildInfo.ModuleType) error { diff --git a/go.mod b/go.mod index b00c02b9b..441717fa7 100644 --- a/go.mod +++ b/go.mod @@ -96,8 +96,8 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect ) -replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240729103741-f6c521eb9a40 +replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240729122747-c9525aec45be -replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20240729055355-5b51bf71e756 +replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20240729133409-38a2c49a0d75 // replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.3.3-0.20231223133729-ef57bd08cedc diff --git a/go.sum b/go.sum index de53dccaa..46cee7337 100644 --- a/go.sum +++ b/go.sum @@ -92,12 +92,12 @@ github.com/jedib0t/go-pretty/v6 v6.5.9 h1:ACteMBRrrmm1gMsXe9PSTOClQ63IXDUt03H5U+ github.com/jedib0t/go-pretty/v6 v6.5.9/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E= github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI= github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw= -github.com/jfrog/build-info-go v1.8.9-0.20240729055355-5b51bf71e756 h1:xyZEZDZvg/sLS5PH5b8zFnj6b2XBW4aYHXey0sv+F9I= -github.com/jfrog/build-info-go v1.8.9-0.20240729055355-5b51bf71e756/go.mod h1:fxSfpp+kQ8qln/0y7pgV3tg2exWe5OM1np5FF2LlSDg= +github.com/jfrog/build-info-go v1.8.9-0.20240729133409-38a2c49a0d75 h1:K4BHluuCXPiPi63KtFXK1VwHO4phGasUbDEYvGBI8r0= +github.com/jfrog/build-info-go v1.8.9-0.20240729133409-38a2c49a0d75/go.mod h1:fxSfpp+kQ8qln/0y7pgV3tg2exWe5OM1np5FF2LlSDg= github.com/jfrog/gofrog v1.7.5 h1:dFgtEDefJdlq9cqTRoe09RLxS5Bxbe1Ev5+E6SmZHcg= github.com/jfrog/gofrog v1.7.5/go.mod h1:jyGiCgiqSSR7k86hcUSu67XVvmvkkgWTmPsH25wI298= -github.com/jfrog/jfrog-client-go v1.28.1-0.20240729103741-f6c521eb9a40 h1:zmh1NwYtbS3GE7jCPWtQpGsTakMo5DHl73BRkZTn6Ag= -github.com/jfrog/jfrog-client-go v1.28.1-0.20240729103741-f6c521eb9a40/go.mod h1:BJqDxNVbs34kuCx9ct6ge3/M39Pf/m5L5IYgZ9p8ap4= +github.com/jfrog/jfrog-client-go v1.28.1-0.20240729122747-c9525aec45be h1:DxjfGR8F2AFNchhliRt+ORWpIL4AC6je/rPttwcOAhw= +github.com/jfrog/jfrog-client-go v1.28.1-0.20240729122747-c9525aec45be/go.mod h1:BJqDxNVbs34kuCx9ct6ge3/M39Pf/m5L5IYgZ9p8ap4= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=