Commit cb8bb478 authored by Fabio Pitino's avatar Fabio Pitino

Merge branch 'lm-expose-errors-linter' into 'master'

Return multiple errors and warnings in CI Lint API endpoint

See merge request gitlab-org/gitlab!50891
parents fe7b31dd 0b2ac679
......@@ -15,7 +15,9 @@ module BlobViewer
prepare!
@validation_message = Gitlab::Ci::YamlProcessor.validation_message(blob.data, opts)
@validation_message = Gitlab::Ci::Lint
.new(project: opts[:project], current_user: opts[:user], sha: opts[:sha])
.validate(blob.data).errors.first
end
def valid?(opts)
......
---
title: 'API: Exposes errors in lint endpoint'
merge_request: 50891
author:
type: changed
......@@ -743,7 +743,7 @@ m.project.try(:ci_service)
```ruby
project = Project.find_by_full_path 'group/project'
content = project.repository.gitlab_ci_yml_for(project.repository.root_ref_sha)
Gitlab::Ci::YamlProcessor.validation_message(content, user: User.first)
Gitlab::Ci::Lint.new(project: project, current_user: User.first).validate(content)
```
### Disable AutoDevOps on Existing Projects
......
......@@ -12,14 +12,13 @@ module API
end
post '/lint' do
result = Gitlab::Ci::YamlProcessor.new(params[:content], user: current_user).execute
error = result.errors.first
status 200
response = if error.blank?
response = if result.errors.empty?
{ status: 'valid', errors: [], warnings: result.warnings }
else
{ status: 'invalid', errors: [error], warnings: result.warnings }
{ status: 'invalid', errors: result.errors, warnings: result.warnings }
end
response.tap do |response|
......
......@@ -18,9 +18,10 @@ module Gitlab
end
end
def initialize(project:, current_user:)
def initialize(project:, current_user:, sha: nil)
@project = project
@current_user = current_user
@sha = sha || project.repository.commit.sha
end
def validate(content, dry_run: false)
......@@ -51,7 +52,7 @@ module Gitlab
content,
project: @project,
user: @current_user,
sha: @project.repository.commit.sha
sha: @sha
).execute
Result.new(
......
......@@ -10,12 +10,6 @@ module Gitlab
class YamlProcessor
ValidationError = Class.new(StandardError)
def self.validation_message(content, opts = {})
result = new(content, opts).execute
result.errors.first
end
def initialize(config_content, opts = {})
@config_content = config_content
@opts = opts
......
......@@ -2711,40 +2711,6 @@ module Gitlab
end
end
describe "#validation_message" do
subject { Gitlab::Ci::YamlProcessor.validation_message(content) }
context "when the YAML could not be parsed" do
let(:content) { YAML.dump("invalid: yaml: test") }
it { is_expected.to eq "Invalid configuration format" }
end
context "when the tags parameter is invalid" do
let(:content) { YAML.dump({ rspec: { script: "test", tags: "mysql" } }) }
it { is_expected.to eq "jobs:rspec:tags config should be an array of strings" }
end
context "when YAML content is empty" do
let(:content) { '' }
it { is_expected.to eq "Please provide content of .gitlab-ci.yml" }
end
context 'when the YAML contains an unknown alias' do
let(:content) { 'steps: *bad_alias' }
it { is_expected.to eq "Unknown alias: bad_alias" }
end
context "when the YAML is valid" do
let(:content) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) }
it { is_expected.to be_nil }
end
end
describe '#execute' do
subject { Gitlab::Ci::YamlProcessor.new(content).execute }
......
......@@ -63,7 +63,7 @@ RSpec.describe API::Lint do
end
context 'with invalid configuration' do
let(:yaml_content) { '{ image: "ruby:2.7", services: ["postgres"] }' }
let(:yaml_content) { '{ image: "ruby:2.7", services: ["postgres"], invalid }' }
it 'responds with errors about invalid configuration' do
post api('/ci/lint'), params: { content: yaml_content }
......@@ -71,7 +71,7 @@ RSpec.describe API::Lint do
expect(response).to have_gitlab_http_status(:ok)
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 invalid config should implement a script: or a trigger: keyword', 'jobs config should contain at least one visible job'])
end
it 'outputs expanded yaml content' 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