From d3c3c141dca268ce41e594296000114d9252e19e Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Tue, 6 Jun 2023 23:52:07 +0300 Subject: [PATCH] Allow printing merged yaml result on ci lint --- commands/ci/lint/lint.go | 22 +++++++++++++++++---- commands/ci/lint/lint_test.go | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/commands/ci/lint/lint.go b/commands/ci/lint/lint.go index 719eb8ebd..e0208071b 100644 --- a/commands/ci/lint/lint.go +++ b/commands/ci/lint/lint.go @@ -15,7 +15,13 @@ import ( "github.com/spf13/cobra" ) +type LintOptions struct { + PrintMerged bool +} + func NewCmdLint(f *cmdutils.Factory) *cobra.Command { + opts := &LintOptions{} + pipelineCILintCmd := &cobra.Command{ Use: "lint", Short: "Checks if your `.gitlab-ci.yml` file is valid.", @@ -33,16 +39,20 @@ func NewCmdLint(f *cmdutils.Factory) *cobra.Command { if len(args) == 1 { path = args[0] } - return lintRun(f, path) + return lintRun(f, opts, path) }, } - + pipelineCILintCmd.Flags().BoolVarP(&opts.PrintMerged, "print-merged", "", false, "Print merged YAML result on success") return pipelineCILintCmd } -func lintRun(f *cmdutils.Factory, path string) error { +func lintRun(f *cmdutils.Factory, opts *LintOptions, path string) error { var err error out := f.IO.StdOut + if(opts.PrintMerged) { + // print regular messages to stderr so that yaml output can be piped pristine + out = f.IO.StdErr + } c := f.IO.Color() apiClient, err := f.HttpClient() @@ -85,7 +95,7 @@ func lintRun(f *cmdutils.Factory, path string) error { } } - fmt.Fprintln(f.IO.StdOut, "Validating...") + fmt.Fprintln(out, "Validating...") lint, err := api.ProjectNamespaceLint(apiClient, projectID, string(content)) if err != nil { @@ -102,5 +112,9 @@ func lintRun(f *cmdutils.Factory, path string) error { os.Exit(1) } fmt.Fprintln(out, c.GreenCheck(), "CI/CD YAML is valid!") + + if(opts.PrintMerged) { + fmt.Fprint(f.IO.StdOut, lint.MergedYaml) + } return nil } diff --git a/commands/ci/lint/lint_test.go b/commands/ci/lint/lint_test.go index c268ec46d..bcf6095bb 100644 --- a/commands/ci/lint/lint_test.go +++ b/commands/ci/lint/lint_test.go @@ -27,7 +27,9 @@ func Test_lintRun(t *testing.T) { tests := []struct { name string testFile string + args string StdOut string + StdErr string wantErr bool errMsg string httpMocks []httpMock @@ -88,6 +90,36 @@ func Test_lintRun(t *testing.T) { }, }, }, + { + name: "when a valid path is specified and yaml is valid and --print-merged arg", + testFile: ".gitlab-ci.yaml", + args: "--print-merged", + StdErr: "Validating...\n✓ CI/CD YAML is valid!\n", + StdOut: "---\nsome: result", + wantErr: false, + errMsg: "", + showHaveBaseRepo: true, + httpMocks: []httpMock{ + { + http.MethodGet, + "/api/v4/projects/OWNER/REPO", + http.StatusOK, + `{ + "id": 123, + "iid": 123 + }`, + }, + { + http.MethodPost, + "/api/v4/projects/123/ci/lint", + http.StatusOK, + `{ + "valid": true, + "merged_yaml": "---\nsome: result" + }`, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -100,6 +132,9 @@ func Test_lintRun(t *testing.T) { _, filename, _, _ := runtime.Caller(0) args := path.Join(path.Dir(filename), "testdata", tt.testFile) + if(tt.args != ""){ + args = fmt.Sprintf("%s %s", args, tt.args) + } result, err := runCommand(t, fakeHTTP, false, args, tt.showHaveBaseRepo) if tt.wantErr { @@ -109,6 +144,7 @@ func Test_lintRun(t *testing.T) { require.NoError(t, err) assert.Equal(t, tt.StdOut, result.String()) + assert.Equal(t, tt.StdErr, result.Stderr()) }) } } -- GitLab