From 9d7fe783dc92fdded0f9426d50696beea7b47b13 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Tue, 6 Jun 2023 23:52:07 +0300 Subject: [PATCH] feat(ci/lint): Allow printing merged yaml result --- commands/ci/lint/lint.go | 17 +++++++++++++++-- commands/ci/lint/lint_test.go | 36 +++++++++++++++++++++++++++++++++++ docs/source/ci/ci/lint.md | 1 + docs/source/ci/lint.md | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/commands/ci/lint/lint.go b/commands/ci/lint/lint.go index b083e6985..086718a91 100644 --- a/commands/ci/lint/lint.go +++ b/commands/ci/lint/lint.go @@ -19,6 +19,7 @@ var ( ref string dryRun bool includeJobs bool + printMerged bool ) func NewCmdLint(f *cmdutils.Factory) *cobra.Command { @@ -45,6 +46,7 @@ func NewCmdLint(f *cmdutils.Factory) *cobra.Command { pipelineCILintCmd.Flags().BoolVarP(&dryRun, "dry-run", "", false, "Run pipeline creation simulation.") pipelineCILintCmd.Flags().BoolVarP(&includeJobs, "include-jobs", "", false, "The response should include the list of jobs that would exist in a static check or pipeline simulation.") + pipelineCILintCmd.Flags().BoolVarP(&printMerged, "print-merged", "", false, "Print merged YAML result on success.") pipelineCILintCmd.Flags().StringVar(&ref, "ref", "", "When dry-run is true, sets the branch or tag context for validating the CI/CD YAML configuration.") return pipelineCILintCmd @@ -53,6 +55,10 @@ func NewCmdLint(f *cmdutils.Factory) *cobra.Command { func lintRun(f *cmdutils.Factory, path string) error { var err error out := f.IO.StdOut + if printMerged { + out = f.IO.StdErr + } + c := f.IO.Color() apiClient, err := f.HttpClient() @@ -95,7 +101,9 @@ func lintRun(f *cmdutils.Factory, path string) error { } } - fmt.Fprintln(f.IO.StdOut, "Validating...") + if !printMerged { + fmt.Fprintln(out, "Validating...") + } lint, err := api.ProjectNamespaceLint(apiClient, projectID, string(content), ref, dryRun, includeJobs) if err != nil { @@ -111,6 +119,11 @@ func lintRun(f *cmdutils.Factory, path string) error { // Returning the error to cobra here causes the process to exit with code 0, so we exit manually. os.Exit(1) } - fmt.Fprintln(out, c.GreenCheck(), "CI/CD YAML is valid!") + + if printMerged { + fmt.Fprint(f.IO.StdOut, lint.MergedYaml) + } else { + fmt.Fprintln(out, c.GreenCheck(), "CI/CD YAML is valid!") + } return nil } diff --git a/commands/ci/lint/lint_test.go b/commands/ci/lint/lint_test.go index 05fc35de8..95f98662c 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: "", + 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()) }) } } diff --git a/docs/source/ci/ci/lint.md b/docs/source/ci/ci/lint.md index 25a5e7b15..d025ba375 100644 --- a/docs/source/ci/ci/lint.md +++ b/docs/source/ci/ci/lint.md @@ -34,6 +34,7 @@ $ glab ci lint path/to/.gitlab-ci.yml ```plaintext --dry-run Run pipeline creation simulation. --include-jobs The response should include the list of jobs that would exist in a static check or pipeline simulation. + --print-merged Print merged YAML result on success. --ref string When dry-run is true, sets the branch or tag context for validating the CI/CD YAML configuration. ``` diff --git a/docs/source/ci/lint.md b/docs/source/ci/lint.md index 683f6cda5..5589f8dc6 100644 --- a/docs/source/ci/lint.md +++ b/docs/source/ci/lint.md @@ -34,6 +34,7 @@ $ glab ci lint path/to/.gitlab-ci.yml ```plaintext --dry-run Run pipeline creation simulation. --include-jobs The response should include the list of jobs that would exist in a static check or pipeline simulation. + --print-merged Print merged YAML result on success. --ref string When dry-run is true, sets the branch or tag context for validating the CI/CD YAML configuration. ``` -- GitLab