diff --git a/lib/api/lint.rb b/lib/api/lint.rb index b1c6f52bccba61692343a7d4c51f91358859cd82..98010cf3b68601892dc07ce5e4cf69d90a3c550a 100644 --- a/lib/api/lint.rb +++ b/lib/api/lint.rb @@ -5,22 +5,18 @@ module API requires :content, type: String, desc: 'Content of .gitlab-ci.yml' end - post 'ci/lint' do - error = Ci::GitlabCiYamlProcessor.validation_message(params[:content]) - response = { - status: '', - error: '' - } + namespace 'ci' do + post '/lint' do + errors = Ci::GitlabCiYamlProcessor.validation_message(params[:content]) - if error.blank? - response[:status] = 'valid' - else - response[:error] = error - response[:status] = 'invalid' - end + status 200 - status 200 - response + if errors.blank? + { status: 'valid', errors: [] } + else + { status: 'invalid', errors: [errors] } + end + end end end end diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index c547193ce4cb7f5911c88d0f2a7f7331b6275c21..bdae3205511e3a45a3f8216fbb3c22b5a4e5662e 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -79,15 +79,12 @@ module Ci end def self.validation_message(content) - if content.blank? - 'Please provide content of .gitlab-ci.yml' - else - begin - Ci::GitlabCiYamlProcessor.new(content) - nil - rescue ValidationError, Psych::SyntaxError => e - e.message - end + return 'Please provide content of .gitlab-ci.yml' if content.blank? + begin + Ci::GitlabCiYamlProcessor.new(content) + nil + rescue ValidationError, Psych::SyntaxError => e + e.message end end diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb index 21aca9ee39fc0c87015a0994ceecc4613391400c..3b77dbdb8171ec363aaf32d5adf3df14a9845ba2 100644 --- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb +++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb @@ -1256,7 +1256,8 @@ EOT it "returns an error about invalid configutaion" do content = YAML.dump("invalid: yaml: test") - expect(GitlabCiYamlProcessor.validation_message(content)).to eq "Invalid configuration format" + expect(GitlabCiYamlProcessor.validation_message(content)) + .to eq "Invalid configuration format" end end @@ -1264,13 +1265,15 @@ EOT it "returns an error about invalid tags" do content = YAML.dump({ rspec: { script: "test", tags: "mysql" } }) - expect(GitlabCiYamlProcessor.validation_message(content)).to eq "jobs:rspec tags should be an array of strings" + expect(GitlabCiYamlProcessor.validation_message(content)) + .to eq "jobs:rspec tags should be an array of strings" end end context "when YMAL content is empty" do it "returns an error about missing content" do - expect(GitlabCiYamlProcessor.validation_message('')).to eq "Please provide content of .gitlab-ci.yml" + expect(GitlabCiYamlProcessor.validation_message('')) + .to eq "Please provide content of .gitlab-ci.yml" end end diff --git a/spec/requests/api/lint_spec.rb b/spec/requests/api/lint_spec.rb index 5f8c2829dfa34ae86e723822c90224e1fef3ae3a..c791b4891b6c026f1c161e0154d529da789192ad 100644 --- a/spec/requests/api/lint_spec.rb +++ b/spec/requests/api/lint_spec.rb @@ -15,7 +15,7 @@ describe API::Lint, api: true do expect(response).to have_http_status(200) expect(json_response).to be_an Hash expect(json_response['status']).to eq('valid') - expect(json_response['error']).to eq('') + expect(json_response['errors']).to eq([]) end end @@ -25,7 +25,7 @@ describe API::Lint, api: true do expect(response).to have_http_status(200) expect(json_response['status']).to eq('invalid') - expect(json_response['error']).to eq('Invalid configuration format') + expect(json_response['errors']).to eq(['Invalid configuration format']) end it "responds with errors about invalid configuration" do @@ -33,7 +33,7 @@ describe API::Lint, api: true do expect(response).to have_http_status(200) expect(json_response['status']).to eq('invalid') - expect(json_response['error']).to eq('jobs config should contain at least one visible job') + expect(json_response['errors']).to eq(['jobs config should contain at least one visible job']) end end