Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalbe4 committed Jun 9, 2024
2 parents d621b66 + f93a07f commit e5a7ca2
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x
cache: false
- name: Static Code Analysis
uses: golangci/golangci-lint-action@v3
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x
cache: false
- name: Run Gosec Security Scanner
uses: securego/gosec@master
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/frogbot-scan-and-fix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:

# Install prerequisites
- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x

- uses: jfrog/frogbot@v2
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/frogbot-scan-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
steps:
# Install prerequisites
- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x

- uses: jfrog/frogbot@v2
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ jobs:
- uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x

- name: Setup Python3
uses: actions/setup-python@v5
Expand Down
2 changes: 1 addition & 1 deletion build/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (gm *GoModule) loadDependencies() ([]entities.Dependency, error) {
}

func (gm *GoModule) getGoDependencies(cachePath string) (map[string]entities.Dependency, error) {
modulesMap, err := utils.GetDependenciesList(gm.srcPath, gm.containingBuild.logger)
modulesMap, err := utils.GetDependenciesList(gm.srcPath, gm.containingBuild.logger, nil)
if err != nil || len(modulesMap) == 0 {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/jfrog/build-info-go

go 1.20
go 1.22.3

require (
github.com/BurntSushi/toml v1.3.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
github.com/CycloneDX/cyclonedx-go v0.8.0 h1:FyWVj6x6hoJrui5uRQdYZcSievw3Z32Z88uYzG/0D6M=
github.com/CycloneDX/cyclonedx-go v0.8.0/go.mod h1:K2bA+324+Og0X84fA8HhN2X066K7Bxz4rpMQ4ZhjtSk=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
Expand All @@ -25,6 +26,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/terminalstatic/go-xsd-validate v0.1.5 h1:RqpJnf6HGE2CB/lZB1A8BYguk8uRtcvYAPLCF15qguo=
github.com/terminalstatic/go-xsd-validate v0.1.5/go.mod h1:18lsvYFofBflqCrvo1umpABZ99+GneNTw2kEEc8UPJw=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
Expand Down
13 changes: 11 additions & 2 deletions utils/goutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,25 @@ func getListCmdArgs() (cmdArgs []string, err error) {
return []string{"list", "-mod=mod"}, nil
}

// The handle error function is designed to manage errors that occur while running the
// 'go list' command. If the function returns true, the error will be propagated back to the caller.
type HandleErrorFunc func(err error) (bool, error)

// Runs go list -f {{with .Module}}{{.Path}}:{{.Version}}{{end}} all command and returns map of the dependencies
func GetDependenciesList(projectDir string, log Log) (map[string]bool, error) {
func GetDependenciesList(projectDir string, log Log, handleError HandleErrorFunc) (map[string]bool, error) {
cmdArgs, err := getListCmdArgs()
if err != nil {
return nil, err
}
output, err := runDependenciesCmd(projectDir, append(cmdArgs, "-f", "{{with .Module}}{{.Path}}:{{.Version}}{{end}}", "all"), log)
if err != nil {
// Errors occurred while running "go list". Run again and this time ignore errors (with '-e')
log.Warn("Errors occurred while building the Go dependency tree. The dependency tree may be incomplete: " + err.Error())
if handleError != nil {
if stop, newErr := handleError(err); stop {
return nil, newErr
}
}
// Errors occurred while running "go list". Run again and this time ignore errors (with '-e')
output, err = runDependenciesCmd(projectDir, append(cmdArgs, "-e", "-f", "{{with .Module}}{{.Path}}:{{.Version}}{{end}}", "all"), log)
if err != nil {
return nil, err
Expand Down
20 changes: 16 additions & 4 deletions utils/goutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,24 @@ func TestGetProjectRoot(t *testing.T) {
}

func TestGetDependenciesList(t *testing.T) {
testGetDependenciesList(t, "testGoList")
testGetDependenciesList(t, "testGoList", nil)
}

func TestGetDependenciesListWithIgnoreErrors(t *testing.T) {
// In some cases, we see that running go list on some Go packages may fail.
// We should allow ignoring the errors in such cases and build the Go dependency tree, even if partial.
testGetDependenciesList(t, "testBadGoList")
testGetDependenciesList(t, "testBadGoList", nil)
// In some cases we would like to exit after we receive an error. This can be done with custom error handle func.
// This test handleErrorFunc return an error
testGetDependenciesList(t, "testBadGoList", func(err error) (bool, error) {
if err != nil {
return true, err
}
return false, nil
})
}

func testGetDependenciesList(t *testing.T, testDir string) {
func testGetDependenciesList(t *testing.T, testDir string, errorFunc HandleErrorFunc) {
log := NewDefaultLogger(ERROR)
goModPath := filepath.Join("testdata", "mods", testDir)
err := os.Rename(filepath.Join(goModPath, "go.mod.txt"), filepath.Join(goModPath, "go.mod"))
Expand All @@ -113,7 +121,11 @@ func testGetDependenciesList(t *testing.T, testDir string) {
err = os.Rename(filepath.Join(goModPath, "test.go"), filepath.Join(goModPath, "test.go.txt"))
assert.NoError(t, err)
}()
actual, err := GetDependenciesList(goModPath, log)
actual, err := GetDependenciesList(goModPath, log, errorFunc)
if errorFunc != nil {
assert.Error(t, err)
return
}
assert.NoError(t, err)

// Since Go 1.16 'go list' command won't automatically update go.mod and go.sum.
Expand Down

0 comments on commit e5a7ca2

Please sign in to comment.