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

Support azd hooks out of azure.yaml, Part I of merging azd operation to hooks #4244

Merged
merged 19 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4d59c6d
service hooks
vhvb1989 Aug 23, 2024
fc2b714
Merge branch 'main' of github.com:Azure/azure-dev into merge-azd-oper…
vhvb1989 Aug 23, 2024
3c3b954
As part of merging azd operation and hooks, this PR allows users to d…
vhvb1989 Aug 23, 2024
9a561f5
test new function
vhvb1989 Aug 24, 2024
704c5d1
Merge branch 'main' of github.com:Azure/azure-dev into merge-azd-oper…
vhvb1989 Aug 24, 2024
0c6cdf4
Merge branch 'main' of github.com:Azure/azure-dev into merge-azd-oper…
vhvb1989 Aug 26, 2024
a65c3b4
lint
vhvb1989 Aug 26, 2024
8605e02
Merge branch 'main' of github.com:Azure/azure-dev into merge-azd-oper…
vhvb1989 Aug 29, 2024
af3da51
wait here
vhvb1989 Aug 29, 2024
99ca56a
Merge branch 'main' of github.com:Azure/azure-dev into merge-azd-oper…
vhvb1989 Aug 30, 2024
a292068
User project to parse hooks from external files
vhvb1989 Aug 30, 2024
1178156
ignore all stat errors as a way to handle Aspire projects
vhvb1989 Aug 30, 2024
deae098
Merge branch 'main' of github.com:Azure/azure-dev into merge-azd-oper…
vhvb1989 Sep 5, 2024
5b2ae88
Merge branch 'main' of github.com:Azure/azure-dev into merge-azd-oper…
vhvb1989 Sep 6, 2024
f0f2b18
Merge branch 'main' of github.com:Azure/azure-dev into merge-azd-oper…
vhvb1989 Nov 13, 2024
6fa578b
external project hooks only
vhvb1989 Nov 13, 2024
4e4b81e
use module name to define hooks
vhvb1989 Nov 14, 2024
20f1b2a
ignore missing file
vhvb1989 Nov 14, 2024
06b2918
name update
vhvb1989 Nov 14, 2024
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
53 changes: 51 additions & 2 deletions cli/azd/pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func Parse(ctx context.Context, yamlContent string) (*ProjectConfig, error) {
projectConfig.Infra.Path = "infra"
}

if projectConfig.Infra.Module == "" {
projectConfig.Infra.Module = DefaultModule
}

if strings.Contains(projectConfig.Infra.Path, "\\") && !strings.Contains(projectConfig.Infra.Path, "/") {
projectConfig.Infra.Path = strings.ReplaceAll(projectConfig.Infra.Path, "\\", "/")
}
Expand Down Expand Up @@ -154,6 +158,31 @@ func Load(ctx context.Context, projectFilePath string) (*ProjectConfig, error) {
return nil, fmt.Errorf("parsing project file: %w", err)
}

projectConfig.Path = filepath.Dir(projectFilePath)

// complement the project config with hooks defined in the infra path using the syntax `<moduleName>.hooks.yaml`
// for example `main.hooks.yaml`
hooksDefinedAtInfraPath, err := hooksFromInfraModule(
filepath.Join(projectConfig.Path, projectConfig.Infra.Path),
projectConfig.Infra.Module)
if err != nil {
return nil, fmt.Errorf("failed getting hooks from infra path, %w", err)
}
// Merge the hooks defined at the infra path with the hooks defined in the project configuration
if len(hooksDefinedAtInfraPath) > 0 && projectConfig.Hooks == nil {
projectConfig.Hooks = make(map[string][]*ext.HookConfig)
}
for hookName, externalHookList := range hooksDefinedAtInfraPath {
if hookListFromAzureYaml, hookExists := projectConfig.Hooks[hookName]; hookExists {
mergedHooks := make([]*ext.HookConfig, 0, len(hookListFromAzureYaml)+len(externalHookList))
mergedHooks = append(mergedHooks, hookListFromAzureYaml...)
mergedHooks = append(mergedHooks, externalHookList...)
projectConfig.Hooks[hookName] = mergedHooks
continue
}
projectConfig.Hooks[hookName] = externalHookList
}

if projectConfig.Metadata != nil && projectConfig.Metadata.Template != "" {
template := strings.Split(projectConfig.Metadata.Template, "@")
if len(template) == 1 { // no version specifier, just the template ID
Expand Down Expand Up @@ -187,7 +216,6 @@ func Load(ctx context.Context, projectFilePath string) (*ProjectConfig, error) {
tracing.SetUsageAttributes(fields.ProjectServiceHostsKey.StringSlice(hosts))
}

projectConfig.Path = filepath.Dir(projectFilePath)
return projectConfig, nil
}

Expand Down Expand Up @@ -271,7 +299,28 @@ func Save(ctx context.Context, projectConfig *ProjectConfig, projectFilePath str
return fmt.Errorf("saving project file: %w", err)
}

projectConfig.Path = projectFilePath
projectConfig.Path = filepath.Dir(projectFilePath)

return nil
}

// hooksFromInfraModule check if there is file named azd.hooks.yaml in the service path
// and return the hooks configuration.
func hooksFromInfraModule(infraPath, moduleName string) (HooksConfig, error) {
hooksPath := filepath.Join(infraPath, moduleName+".hooks.yaml")
if _, err := os.Stat(hooksPath); os.IsNotExist(err) {
return nil, nil
}
hooksFile, err := os.ReadFile(hooksPath)
if err != nil {
return nil, fmt.Errorf("failed reading hooks from '%s', %w", hooksPath, err)
}

// open hooksPath into a byte array and unmarshal it into a map[string]*ext.HookConfig
hooks := make(HooksConfig)
if err := yaml.Unmarshal(hooksFile, &hooks); err != nil {
return nil, fmt.Errorf("failed unmarshalling hooks from '%s', %w", hooksPath, err)
}

return hooks, nil
}
132 changes: 132 additions & 0 deletions cli/azd/pkg/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package project

import (
"context"
"os"
"path/filepath"
"testing"

Expand All @@ -13,7 +14,9 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/azapi"
"github.com/azure/azure-dev/cli/azd/pkg/azure"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/ext"
"github.com/azure/azure-dev/cli/azd/pkg/infra"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockarmresources"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockazcli"
Expand Down Expand Up @@ -342,3 +345,132 @@ services:
assert.Equal(t, filepath.FromSlash("src/api"), projectConfig.Services["api"].RelativePath)
assert.Equal(t, filepath.FromSlash("bin/api"), projectConfig.Services["api"].OutputPath)
}

func Test_HooksFromFolderPath(t *testing.T) {
t.Run("ProjectInfraHooks", func(t *testing.T) {
prj := &ProjectConfig{
Name: "minimal",
Services: map[string]*ServiceConfig{},
}
contents, err := yaml.Marshal(prj)
require.NoError(t, err)

tempDir := t.TempDir()

azureYamlPath := filepath.Join(tempDir, "azure.yaml")
err = os.WriteFile(azureYamlPath, contents, osutil.PermissionDirectory)
require.NoError(t, err)

infraPath := filepath.Join(tempDir, "infra")
err = os.Mkdir(infraPath, osutil.PermissionDirectory)
require.NoError(t, err)

hooksPath := filepath.Join(infraPath, "main.hooks.yaml")
hooksContent := []byte(`
prebuild:
shell: sh
run: ./pre-build.sh
postbuild:
shell: pwsh
run: ./post-build.ps1
`)

err = os.WriteFile(hooksPath, hooksContent, osutil.PermissionDirectory)
require.NoError(t, err)

expectedHooks := HooksConfig{
"prebuild": {{
Name: "",
Shell: ext.ShellTypeBash,
Run: "./pre-build.sh",
ContinueOnError: false,
Interactive: false,
Windows: nil,
Posix: nil,
}},
"postbuild": {{
Name: "",
Shell: ext.ShellTypePowershell,
Run: "./post-build.ps1",
ContinueOnError: false,
Interactive: false,
Windows: nil,
Posix: nil,
},
}}

project, err := Load(context.Background(), azureYamlPath)
require.NoError(t, err)
require.Equal(t, expectedHooks, project.Hooks)
})

t.Run("DoubleDefintionHooks", func(t *testing.T) {
prj := &ProjectConfig{
Name: "minimal",
Services: map[string]*ServiceConfig{},
Hooks: HooksConfig{
"prebuild": {{
Shell: ext.ShellTypeBash,
Run: "./pre-build.sh",
}},
},
}
contents, err := yaml.Marshal(prj)
require.NoError(t, err)

tempDir := t.TempDir()

azureYamlPath := filepath.Join(tempDir, "azure.yaml")
err = os.WriteFile(azureYamlPath, contents, osutil.PermissionDirectory)
require.NoError(t, err)

infraPath := filepath.Join(tempDir, "infra")
err = os.Mkdir(infraPath, osutil.PermissionDirectory)
require.NoError(t, err)

hooksPath := filepath.Join(infraPath, "main.hooks.yaml")
hooksContent := []byte(`
prebuild:
shell: sh
run: ./pre-build-external.sh
postbuild:
shell: pwsh
run: ./post-build.ps1
`)

err = os.WriteFile(hooksPath, hooksContent, osutil.PermissionDirectory)
require.NoError(t, err)

project, err := Load(context.Background(), azureYamlPath)
require.NoError(t, err)
expectedHooks := HooksConfig{
"prebuild": {{
Name: "",
Shell: ext.ShellTypeBash,
Run: "./pre-build.sh",
ContinueOnError: false,
Interactive: false,
Windows: nil,
Posix: nil,
}, {
Name: "",
Shell: ext.ShellTypeBash,
Run: "./pre-build-external.sh",
ContinueOnError: false,
Interactive: false,
Windows: nil,
Posix: nil,
}},
"postbuild": {{
Name: "",
Shell: ext.ShellTypePowershell,
Run: "./post-build.ps1",
ContinueOnError: false,
Interactive: false,
Windows: nil,
Posix: nil,
}},
}
require.Equal(t, expectedHooks, project.Hooks)
})
}
Loading