From 73c8ecf5d615b46519f103fbeada98dc9d526601 Mon Sep 17 00:00:00 2001 From: rujche Date: Fri, 27 Dec 2024 17:40:48 +0800 Subject: [PATCH 1/8] Skip TestCreateSimulatedEffectivePom only when java command not exist --- cli/azd/internal/appdetect/pom_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/azd/internal/appdetect/pom_test.go b/cli/azd/internal/appdetect/pom_test.go index 0b5c65e33dc..7f1c6f0491e 100644 --- a/cli/azd/internal/appdetect/pom_test.go +++ b/cli/azd/internal/appdetect/pom_test.go @@ -1053,8 +1053,8 @@ func TestAbsorbBuildPlugin(t *testing.T) { } func TestCreateSimulatedEffectivePom(t *testing.T) { - if os.Getenv("GITHUB_ACTIONS") == "true" { - t.Skip("Skip TestCreateSimulatedEffectivePom in GitHub Actions because it will time out.") + if !commandExistsInPath("java") { + t.Skip("Skip TestCreateSimulatedEffectivePom because java command not exist.") } var tests = []struct { name string From 98fe2b9502267847adf8e4eecf17febae6f830ab Mon Sep 17 00:00:00 2001 From: rujche Date: Fri, 27 Dec 2024 17:42:19 +0800 Subject: [PATCH 2/8] Add "-v" when run "go test". --- .github/workflows/go-test-for-sjad-branch.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go-test-for-sjad-branch.yml b/.github/workflows/go-test-for-sjad-branch.yml index bad4b06e15e..2b849a5ab41 100644 --- a/.github/workflows/go-test-for-sjad-branch.yml +++ b/.github/workflows/go-test-for-sjad-branch.yml @@ -31,4 +31,4 @@ jobs: - name: Run tests run: | cd ./cli/azd - go test $(go list ./... | grep -v github.com/azure/azure-dev/cli/azd/test/functional) -cover \ No newline at end of file + go test $(go list ./... | grep -v github.com/azure/azure-dev/cli/azd/test/functional) -cover -v From 43d2938985d04a318715b3825f7cb0736051fbb1 Mon Sep 17 00:00:00 2001 From: rujche Date: Fri, 27 Dec 2024 17:46:02 +0800 Subject: [PATCH 3/8] Fix type. --- cli/azd/internal/appdetect/pom_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/azd/internal/appdetect/pom_test.go b/cli/azd/internal/appdetect/pom_test.go index 7f1c6f0491e..6ffeeae806c 100644 --- a/cli/azd/internal/appdetect/pom_test.go +++ b/cli/azd/internal/appdetect/pom_test.go @@ -1054,7 +1054,7 @@ func TestAbsorbBuildPlugin(t *testing.T) { func TestCreateSimulatedEffectivePom(t *testing.T) { if !commandExistsInPath("java") { - t.Skip("Skip TestCreateSimulatedEffectivePom because java command not exist.") + t.Skip("Skip TestCreateSimulatedEffectivePom because java command doesn't exist.") } var tests = []struct { name string From 24b7dfc725f86e09f1fdd4c8b174ee4a29c7a436 Mon Sep 17 00:00:00 2001 From: rujche Date: Fri, 27 Dec 2024 18:02:47 +0800 Subject: [PATCH 4/8] Add more information about checking "java" command. --- cli/azd/internal/appdetect/pom_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cli/azd/internal/appdetect/pom_test.go b/cli/azd/internal/appdetect/pom_test.go index 6ffeeae806c..e89cbd9247e 100644 --- a/cli/azd/internal/appdetect/pom_test.go +++ b/cli/azd/internal/appdetect/pom_test.go @@ -1,7 +1,9 @@ package appdetect import ( + "log/slog" "os" + "os/exec" "path/filepath" "reflect" "strings" @@ -9,6 +11,12 @@ import ( ) func TestCreateEffectivePom(t *testing.T) { + if !commandExistsInPath("java") { + t.Skip("Skip TestCreateEffectivePom because java command doesn't exist.") + } else { + path, _ := exec.LookPath("java") + slog.Info("Java command found.", "path", path) + } tests := []struct { name string testPoms []testPom @@ -1055,6 +1063,9 @@ func TestAbsorbBuildPlugin(t *testing.T) { func TestCreateSimulatedEffectivePom(t *testing.T) { if !commandExistsInPath("java") { t.Skip("Skip TestCreateSimulatedEffectivePom because java command doesn't exist.") + } else { + path, _ := exec.LookPath("java") + slog.Info("Java command found.", "path", path) } var tests = []struct { name string From f397600a83197842d7d79252f34b54be97cfc346 Mon Sep 17 00:00:00 2001 From: rujche Date: Fri, 27 Dec 2024 20:49:10 +0800 Subject: [PATCH 5/8] Use local maven repository as cache. --- cli/azd/internal/appdetect/download_util.go | 6 +- cli/azd/internal/appdetect/pom.go | 61 +++++++++++++++++++-- cli/azd/internal/appdetect/pom_test.go | 4 +- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/cli/azd/internal/appdetect/download_util.go b/cli/azd/internal/appdetect/download_util.go index 894357eddb9..80faffc9f8b 100644 --- a/cli/azd/internal/appdetect/download_util.go +++ b/cli/azd/internal/appdetect/download_util.go @@ -1,9 +1,10 @@ package appdetect import ( + "context" "fmt" "io" - "log" + "log/slog" "net/http" "net/url" "time" @@ -23,6 +24,7 @@ func download(requestUrl string) ([]byte, error) { Proxy: http.ProxyFromEnvironment, }, } + slog.DebugContext(context.TODO(), "Downloading file.", "requestUrl", requestUrl, "err", err) resp, err := client.Get(requestUrl) if err != nil { return nil, err @@ -30,7 +32,7 @@ func download(requestUrl string) ([]byte, error) { defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { - log.Println("failed to close http response body") + slog.DebugContext(context.TODO(), "Failed to close http body.", "requestUrl", requestUrl, "err", err) } }(resp.Body) return io.ReadAll(resp.Body) diff --git a/cli/azd/internal/appdetect/pom.go b/cli/azd/internal/appdetect/pom.go index 6b9ceed5892..8acee36f893 100644 --- a/cli/azd/internal/appdetect/pom.go +++ b/cli/azd/internal/appdetect/pom.go @@ -212,8 +212,7 @@ func makePathFitCurrentOs(filePath string) string { func absorbInformationFromParentInRemoteMavenRepository(pom *pom) { p := pom.Parent - parent, err := getSimulatedEffectivePomFromRemoteMavenRepository( - p.GroupId, p.ArtifactId, p.Version) + parent, err := getSimulatedEffectivePomFromMavenRepository(p.GroupId, p.ArtifactId, p.Version) if err != nil { slog.InfoContext(context.TODO(), "Skip absorb parent from remote maven repository.", "ArtifactId", pom.ArtifactId, "err", err) @@ -267,7 +266,7 @@ func absorbImportedBomInDependencyManagement(pom *pom) { if dep.Scope != "import" { continue } - toBeAbsorbedPom, err := getSimulatedEffectivePomFromRemoteMavenRepository( + toBeAbsorbedPom, err := getSimulatedEffectivePomFromMavenRepository( dep.GroupId, dep.ArtifactId, dep.Version) if err != nil { slog.InfoContext(context.TODO(), "Skip absorb imported bom from remote maven repository.", @@ -292,12 +291,52 @@ func absorbDependencyManagement(pom *pom, dependencyManagementMap map[string]str } } +func getSimulatedEffectivePomFromMavenRepository(groupId string, artifactId string, version string) (pom, error) { + result, err := getSimulatedEffectivePomFromLocalMavenRepository(groupId, artifactId, version) + if err == nil { + return result, nil + } + return getSimulatedEffectivePomFromRemoteMavenRepository(groupId, artifactId, version) +} + +func getSimulatedEffectivePomFromLocalMavenRepository(groupId string, artifactId string, version string) (pom, error) { + pomPath, err := getPathInLocalMavenRepository(groupId, artifactId, version) + if err != nil { + return pom{}, err + } + return createSimulatedEffectivePom(pomPath) +} + func getSimulatedEffectivePomFromRemoteMavenRepository(groupId string, artifactId string, version string) (pom, error) { requestUrl := getRemoteMavenRepositoryUrl(groupId, artifactId, version) bytes, err := download(requestUrl) if err != nil { return pom{}, err } + savePomFileToLocalMavenRepository(groupId, artifactId, version, bytes) + return createSimulatedEffectivePomByPomFileBytes(bytes) +} + +func savePomFileToLocalMavenRepository(groupId string, artifactId string, version string, bytes []byte) { + pomPath, err := getPathInLocalMavenRepository(groupId, artifactId, version) + if err != nil { + slog.DebugContext(context.TODO(), "Failed to get pomPath.", + "groupId", groupId, "artifactId", artifactId, "version", version, "err", err) + return + } + dir := filepath.Dir(pomPath) + if err := os.MkdirAll(dir, 0755); err != nil { + slog.DebugContext(context.TODO(), "Failed to create pomPath.", + "groupId", groupId, "artifactId", artifactId, "version", version, "err", err) + return + } + err = os.WriteFile(pomPath, bytes, 0644) + if err != nil { + slog.DebugContext(context.TODO(), "Failed to write file.", "pomPath", pomPath, "err", err) + } +} + +func createSimulatedEffectivePomByPomFileBytes(bytes []byte) (pom, error) { var result pom if err := xml.Unmarshal(bytes, &result); err != nil { return pom{}, fmt.Errorf("parsing xml: %w", err) @@ -311,8 +350,22 @@ func getSimulatedEffectivePomFromRemoteMavenRepository(groupId string, artifactI return result, nil } +func getPathInLocalMavenRepository(groupId string, artifactId string, version string) (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + relativePath := makePathFitCurrentOs(relativePathInMavenRepository(groupId, artifactId, version)) + return filepath.Join(homeDir, ".m2", "repository", relativePath), nil +} + func getRemoteMavenRepositoryUrl(groupId string, artifactId string, version string) string { - return fmt.Sprintf("https://repo.maven.apache.org/maven2/%s/%s/%s/%s-%s.pom", + return fmt.Sprintf("https://repo.maven.apache.org/maven2/%s", + relativePathInMavenRepository(groupId, artifactId, version)) +} + +func relativePathInMavenRepository(groupId string, artifactId string, version string) string { + return fmt.Sprintf("%s/%s/%s/%s-%s.pom", strings.ReplaceAll(groupId, ".", "/"), artifactId, version, artifactId, version) } diff --git a/cli/azd/internal/appdetect/pom_test.go b/cli/azd/internal/appdetect/pom_test.go index e89cbd9247e..a0dde4e5f81 100644 --- a/cli/azd/internal/appdetect/pom_test.go +++ b/cli/azd/internal/appdetect/pom_test.go @@ -2183,11 +2183,11 @@ func TestCreateSimulatedEffectivePom(t *testing.T) { } for _, testPom := range tt.testPoms { pomFilePath := filepath.Join(workingDir, testPom.pomFilePath) - effectivePom, err := createEffectivePom(pomFilePath) + simulatedEffectivePom, err := createSimulatedEffectivePom(pomFilePath) if err != nil { t.Fatalf("%v", err) } - simulatedEffectivePom, err := createSimulatedEffectivePom(pomFilePath) + effectivePom, err := createEffectivePom(pomFilePath) if err != nil { t.Fatalf("%v", err) } From 65bf3f1122f96b9c51b254603745118c48f66fa4 Mon Sep 17 00:00:00 2001 From: rujche Date: Fri, 27 Dec 2024 20:50:46 +0800 Subject: [PATCH 6/8] Cache maven repository in GitHub action. --- .github/workflows/go-test-for-sjad-branch.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/go-test-for-sjad-branch.yml b/.github/workflows/go-test-for-sjad-branch.yml index 2b849a5ab41..c44aefe5b36 100644 --- a/.github/workflows/go-test-for-sjad-branch.yml +++ b/.github/workflows/go-test-for-sjad-branch.yml @@ -18,6 +18,14 @@ jobs: with: go-version: 1.23.1 + - name: Cache Maven repository + uses: actions/cache@v4 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + - name: Cache Go modules uses: actions/cache@v4 with: From 45289414e96b93a231122a0a703d1e6982b1af75 Mon Sep 17 00:00:00 2001 From: rujche Date: Fri, 27 Dec 2024 21:05:31 +0800 Subject: [PATCH 7/8] Fix lint error: Expect WriteFile permissions to be 0600 or less (gosec) --- cli/azd/internal/appdetect/pom.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/azd/internal/appdetect/pom.go b/cli/azd/internal/appdetect/pom.go index 8acee36f893..0a02f5bca75 100644 --- a/cli/azd/internal/appdetect/pom.go +++ b/cli/azd/internal/appdetect/pom.go @@ -330,7 +330,7 @@ func savePomFileToLocalMavenRepository(groupId string, artifactId string, versio "groupId", groupId, "artifactId", artifactId, "version", version, "err", err) return } - err = os.WriteFile(pomPath, bytes, 0644) + err = os.WriteFile(pomPath, bytes, 0600) if err != nil { slog.DebugContext(context.TODO(), "Failed to write file.", "pomPath", pomPath, "err", err) } From 1ef98742d0c02995ce21d5fef1d4b192e3fbf3d5 Mon Sep 17 00:00:00 2001 From: rujche Date: Fri, 27 Dec 2024 21:27:35 +0800 Subject: [PATCH 8/8] Just keep "cli-ci" in "Check Enforcer". --- .github/workflows/event.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/event.yml b/.github/workflows/event.yml index 601aa66568c..6479128b97f 100644 --- a/.github/workflows/event.yml +++ b/.github/workflows/event.yml @@ -12,7 +12,7 @@ on: # entirely of github actions won't trigger this action. workflow_run: types: [completed] - workflows: ["cli-ci", "templates-ci", "vscode-ci"] + workflows: ["cli-ci"] permissions: {}