Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/event.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}

Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/go-test-for-sjad-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -31,4 +39,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
go test $(go list ./... | grep -v github.com/azure/azure-dev/cli/azd/test/functional) -cover -v
61 changes: 57 additions & 4 deletions cli/azd/internal/appdetect/pom.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,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)
Expand Down Expand Up @@ -269,7 +268,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.",
Expand All @@ -294,12 +293,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 := internal.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, 0600)
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)
Expand All @@ -313,8 +352,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)
}

Expand Down
8 changes: 8 additions & 0 deletions cli/azd/internal/appdetect/pom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package appdetect

import (
"context"
"log/slog"
"os"
os_exec "os/exec"
"path/filepath"
"reflect"
"strings"
Expand All @@ -13,6 +15,12 @@ import (
)

func TestCreateEffectivePom(t *testing.T) {
path, err := os_exec.LookPath("java")
if err != nil {
t.Skip("Skip TestCreateEffectivePom because java command doesn't exist.")
} else {
slog.Info("Java command found.", "path", path)
}
tests := []struct {
name string
testPoms []testPom
Expand Down
6 changes: 4 additions & 2 deletions cli/azd/internal/download_util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package internal

import (
"context"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"net/url"
"time"
Expand All @@ -23,14 +24,15 @@ 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
}
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)
Expand Down