Commit c54c046e authored by Matija Čupić's avatar Matija Čupić

Show expanded YAML when linting CI config via API

Show YAML with includes and extend entries expanded.
parent 464484f7
......@@ -6,17 +6,22 @@ module API
desc 'Validation of .gitlab-ci.yml content'
params do
requires :content, type: String, desc: 'Content of .gitlab-ci.yml'
optional :include_merged_yaml, type: Boolean, desc: 'Whether or not to include merged CI config yaml in the response'
end
post '/lint' do
error = Gitlab::Ci::YamlProcessor.validation_message(params[:content],
user: current_user)
result = Gitlab::Ci::YamlProcessor.new(params[:content], user: current_user).execute
error = result.errors.first
status 200
if error.blank?
{ status: 'valid', errors: [] }
else
{ status: 'invalid', errors: [error] }
response = if error.blank?
{ status: 'valid', errors: [] }
else
{ status: 'invalid', errors: [error] }
end
response.tap do |response|
response[:merged_yaml] = result.merged_yaml if params[:include_merged_yaml]
end
end
end
......
......@@ -95,6 +95,10 @@ module Gitlab
}.compact }.compact
end
def merged_yaml
@ci_config&.to_hash&.to_yaml
end
private
def variables
......
# frozen_string_literal: true
require 'spec_helper'
module Gitlab
module Ci
class YamlProcessor
RSpec.describe Result do
include StubRequests
let(:user) { create(:user) }
let(:ci_config) { Gitlab::Ci::Config.new(config_content, user: user) }
let(:result) { described_class.new(ci_config: ci_config, warnings: ci_config&.warnings) }
describe '#merged_yaml' do
subject(:merged_yaml) { result.merged_yaml }
let(:config_content) do
YAML.dump(
include: { remote: 'https://example.com/sample.yml' },
test: { stage: 'test', script: 'echo' }
)
end
let(:included_yml) do
YAML.dump(
another_test: { stage: 'test', script: 'echo 2' }
)
end
before do
stub_full_request('https://example.com/sample.yml').to_return(body: included_yml)
end
it 'returns expanded yaml config' do
expanded_config = YAML.safe_load(merged_yaml, [Symbol])
included_config = YAML.safe_load(included_yml, [Symbol])
expect(expanded_config).to include(*included_config.keys)
end
end
end
end
end
end
......@@ -17,23 +17,52 @@ RSpec.describe API::Lint do
expect(json_response['status']).to eq('valid')
expect(json_response['errors']).to eq([])
end
it 'outputs expanded yaml content' do
post api('/ci/lint'), params: { content: yaml_content, include_merged_yaml: true }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to have_key('merged_yaml')
end
end
context 'with an invalid .gitlab_ci.yml' do
it 'responds with errors about invalid syntax' do
post api('/ci/lint'), params: { content: 'invalid content' }
context 'with invalid syntax' do
let(:yaml_content) { 'invalid content' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('invalid')
expect(json_response['errors']).to eq(['Invalid configuration format'])
it 'responds with errors about invalid syntax' do
post api('/ci/lint'), params: { content: yaml_content }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('invalid')
expect(json_response['errors']).to eq(['Invalid configuration format'])
end
it 'outputs expanded yaml content' do
post api('/ci/lint'), params: { content: yaml_content, include_merged_yaml: true }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to have_key('merged_yaml')
end
end
it "responds with errors about invalid configuration" do
post api('/ci/lint'), params: { content: '{ image: "ruby:2.7", services: ["postgres"] }' }
context 'with invalid configuration' do
let(:yaml_content) { '{ image: "ruby:2.7", services: ["postgres"] }' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('invalid')
expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
it 'responds with errors about invalid configuration' do
post api('/ci/lint'), params: { content: yaml_content }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to eq('invalid')
expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
end
it 'outputs expanded yaml content' do
post api('/ci/lint'), params: { content: yaml_content, include_merged_yaml: true }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to have_key('merged_yaml')
end
end
end
......
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