Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix root module name handling in build info collection #1201

Merged
8 changes: 6 additions & 2 deletions artifactory/commands/golang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,13 @@ func (gc *GoCommand) run() (err error) {
if errorutils.CheckError(err) != nil {
return
}
if gc.buildConfiguration.GetModule() != "" {
goModule.SetName(gc.buildConfiguration.GetModule())
var moduleName string
moduleName, err = gc.buildConfiguration.ResolveBaseModuleName()
if err != nil {
return
}
goModule.SetName(moduleName)

err = errorutils.CheckError(goModule.CalcDependencies())
}

Expand Down
6 changes: 4 additions & 2 deletions artifactory/commands/golang/gopublish.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ func (gpc *GoPublishCommand) Run() error {
if err != nil {
return errorutils.CheckError(err)
}
if gpc.buildConfiguration.GetModule() != "" {
goModule.SetName(gpc.buildConfiguration.GetModule())
moduleName, err := gpc.buildConfiguration.ResolveBaseModuleName()
if err != nil {
return err
}
goModule.SetName(moduleName)
err = goModule.AddArtifacts(artifacts...)
if err != nil {
return errorutils.CheckError(err)
Expand Down
6 changes: 4 additions & 2 deletions artifactory/commands/npm/npmcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,11 @@ func (nc *NpmCommand) prepareBuildInfoModule() error {
return errorutils.CheckError(err)
}
nc.buildInfoModule.SetCollectBuildInfo(nc.collectBuildInfo)
if nc.buildConfiguration.GetModule() != "" {
nc.buildInfoModule.SetName(nc.buildConfiguration.GetModule())
moduleName, err := nc.buildConfiguration.ResolveBaseModuleName()
if err != nil {
return err
}
nc.buildInfoModule.SetName(moduleName)
return nil
}

Expand Down
8 changes: 6 additions & 2 deletions artifactory/commands/npm/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ func (npc *NpmPublishCommand) Run() (err error) {
if err != nil {
return errorutils.CheckError(err)
}
if npc.buildConfiguration.GetModule() != "" {
npmModule.SetName(npc.buildConfiguration.GetModule())

moduleName, err := npc.buildConfiguration.ResolveBaseModuleName()
if err != nil {
return
}
npmModule.SetName(moduleName)

buildArtifacts, err := specutils.ConvertArtifactsDetailsToBuildInfoArtifacts(npc.artifactsDetailsReader)
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions artifactory/commands/python/poetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func (pc *PoetryCommand) install(buildConfiguration *buildUtils.BuildConfigurati
if err != nil {
return
}
if buildConfiguration.GetModule() != "" {
pythonModule.SetName(buildConfiguration.GetModule())
moduleName, err := buildConfiguration.ResolveBaseModuleName()
if err != nil {
return
}
pythonModule.SetName(moduleName)
var localDependenciesPath string
localDependenciesPath, err = config.GetJfrogDependenciesPath()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions artifactory/commands/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ func (pc *PythonCommand) Run() (err error) {
if err != nil {
return
}
if buildConfiguration.GetModule() != "" {
pythonModule.SetName(buildConfiguration.GetModule())
var moduleName string
moduleName, err = buildConfiguration.ResolveBaseModuleName()
if err != nil {
return
}
pythonModule.SetName(moduleName)
var localDependenciesPath string
localDependenciesPath, err = config.GetJfrogDependenciesPath()
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions common/build/buildutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -461,6 +462,18 @@ 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, error) {
if bc.module != "" {
return bc.module, nil
}
wd, err := os.Getwd()
return path.Base(wd), errorutils.CheckError(err)
}

func PopulateBuildArtifactsAsPartials(buildArtifacts []buildInfo.Artifact, buildConfiguration *BuildConfiguration, moduleType buildInfo.ModuleType) error {
populateFunc := func(partial *buildInfo.Partial) {
partial.Artifacts = buildArtifacts
Expand Down
35 changes: 35 additions & 0 deletions common/build/buildutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
biutils "github.com/jfrog/build-info-go/utils"
"os"
"path"
"path/filepath"
"strconv"
"testing"
Expand Down Expand Up @@ -245,3 +246,37 @@ 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, fallback to base directory name",
module: "",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup
bc := BuildConfiguration{module: tc.module}
// Execute
result, err := bc.ResolveBaseModuleName()
assert.NoError(t, err)
// Assert
if tc.module == "" {
wd, err := os.Getwd()
assert.NoError(t, err)
assert.Equal(t, path.Base(wd), result)
} else {
assert.Equal(t, tc.module, result)
}
})
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading