Commit ec0d1f5f authored by lauraMon's avatar lauraMon

Refactored a little and added more specs

parent 214a372a
...@@ -13,14 +13,13 @@ module API ...@@ -13,14 +13,13 @@ module API
post '/lint' do post '/lint' do
result = Gitlab::Ci::YamlProcessor.new(params[:content], user: current_user).execute result = Gitlab::Ci::YamlProcessor.new(params[:content], user: current_user).execute
error = result.errors.first error = result.errors.first
warning = result.warnings.first
status 200 status 200
response = if error.blank? response = if error.blank?
{ status: 'valid', errors: [], warnings: [warning] } { status: 'valid', errors: [], warnings: result.warnings }
else else
{ status: 'invalid', errors: [error], warnings: [] } { status: 'invalid', errors: [error], warnings: result.warnings }
end end
response.tap do |response| response.tap do |response|
......
...@@ -9,12 +9,13 @@ RSpec.describe API::Lint do ...@@ -9,12 +9,13 @@ RSpec.describe API::Lint do
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
end end
it 'passes validation' do it 'passes validation without warnings' do
post api('/ci/lint'), params: { content: yaml_content } post api('/ci/lint'), params: { content: yaml_content }
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_an Hash expect(json_response).to be_an Hash
expect(json_response['status']).to eq('valid') expect(json_response['status']).to eq('valid')
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq([]) expect(json_response['errors']).to eq([])
end end
...@@ -26,8 +27,8 @@ RSpec.describe API::Lint do ...@@ -26,8 +27,8 @@ RSpec.describe API::Lint do
end end
end end
context 'with valid .gitlab-ci.yaml but with warnings' do context 'with valid .gitlab-ci.yaml with warnings' do
let(:yaml_content) { { job: { script: 'ls', rules: [{ when: 'always' }] } }.to_yaml } let(:yaml_content) { { job: { script: 'ls', rules: [{ when: 'always' }] } }.to_yaml }
it 'passes validation but raises a warning' do it 'passes validation but raises a warning' do
post api('/ci/lint'), params: { content: yaml_content } post api('/ci/lint'), params: { content: yaml_content }
...@@ -48,6 +49,7 @@ RSpec.describe API::Lint do ...@@ -48,6 +49,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('invalid') expect(json_response['status']).to eq('invalid')
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq(['Invalid configuration format']) expect(json_response['errors']).to eq(['Invalid configuration format'])
end end
...@@ -67,6 +69,7 @@ RSpec.describe API::Lint do ...@@ -67,6 +69,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('invalid') expect(json_response['status']).to eq('invalid')
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).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
...@@ -106,7 +109,7 @@ RSpec.describe API::Lint do ...@@ -106,7 +109,7 @@ RSpec.describe API::Lint do
end end
end end
RSpec.shared_examples 'valid config' do RSpec.shared_examples 'valid config without warnings' do
it 'passes validation' do it 'passes validation' do
ci_lint ci_lint
...@@ -118,6 +121,7 @@ RSpec.describe API::Lint do ...@@ -118,6 +121,7 @@ RSpec.describe API::Lint do
expect(json_response).to be_an Hash expect(json_response).to be_an Hash
expect(json_response['merged_yaml']).to eq(expected_yaml) expect(json_response['merged_yaml']).to eq(expected_yaml)
expect(json_response['valid']).to eq(true) expect(json_response['valid']).to eq(true)
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq([]) expect(json_response['errors']).to eq([])
end end
end end
...@@ -129,6 +133,7 @@ RSpec.describe API::Lint do ...@@ -129,6 +133,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['merged_yaml']).to eq(yaml_content) expect(json_response['merged_yaml']).to eq(yaml_content)
expect(json_response['valid']).to eq(false) expect(json_response['valid']).to eq(false)
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).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
end end
...@@ -181,6 +186,7 @@ RSpec.describe API::Lint do ...@@ -181,6 +186,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['merged_yaml']).to eq(nil) expect(json_response['merged_yaml']).to eq(nil)
expect(json_response['valid']).to eq(false) expect(json_response['valid']).to eq(false)
expect(json_response['warnings']).to eq([])
expect(json_response['errors']).to eq(['Insufficient permissions to create a new pipeline']) expect(json_response['errors']).to eq(['Insufficient permissions to create a new pipeline'])
end end
end end
...@@ -210,7 +216,7 @@ RSpec.describe API::Lint do ...@@ -210,7 +216,7 @@ RSpec.describe API::Lint do
) )
end end
it_behaves_like 'valid config' it_behaves_like 'valid config without warnings'
end end
end end
end end
...@@ -266,13 +272,13 @@ RSpec.describe API::Lint do ...@@ -266,13 +272,13 @@ RSpec.describe API::Lint do
context 'when running as dry run' do context 'when running as dry run' do
let(:dry_run) { true } let(:dry_run) { true }
it_behaves_like 'valid config' it_behaves_like 'valid config without warnings'
end end
context 'when running static validation' do context 'when running static validation' do
let(:dry_run) { false } let(:dry_run) { false }
it_behaves_like 'valid config' it_behaves_like 'valid config without warnings'
end end
context 'With warnings' do context 'With warnings' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment