...

Source file src/github.com/xanzy/go-gitlab/validate.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  )
    23  
    24  // ValidateService handles communication with the validation related methods of
    25  // the GitLab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/lint.html
    28  type ValidateService struct {
    29  	client *Client
    30  }
    31  
    32  // LintResult represents the linting results.
    33  //
    34  // GitLab API docs: https://docs.gitlab.com/ee/api/lint.html
    35  type LintResult struct {
    36  	Status     string   `json:"status"`
    37  	Errors     []string `json:"errors"`
    38  	Warnings   []string `json:"warnings"`
    39  	MergedYaml string   `json:"merged_yaml"`
    40  }
    41  
    42  // ProjectLintResult represents the linting results by project.
    43  //
    44  // GitLab API docs:
    45  // https://docs.gitlab.com/ee/api/lint.html#validate-a-projects-ci-configuration
    46  type ProjectLintResult struct {
    47  	Valid      bool     `json:"valid"`
    48  	Errors     []string `json:"errors"`
    49  	Warnings   []string `json:"warnings"`
    50  	MergedYaml string   `json:"merged_yaml"`
    51  }
    52  
    53  // LintOptions represents the available Lint() options.
    54  //
    55  // Gitlab API docs:
    56  // https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration
    57  type LintOptions struct {
    58  	Content           string `url:"content,omitempty" json:"content,omitempty"`
    59  	IncludeMergedYAML bool   `url:"include_merged_yaml,omitempty" json:"include_merged_yaml,omitempty"`
    60  	IncludeJobs       bool   `url:"include_jobs,omitempty" json:"include_jobs,omitempty"`
    61  }
    62  
    63  // Lint validates .gitlab-ci.yml content.
    64  // Deprecated: This endpoint was removed in GitLab 16.0.
    65  //
    66  // Gitlab API docs:
    67  // https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration-deprecated
    68  func (s *ValidateService) Lint(opts *LintOptions, options ...RequestOptionFunc) (*LintResult, *Response, error) {
    69  	req, err := s.client.NewRequest(http.MethodPost, "ci/lint", &opts, options)
    70  	if err != nil {
    71  		return nil, nil, err
    72  	}
    73  
    74  	l := new(LintResult)
    75  	resp, err := s.client.Do(req, l)
    76  	if err != nil {
    77  		return nil, resp, err
    78  	}
    79  
    80  	return l, resp, nil
    81  }
    82  
    83  // ProjectNamespaceLintOptions represents the available ProjectNamespaceLint() options.
    84  //
    85  // GitLab API docs:
    86  // https://docs.gitlab.com/ee/api/lint.html#validate-a-ci-yaml-configuration-with-a-namespace
    87  type ProjectNamespaceLintOptions struct {
    88  	Content     *string `url:"content,omitempty" json:"content,omitempty"`
    89  	DryRun      *bool   `url:"dry_run,omitempty" json:"dry_run,omitempty"`
    90  	IncludeJobs *bool   `url:"include_jobs,omitempty" json:"include_jobs,omitempty"`
    91  	Ref         *string `url:"ref,omitempty" json:"ref,omitempty"`
    92  }
    93  
    94  // ProjectNamespaceLint validates .gitlab-ci.yml content by project.
    95  //
    96  // GitLab API docs:
    97  // https://docs.gitlab.com/ee/api/lint.html#validate-a-ci-yaml-configuration-with-a-namespace
    98  func (s *ValidateService) ProjectNamespaceLint(pid interface{}, opt *ProjectNamespaceLintOptions, options ...RequestOptionFunc) (*ProjectLintResult, *Response, error) {
    99  	project, err := parseID(pid)
   100  	if err != nil {
   101  		return nil, nil, err
   102  	}
   103  	u := fmt.Sprintf("projects/%s/ci/lint", PathEscape(project))
   104  
   105  	req, err := s.client.NewRequest(http.MethodPost, u, &opt, options)
   106  	if err != nil {
   107  		return nil, nil, err
   108  	}
   109  
   110  	l := new(ProjectLintResult)
   111  	resp, err := s.client.Do(req, l)
   112  	if err != nil {
   113  		return nil, resp, err
   114  	}
   115  
   116  	return l, resp, nil
   117  }
   118  
   119  // ProjectLintOptions represents the available ProjectLint() options.
   120  //
   121  // GitLab API docs:
   122  // https://docs.gitlab.com/ee/api/lint.html#validate-a-projects-ci-configuration
   123  type ProjectLintOptions struct {
   124  	ContentRef  *string `url:"content_ref,omitempty" json:"content_ref,omitempty"`
   125  	DryRunRef   *string `url:"dry_run_ref,omitempty" json:"dry_run_ref,omitempty"`
   126  	DryRun      *bool   `url:"dry_run,omitempty" json:"dry_run,omitempty"`
   127  	IncludeJobs *bool   `url:"include_jobs,omitempty" json:"include_jobs,omitempty"`
   128  	Ref         *string `url:"ref,omitempty" json:"ref,omitempty"`
   129  }
   130  
   131  // ProjectLint validates .gitlab-ci.yml content by project.
   132  //
   133  // GitLab API docs:
   134  // https://docs.gitlab.com/ee/api/lint.html#validate-a-projects-ci-configuration
   135  func (s *ValidateService) ProjectLint(pid interface{}, opt *ProjectLintOptions, options ...RequestOptionFunc) (*ProjectLintResult, *Response, error) {
   136  	project, err := parseID(pid)
   137  	if err != nil {
   138  		return nil, nil, err
   139  	}
   140  	u := fmt.Sprintf("projects/%s/ci/lint", PathEscape(project))
   141  
   142  	req, err := s.client.NewRequest(http.MethodGet, u, &opt, options)
   143  	if err != nil {
   144  		return nil, nil, err
   145  	}
   146  
   147  	l := new(ProjectLintResult)
   148  	resp, err := s.client.Do(req, l)
   149  	if err != nil {
   150  		return nil, resp, err
   151  	}
   152  
   153  	return l, resp, nil
   154  }
   155  

View as plain text