gitlab-org--gitlab-foss/lib/api/lint.rb

49 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-08-24 09:42:48 +00:00
module API
class Lint < Grape::API::Instance
2016-08-30 13:38:36 +00:00
namespace :ci do
2016-09-07 10:19:36 +00:00
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'
2016-09-07 10:19:36 +00:00
end
2016-08-30 11:03:29 +00:00
post '/lint' do
result = Gitlab::Ci::YamlProcessor.new(params[:content], user: current_user).execute
error = result.errors.first
2016-08-30 11:03:29 +00:00
status 200
2016-08-29 13:07:19 +00:00
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]
2016-08-30 11:03:29 +00:00
end
end
2016-08-24 09:42:48 +00:00
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Validation of .gitlab-ci.yml content' do
detail 'This feature was introduced in GitLab 13.5.'
end
params do
optional :dry_run, type: Boolean, default: false, desc: 'Run pipeline creation simulation, or only do static check.'
end
get ':id/ci/lint' do
authorize! :download_code, user_project
content = user_project.repository.gitlab_ci_yml_for(user_project.commit.id, user_project.ci_config_path_or_default)
result = Gitlab::Ci::Lint
.new(project: user_project, current_user: current_user)
.validate(content, dry_run: params[:dry_run])
present result, with: Entities::Ci::Lint::Result, current_user: current_user
end
end
2016-08-24 09:42:48 +00:00
end
end