From 959be4f5a3316c8951211fe84899a68ac4d000dd Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 21 Nov 2024 13:58:55 +0100 Subject: [PATCH] Change name to DEBRICKED_VERSION --- .github/workflows/test.yml | 3 +- .../callgraph/language/java/soot_handler.go | 27 ++++++--- .../language/java/soot_handler_test.go | 55 +++++++++++++++++++ internal/cmd/callgraph/callgraph.go | 2 +- internal/cmd/root/root.go | 1 + internal/cmd/root/root_test.go | 2 +- scripts/install.sh | 2 +- 7 files changed, 79 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 80232ef3..0d8932fc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -92,7 +92,8 @@ jobs: distribution: 'temurin' - name: Install Debricked CLI - run: go install -ldflags "-X main.version=${GITHUB_REF#refs/heads/}" ./cmd/debricked + run: | + go install -ldflags "-X main.version=${{ secrets.DEBRICKED_VERSION }}" ./cmd/debricked - name: Callgraph E2E run: ./scripts/test_e2e_callgraph_java_version.sh ${{matrix.java}} diff --git a/internal/callgraph/language/java/soot_handler.go b/internal/callgraph/language/java/soot_handler.go index be786a0f..e611a1c3 100644 --- a/internal/callgraph/language/java/soot_handler.go +++ b/internal/callgraph/language/java/soot_handler.go @@ -78,6 +78,7 @@ func (sh SootHandler) downloadCompressedSootWrapper(fs ioFs.IFileSystem, zipFile version, ".zip", }, "") + fmt.Println("URL=", fullURLFile) client := http.Client{ CheckRedirect: func(r *http.Request, via []*http.Request) error { @@ -101,10 +102,11 @@ func (sh SootHandler) downloadCompressedSootWrapper(fs ioFs.IFileSystem, zipFile func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) { versionInt, err := strconv.Atoi(version) if err != nil { - return "", fmt.Errorf("error when trying to convert java version string to int") + return "", fmt.Errorf("could not convert version to int") } - if versionInt < 11 { - return "", fmt.Errorf("lowest supported version for running callgraph generation is 11") + version, err = sh.getSootHandlerJavaVersion(versionInt) + if err != nil { + return "", err } debrickedDir := ".debricked" if _, err := fs.Stat(debrickedDir); fs.IsNotExist(err) { @@ -119,17 +121,24 @@ func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc io return "", err } if _, err := fs.Stat(path); fs.IsNotExist(err) { - if versionInt >= 21 { + if version == "21" { return sh.initializeSootWrapper(fs, debrickedDir) } - if versionInt >= 17 { - version = "17" - } else { - version = "11" - } // Handling correct jar to install return path, sh.downloadSootWrapper(arc, fs, path, version) } return path, nil } + +func (sh SootHandler) getSootHandlerJavaVersion(version int) (string, error) { + if version >= 21 { + return "21", nil + } else if version >= 17 { + return "17", nil + } else if version >= 11 { + return "11", nil + } else { + return "", fmt.Errorf("lowest supported version for running callgraph generation is 11") + } +} diff --git a/internal/callgraph/language/java/soot_handler_test.go b/internal/callgraph/language/java/soot_handler_test.go index 0d925a03..3c7605a5 100644 --- a/internal/callgraph/language/java/soot_handler_test.go +++ b/internal/callgraph/language/java/soot_handler_test.go @@ -173,3 +173,58 @@ func TestGetSootWrapperMkdirError(t *testing.T) { assert.Error(t, err) assert.Equal(t, err.Error(), errString) } + +func TestGetSootHandlerJavaVersion(t *testing.T) { + sootHandler := SootHandler{} + tests := []struct { + name string + version int + expectedVersion string + expectError bool + }{ + { + name: "Unsupported version", + version: 8, + expectedVersion: "", + expectError: true, + }, + { + name: "Version 11", + version: 11, + expectedVersion: "11", + expectError: false, + }, + { + name: "Version 17", + version: 17, + expectedVersion: "17", + expectError: false, + }, + { + name: "Version 21", + version: 21, + expectedVersion: "21", + expectError: false, + }, + { + name: "Version not int", + version: 12, + expectedVersion: "11", + expectError: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result, err := sootHandler.getSootHandlerJavaVersion(tt.version) + + if tt.expectError { + assert.Error(t, err) + assert.Empty(t, result) + } else { + assert.NoError(t, err) + assert.NotEmpty(t, result) + } + }) + } +} diff --git a/internal/cmd/callgraph/callgraph.go b/internal/cmd/callgraph/callgraph.go index 1438717f..aab282cf 100644 --- a/internal/cmd/callgraph/callgraph.go +++ b/internal/cmd/callgraph/callgraph.go @@ -127,7 +127,7 @@ func RunE(callgraph callgraph.IGenerator) func(_ *cobra.Command, args []string) } configs := []conf.IConfig{} - version := viper.GetString("version") + version := viper.GetString("cliVersion") for _, language := range languages { configs = append(configs, conf.NewConfig(language, args, map[string]string{}, !buildDisabled, languageMap[language], version)) diff --git a/internal/cmd/root/root.go b/internal/cmd/root/root.go index d51ad765..a917032c 100644 --- a/internal/cmd/root/root.go +++ b/internal/cmd/root/root.go @@ -29,6 +29,7 @@ Complete documentation is available at https://docs.debricked.com/tools-and-inte }, Version: version, } + viper.Set("cliVersion", version) viper.SetEnvPrefix("DEBRICKED") viper.AutomaticEnv() viper.MustBindEnv(AccessTokenFlag) diff --git a/internal/cmd/root/root_test.go b/internal/cmd/root/root_test.go index 5541b033..7f576af2 100644 --- a/internal/cmd/root/root_test.go +++ b/internal/cmd/root/root_test.go @@ -35,7 +35,7 @@ func TestNewRootCmd(t *testing.T) { } } assert.Truef(t, match, "failed to assert that flag was present: "+OldAccessTokenFlag) - assert.Len(t, viperKeys, 22) + assert.Len(t, viperKeys, 23) } func TestPreRun(t *testing.T) { diff --git a/scripts/install.sh b/scripts/install.sh index 62d78a48..ee228b29 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -4,7 +4,7 @@ if ! command -v git &> /dev/null then echo -e "Failed to find git, thus also the version. Version will be set to v0.0.0" fi -version=${VERSION:-$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)} +version=${DEBRICKED_VERSION:-$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)} ldFlags="-X main.version=${version}" go install -ldflags "${ldFlags}" ./cmd/debricked go generate -v -x ./cmd/debricked