Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Cleanup PR
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed Jun 20, 2022
1 parent 0f14fde commit df03464
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 96 deletions.
5 changes: 4 additions & 1 deletion jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ type Bridge struct {
DownstreamPipeline *PipelineInfo `json:"downstream_pipeline"`
}

// ListJobsOptions are options for two list apis
// ListJobsOptions represents the available ListProjectJobs() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/jobs.html#list-project-jobs
type ListJobsOptions struct {
ListOptions
Scope *[]BuildStateValue `url:"scope[],omitempty" json:"scope,omitempty"`
Expand Down
43 changes: 11 additions & 32 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ type ValidateService struct {
client *Client
}

// LintOptions represent options for the lint API
//
// Gitlab API docs: https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration
type LintOptions struct {
Content string `url:"content,omitempty" json:"content,omitempty"`
IncludeMergedYAML bool `url:"include_merged_yaml,omitempty" json:"include_merged_yaml,omitempty"`
IncludeJobs bool `url:"include_jobs,omitempty" json:"include_jobs,omitempty"`
}

// LintResult represents the linting results.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/lint.html
Expand All @@ -59,33 +50,21 @@ type ProjectLintResult struct {
MergedYaml string `json:"merged_yaml"`
}

// Lint validates .gitlab-ci.yml content.
// LintOptions represents the available Lint() options.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/lint.html
func (s *ValidateService) Lint(content string, options ...RequestOptionFunc) (*LintResult, *Response, error) {
var opts struct {
Content string `url:"content,omitempty" json:"content,omitempty"`
}
opts.Content = content

req, err := s.client.NewRequest(http.MethodPost, "ci/lint", &opts, options)
if err != nil {
return nil, nil, err
}

l := new(LintResult)
resp, err := s.client.Do(req, l)
if err != nil {
return nil, resp, err
}

return l, resp, nil
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration
type LintOptions struct {
Content string `url:"content,omitempty" json:"content,omitempty"`
IncludeMergedYAML bool `url:"include_merged_yaml,omitempty" json:"include_merged_yaml,omitempty"`
IncludeJobs bool `url:"include_jobs,omitempty" json:"include_jobs,omitempty"`
}

// LintWithOptions validates .gitlab-ci.yml content with API Options
// Lint validates .gitlab-ci.yml content.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/lint.html
func (s *ValidateService) LintWithOptions(opts *LintOptions, options ...RequestOptionFunc) (*LintResult, *Response, error) {
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration
func (s *ValidateService) Lint(opts *LintOptions, options ...RequestOptionFunc) (*LintResult, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "ci/lint", &opts, options)
if err != nil {
return nil, nil, err
Expand Down
64 changes: 1 addition & 63 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,68 +24,6 @@ import (
)

func TestValidate(t *testing.T) {
testCases := []struct {
description string
content string
response string
want *LintResult
}{
{
description: "valid",
content: `
build1:
stage: build
script:
- echo "Do your build here"`,
response: `{
"status": "valid",
"errors": []
}`,
want: &LintResult{
Status: "valid",
Errors: []string{},
},
},
{
description: "invalid",
content: `
build1:
- echo "Do your build here"`,
response: `{
"status": "invalid",
"errors": ["error message when content is invalid"]
}`,
want: &LintResult{
Status: "invalid",
Errors: []string{"error message when content is invalid"},
},
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/ci/lint", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, tc.response)
})

got, _, err := client.Validate.Lint(tc.content)
if err != nil {
t.Errorf("Validate returned error: %v", err)
}

want := tc.want
if !reflect.DeepEqual(got, want) {
t.Errorf("Validate returned \ngot:\n%v\nwant:\n%v", Stringify(got), Stringify(want))
}
})
}
}

func TestValidateWithOptions(t *testing.T) {
testCases := []struct {
description string
opts *LintOptions
Expand Down Expand Up @@ -142,7 +80,7 @@ func TestValidateWithOptions(t *testing.T) {
fmt.Fprint(w, tc.response)
})

got, _, err := client.Validate.LintWithOptions(tc.opts)
got, _, err := client.Validate.Lint(tc.opts)
if err != nil {
t.Errorf("Validate returned error: %v", err)
}
Expand Down

0 comments on commit df03464

Please sign in to comment.