From a85b80550f18a65f81778982d9287a90c9abca52 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Wed, 4 Dec 2024 19:46:19 +0900 Subject: [PATCH 1/2] fix: fix a bug that aqua g -i removes comments in packages --- .github/workflows/wc-integration-test.yaml | 7 ++++++ pkg/controller/generate/output/insert.go | 29 ++++++++++++---------- tests/insert/aqua.yaml | 10 ++++++++ tests/insert/expect.yaml | 13 ++++++++++ 4 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 tests/insert/aqua.yaml create mode 100644 tests/insert/expect.yaml diff --git a/.github/workflows/wc-integration-test.yaml b/.github/workflows/wc-integration-test.yaml index bdafdc887..7d1a359d6 100644 --- a/.github/workflows/wc-integration-test.yaml +++ b/.github/workflows/wc-integration-test.yaml @@ -84,6 +84,13 @@ jobs: - name: output fish completion run: aqua completion fish + - name: Test aqua g -i + run: aqua g -i suzuki-shunsuke/ghalint@v1.0.0 suzuki-shunsuke/ci-info@v2.3.1 suzuki-shunsuke/ghd2i@v0.1.2 + working-directory: tests/insert + - name: compare aqua.yaml and expect.yaml + run: diff aqua.yaml expect.yaml + working-directory: tests/insert + - run: aqua g -i suzuki-shunsuke/tfcmt working-directory: tests/main - name: add duplicated package diff --git a/pkg/controller/generate/output/insert.go b/pkg/controller/generate/output/insert.go index 893a28902..4a7741e2f 100644 --- a/pkg/controller/generate/output/insert.go +++ b/pkg/controller/generate/output/insert.go @@ -61,30 +61,33 @@ func (o *Outputter) getAppendedTxt(cfgFilePath string, file *ast.File, pkgs []*a return file.String(), nil } -func appendPkgsNode(mapValue *ast.MappingValueNode, node ast.Node) error { - switch mapValue.Value.Type() { +func updateASTFile(values *ast.MappingValueNode, pkgs []*aqua.Package) error { + node, err := yaml.ValueToNode(pkgs) + if err != nil { + return fmt.Errorf("convert packages to node: %w", err) + } + + switch values.Value.Type() { case ast.NullType: - mapValue.Value = node + values.Value = node return nil case ast.SequenceType: - if err := ast.Merge(mapValue.Value, node); err != nil { + if err := ast.Merge(values.Value, node); err != nil { return fmt.Errorf("merge packages: %w", err) } + seq, ok := values.Value.(*ast.SequenceNode) + if ok { + for range len(pkgs) { + // https://github.com/goccy/go-yaml/issues/502#issuecomment-2515981600 + seq.ValueHeadComments = append(seq.ValueHeadComments, nil) + } + } return nil default: return errors.New("packages must be null or array") } } -func updateASTFile(values *ast.MappingValueNode, pkgs []*aqua.Package) error { - node, err := yaml.ValueToNode(pkgs) - if err != nil { - return fmt.Errorf("convert packages to node: %w", err) - } - - return appendPkgsNode(values, node) -} - func (o *Outputter) appendPkgsTxt(cfgFilePath string, pkgs []*aqua.Package) (string, error) { a, err := yaml.Marshal(struct { Packages []*aqua.Package `yaml:"packages"` diff --git a/tests/insert/aqua.yaml b/tests/insert/aqua.yaml new file mode 100644 index 000000000..f180c0be2 --- /dev/null +++ b/tests/insert/aqua.yaml @@ -0,0 +1,10 @@ +registries: + - type: standard + ref: v4.267.0 # renovate: depName=aquaproj/aqua-registry + +packages: + # foo + - name: suzuki-shunsuke/mkghtag@v0.1.4 + # hello + - name: suzuki-shunsuke/tfcmt@v4.14.0 + - name: suzuki-shunsuke/pinact@v1.0.0 diff --git a/tests/insert/expect.yaml b/tests/insert/expect.yaml new file mode 100644 index 000000000..85ab775df --- /dev/null +++ b/tests/insert/expect.yaml @@ -0,0 +1,13 @@ +registries: + - type: standard + ref: v4.267.0 # renovate: depName=aquaproj/aqua-registry + +packages: + # foo + - name: suzuki-shunsuke/mkghtag@v0.1.4 + # hello + - name: suzuki-shunsuke/tfcmt@v4.14.0 + - name: suzuki-shunsuke/pinact@v1.0.0 + - name: suzuki-shunsuke/ghalint@v1.0.0 + - name: suzuki-shunsuke/ci-info@v2.3.1 + - name: suzuki-shunsuke/ghd2i@v0.1.2 From f23f2d94c263498e215185a9084461f8fda189b7 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Wed, 4 Dec 2024 19:51:41 +0900 Subject: [PATCH 2/2] fix: fix a lint error --- pkg/controller/generate/output/insert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/controller/generate/output/insert.go b/pkg/controller/generate/output/insert.go index 4a7741e2f..005543d58 100644 --- a/pkg/controller/generate/output/insert.go +++ b/pkg/controller/generate/output/insert.go @@ -77,7 +77,7 @@ func updateASTFile(values *ast.MappingValueNode, pkgs []*aqua.Package) error { } seq, ok := values.Value.(*ast.SequenceNode) if ok { - for range len(pkgs) { + for range pkgs { // https://github.com/goccy/go-yaml/issues/502#issuecomment-2515981600 seq.ValueHeadComments = append(seq.ValueHeadComments, nil) }