From 257c2acde7a0be63d955df63ca29488236e5654f Mon Sep 17 00:00:00 2001 From: Katarzyna Kobierska Date: Wed, 24 Aug 2016 10:16:58 +0200 Subject: [PATCH] Add params requires to lint --- lib/ci/api/api.rb | 2 +- lib/ci/api/lint.rb | 40 +++++++++++++++---------------- spec/requests/ci/api/lint_spec.rb | 37 +++++++++++----------------- 3 files changed, 35 insertions(+), 44 deletions(-) diff --git a/lib/ci/api/api.rb b/lib/ci/api/api.rb index 741a81ca8f0..00572e6efdb 100644 --- a/lib/ci/api/api.rb +++ b/lib/ci/api/api.rb @@ -22,9 +22,9 @@ module Ci helpers Gitlab::CurrentSettings mount ::Ci::API::Builds + mount ::Ci::API::Lint mount ::Ci::API::Runners mount ::Ci::API::Triggers - mount ::Ci::API::Lint end end end diff --git a/lib/ci/api/lint.rb b/lib/ci/api/lint.rb index fab4f8d5925..6ea91ac34dd 100644 --- a/lib/ci/api/lint.rb +++ b/lib/ci/api/lint.rb @@ -1,35 +1,35 @@ module Ci module API class Lint < Grape::API - before { authenticate! } - - resources :lint do + resource :lint do post do + status 200 + params do + requires :content, type: String, desc: 'content of .gitlab-ci.yml' + end + begin - response = {} - @content = params[:content] + response = { + status: '', + errors: [], + jobs: [] + } - if @content - @config_processor = Ci::GitlabCiYamlProcessor.new(@content) - @stages = @config_processor.stages - @builds = @config_processor.builds + config_processor = Ci::GitlabCiYamlProcessor.new(params[:content]) - response = { - content: @content, - status: "syntax is correct" - } - - @stages.each do |stage| - response["#{stage}"] = @builds.select { |build| build[:stage] == stage } - end - else - render_api_error!("Please provide content of .gitlab-ci.yml", 400) + config_processor.builds.each do |build| + response[:jobs].push("#{build[:name]}") + response[:status] = 'valid' end response rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e - error!({ content: @content, status: "syntax is incorrect", message: e.message }) + status 200 + response[:errors].push(e.message) + response[:status] = 'invalid' + + response end end end diff --git a/spec/requests/ci/api/lint_spec.rb b/spec/requests/ci/api/lint_spec.rb index 4a18fd0b6b3..cc3d77f3b38 100644 --- a/spec/requests/ci/api/lint_spec.rb +++ b/spec/requests/ci/api/lint_spec.rb @@ -9,41 +9,32 @@ describe Ci::API::API do end describe 'POST /ci/lint' do - context "with valid .gitlab-ci.yaml content" do - context "authorized user" do - it "validate content" do - post ci_api('/lint'), { private_token: user.private_token, content: yaml_content } - - expect(response).to have_http_status(201) - expect(json_response).to be_an Hash - expect(json_response['status']).to eq('syntax is correct') - end - end - - context "unauthorized user" do - it "does not validate content" do + context 'with valid .gitlab-ci.yaml content' do + context 'authorized user' do + it 'validate content' do post ci_api('/lint'), { content: yaml_content } - expect(response).to have_http_status(401) + expect(response).to have_http_status(200) + expect(json_response).to be_an Hash + expect(json_response['status']).to eq('valid') end end end - context "with invalid .gitlab_ci.yml content" do - it "validate content" do - post ci_api('/lint'), { private_token: user.private_token, content: 'invalid content' } + context 'with invalid .gitlab_ci.yml content' do + it 'validate content' do + post ci_api('/lint'), { content: 'invalid content' } - expect(response).to have_http_status(500) - expect(json_response['status']).to eq('syntax is incorrect') + expect(response).to have_http_status(200) + expect(json_response['status']).to eq('invalid') end end - context "no content" do - it "shows error message" do - post ci_api('/lint'), { private_token: user.private_token } + context 'no content parameters' do + it 'shows error message' do + post ci_api('/lint') expect(response).to have_http_status(400) - expect(json_response['message']).to eq('Please provide content of .gitlab-ci.yml') end end end