Add params requires to lint

This commit is contained in:
Katarzyna Kobierska 2016-08-24 10:16:58 +02:00
parent 0e4c0e7818
commit 257c2acde7
3 changed files with 35 additions and 44 deletions

View file

@ -22,9 +22,9 @@ module Ci
helpers Gitlab::CurrentSettings helpers Gitlab::CurrentSettings
mount ::Ci::API::Builds mount ::Ci::API::Builds
mount ::Ci::API::Lint
mount ::Ci::API::Runners mount ::Ci::API::Runners
mount ::Ci::API::Triggers mount ::Ci::API::Triggers
mount ::Ci::API::Lint
end end
end end
end end

View file

@ -1,35 +1,35 @@
module Ci module Ci
module API module API
class Lint < Grape::API class Lint < Grape::API
before { authenticate! } resource :lint do
resources :lint do
post do post do
status 200
params do
requires :content, type: String, desc: 'content of .gitlab-ci.yml'
end
begin begin
response = {} response = {
@content = params[:content] status: '',
errors: [],
jobs: []
}
if @content config_processor = Ci::GitlabCiYamlProcessor.new(params[:content])
@config_processor = Ci::GitlabCiYamlProcessor.new(@content)
@stages = @config_processor.stages
@builds = @config_processor.builds
response = { config_processor.builds.each do |build|
content: @content, response[:jobs].push("#{build[:name]}")
status: "syntax is correct" response[:status] = 'valid'
}
@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)
end end
response response
rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e 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 end
end end

View file

@ -9,41 +9,32 @@ describe Ci::API::API do
end end
describe 'POST /ci/lint' do describe 'POST /ci/lint' do
context "with valid .gitlab-ci.yaml content" do context 'with valid .gitlab-ci.yaml content' do
context "authorized user" do context 'authorized user' do
it "validate content" 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
post ci_api('/lint'), { content: yaml_content } 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 end
end end
context "with invalid .gitlab_ci.yml content" do context 'with invalid .gitlab_ci.yml content' do
it "validate content" do it 'validate content' do
post ci_api('/lint'), { private_token: user.private_token, content: 'invalid content' } post ci_api('/lint'), { content: 'invalid content' }
expect(response).to have_http_status(500) expect(response).to have_http_status(200)
expect(json_response['status']).to eq('syntax is incorrect') expect(json_response['status']).to eq('invalid')
end end
end end
context "no content" do context 'no content parameters' do
it "shows error message" do it 'shows error message' do
post ci_api('/lint'), { private_token: user.private_token } post ci_api('/lint')
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect(json_response['message']).to eq('Please provide content of .gitlab-ci.yml')
end end
end end
end end