From afed20cdac1fb4e96df58901a0f8b7699f87bb48 Mon Sep 17 00:00:00 2001 From: Jan Wolfensberger Date: Tue, 3 Dec 2024 12:57:14 +0100 Subject: [PATCH] Extend project lint result struct with includes The Gitlab API exposes a list of (recursive) includes when linting a projects ci configuration. The ProjectLintResult struct representing the API response does not reflect this list, so a user of the SDK cannot use this information from the API. --- validate.go | 24 +++++++++++++++---- validate_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 6 deletions(-) diff --git a/validate.go b/validate.go index cb79ac838..f4aa11f47 100644 --- a/validate.go +++ b/validate.go @@ -44,10 +44,26 @@ type LintResult struct { // GitLab API docs: // https://docs.gitlab.com/ee/api/lint.html#validate-a-projects-ci-configuration type ProjectLintResult struct { - Valid bool `json:"valid"` - Errors []string `json:"errors"` - Warnings []string `json:"warnings"` - MergedYaml string `json:"merged_yaml"` + Valid bool `json:"valid"` + Errors []string `json:"errors"` + Warnings []string `json:"warnings"` + MergedYaml string `json:"merged_yaml"` + Includes []Include `json:"includes"` +} + +// Include contains the details about an include block in the .gitlab-ci.yml file. +// It is used in ProjectLintResult. +// +// Reference can be found at the lint API endpoint in the openapi yaml: +// https://gitlab.com/gitlab-org/gitlab/-/blob/master/doc/api/openapi/openapi_v2.yaml +type Include struct { + Type string `json:"type"` + Location string `json:"location"` + Blob string `json:"blob"` + Raw string `json:"raw"` + Extra map[string]interface{} `json:"extra"` + ContextProject string `json:"context_project"` + ContextSHA string `json:"context_sha"` } // LintOptions represents the available Lint() options. diff --git a/validate_test.go b/validate_test.go index 20171e24f..81fc5d9e2 100644 --- a/validate_test.go +++ b/validate_test.go @@ -172,13 +172,41 @@ func TestValidateProjectNamespace(t *testing.T) { "valid": true, "errors": [], "warnings": [], - "merged_yaml": "---\n:build:\n :script:\n - echo build" + "merged_yaml": "---\n:build:\n :script:\n - echo build", + "includes": [ + { + "type": "file", + "location": "template/pipeline.yml", + "blob": "https://gitlab.com/namespace/project/-/blob/abcd1234/template/pipeline.yml", + "raw": "https://gitlab.com/namespace/project/-/raw/abcd1234/template/pipeline.yml", + "extra": { + "project": "namespace/project", + "ref": "1.2.3" + }, + "context_project": "namespace/current-project", + "context_sha": "abcd1234" + } + ] }`, want: &ProjectLintResult{ Valid: true, Warnings: []string{}, Errors: []string{}, MergedYaml: "---\n:build:\n :script:\n - echo build", + Includes: []Include{ + { + Type: "file", + Location: "template/pipeline.yml", + Blob: "https://gitlab.com/namespace/project/-/blob/abcd1234/template/pipeline.yml", + Raw: "https://gitlab.com/namespace/project/-/raw/abcd1234/template/pipeline.yml", + Extra: map[string]interface{}{ + "project": "namespace/project", + "ref": "1.2.3", + }, + ContextProject: "namespace/current-project", + ContextSHA: "abcd1234", + }, + }, }, }, { @@ -242,13 +270,41 @@ func TestValidateProjectLint(t *testing.T) { "valid": true, "errors": [], "warnings": [], - "merged_yaml": "---\n:build:\n :script:\n - echo build" + "merged_yaml": "---\n:build:\n :script:\n - echo build", + "includes": [ + { + "type": "file", + "location": "template/pipeline.yml", + "blob": "https://gitlab.com/namespace/project/-/blob/abcd1234/template/pipeline.yml", + "raw": "https://gitlab.com/namespace/project/-/raw/abcd1234/template/pipeline.yml", + "extra": { + "project": "namespace/project", + "ref": "1.2.3" + }, + "context_project": "namespace/current-project", + "context_sha": "abcd1234" + } + ] }`, want: &ProjectLintResult{ Valid: true, Warnings: []string{}, Errors: []string{}, MergedYaml: "---\n:build:\n :script:\n - echo build", + Includes: []Include{ + { + Type: "file", + Location: "template/pipeline.yml", + Blob: "https://gitlab.com/namespace/project/-/blob/abcd1234/template/pipeline.yml", + Raw: "https://gitlab.com/namespace/project/-/raw/abcd1234/template/pipeline.yml", + Extra: map[string]interface{}{ + "project": "namespace/project", + "ref": "1.2.3", + }, + ContextProject: "namespace/current-project", + ContextSHA: "abcd1234", + }, + }, }, }, }