2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 05:42:48 -04:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class Lint < ::API::Base
|
2020-10-29 14:09:11 -04:00
|
|
|
feature_category :pipeline_authoring
|
|
|
|
|
2016-08-30 09:38:36 -04:00
|
|
|
namespace :ci do
|
2016-09-07 06:19:36 -04:00
|
|
|
desc 'Validation of .gitlab-ci.yml content'
|
|
|
|
params do
|
|
|
|
requires :content, type: String, desc: 'Content of .gitlab-ci.yml'
|
2020-09-25 14:09:46 -04:00
|
|
|
optional :include_merged_yaml, type: Boolean, desc: 'Whether or not to include merged CI config yaml in the response'
|
2016-09-07 06:19:36 -04:00
|
|
|
end
|
2016-08-30 07:03:29 -04:00
|
|
|
post '/lint' do
|
2020-09-25 14:09:46 -04:00
|
|
|
result = Gitlab::Ci::YamlProcessor.new(params[:content], user: current_user).execute
|
|
|
|
error = result.errors.first
|
2016-08-25 07:40:35 -04:00
|
|
|
|
2016-08-30 07:03:29 -04:00
|
|
|
status 200
|
2016-08-29 09:07:19 -04:00
|
|
|
|
2020-09-25 14:09:46 -04:00
|
|
|
response = if error.blank?
|
2020-11-17 22:09:21 -05:00
|
|
|
{ status: 'valid', errors: [], warnings: result.warnings }
|
2020-09-25 14:09:46 -04:00
|
|
|
else
|
2020-11-17 22:09:21 -05:00
|
|
|
{ status: 'invalid', errors: [error], warnings: result.warnings }
|
2020-09-25 14:09:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
response.tap do |response|
|
|
|
|
response[:merged_yaml] = result.merged_yaml if params[:include_merged_yaml]
|
2016-08-30 07:03:29 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-24 05:42:48 -04:00
|
|
|
end
|
2020-10-05 08:08:47 -04:00
|
|
|
|
|
|
|
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
|
2020-11-11 10:08:46 -05:00
|
|
|
|
|
|
|
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.6.'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :content, type: String, desc: 'Content of .gitlab-ci.yml'
|
|
|
|
optional :dry_run, type: Boolean, default: false, desc: 'Run pipeline creation simulation, or only do static check.'
|
|
|
|
end
|
|
|
|
post ':id/ci/lint' do
|
|
|
|
authorize! :download_code, user_project
|
|
|
|
|
|
|
|
result = Gitlab::Ci::Lint
|
|
|
|
.new(project: user_project, current_user: current_user)
|
|
|
|
.validate(params[:content], dry_run: params[:dry_run])
|
|
|
|
|
|
|
|
status 200
|
|
|
|
present result, with: Entities::Ci::Lint::Result, current_user: current_user
|
|
|
|
end
|
|
|
|
end
|
2016-08-24 05:42:48 -04:00
|
|
|
end
|
|
|
|
end
|