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
|
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
helpers do
|
|
|
|
def can_lint_ci?
|
|
|
|
signup_unrestricted = Gitlab::CurrentSettings.signup_enabled? && !Gitlab::CurrentSettings.signup_limited?
|
|
|
|
internal_user = current_user.present? && !current_user.external?
|
|
|
|
is_developer = current_user.present? && current_user.projects.any? { |p| p.team.member?(current_user, Gitlab::Access::DEVELOPER) }
|
|
|
|
|
|
|
|
signup_unrestricted || internal_user || is_developer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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'
|
2021-11-03 14:13:40 -04:00
|
|
|
optional :include_jobs, type: Boolean, desc: 'Whether or not to include CI jobs in the response'
|
2016-09-07 06:19:36 -04:00
|
|
|
end
|
2022-03-23 11:08:38 -04:00
|
|
|
post '/lint', urgency: :low do
|
2021-12-06 22:12:22 -05:00
|
|
|
unauthorized! unless can_lint_ci?
|
2021-02-11 10:09:11 -05:00
|
|
|
|
2021-08-26 08:10:28 -04:00
|
|
|
result = Gitlab::Ci::Lint.new(project: nil, current_user: current_user)
|
|
|
|
.validate(params[:content], dry_run: false)
|
2016-08-25 07:40:35 -04:00
|
|
|
|
2016-08-30 07:03:29 -04:00
|
|
|
status 200
|
2021-11-03 14:13:40 -04:00
|
|
|
Entities::Ci::Lint::Result.represent(result, current_user: current_user, include_jobs: params[:include_jobs]).serializable_hash.tap do |presented_result|
|
2021-08-26 08:10:28 -04:00
|
|
|
presented_result[:status] = presented_result[:valid] ? 'valid' : 'invalid'
|
|
|
|
presented_result.delete(:merged_yaml) unless 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.'
|
2021-11-03 14:13:40 -04:00
|
|
|
optional :include_jobs, type: Boolean, desc: 'Whether or not to include CI jobs in the response'
|
2022-02-14 07:14:02 -05:00
|
|
|
optional :ref, type: String, desc: 'Branch or tag used to execute a dry run. Defaults to the default branch of the project. Only used when dry_run is true'
|
2020-10-05 08:08:47 -04:00
|
|
|
end
|
2021-12-03 07:10:23 -05:00
|
|
|
get ':id/ci/lint', urgency: :low do
|
2020-10-05 08:08:47 -04:00
|
|
|
authorize! :download_code, user_project
|
|
|
|
|
2022-02-10 07:18:48 -05:00
|
|
|
if user_project.commit.present?
|
|
|
|
content = user_project.repository.gitlab_ci_yml_for(user_project.commit.id, user_project.ci_config_path_or_default)
|
|
|
|
end
|
|
|
|
|
2020-10-05 08:08:47 -04:00
|
|
|
result = Gitlab::Ci::Lint
|
|
|
|
.new(project: user_project, current_user: current_user)
|
2022-02-14 07:14:02 -05:00
|
|
|
.validate(content, dry_run: params[:dry_run], ref: params[:ref] || user_project.default_branch)
|
2020-10-05 08:08:47 -04:00
|
|
|
|
2021-11-03 14:13:40 -04:00
|
|
|
present result, with: Entities::Ci::Lint::Result, current_user: current_user, include_jobs: params[:include_jobs]
|
2020-10-05 08:08:47 -04:00
|
|
|
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.'
|
2021-11-03 14:13:40 -04:00
|
|
|
optional :include_jobs, type: Boolean, desc: 'Whether or not to include CI jobs in the response'
|
2022-02-14 07:14:02 -05:00
|
|
|
optional :ref, type: String, desc: 'Branch or tag used to execute a dry run. Defaults to the default branch of the project. Only used when dry_run is true'
|
2020-11-11 10:08:46 -05:00
|
|
|
end
|
2021-12-03 07:10:23 -05:00
|
|
|
post ':id/ci/lint', urgency: :low do
|
2021-02-11 10:09:11 -05:00
|
|
|
authorize! :create_pipeline, user_project
|
2020-11-11 10:08:46 -05:00
|
|
|
|
|
|
|
result = Gitlab::Ci::Lint
|
|
|
|
.new(project: user_project, current_user: current_user)
|
2022-02-14 07:14:02 -05:00
|
|
|
.validate(params[:content], dry_run: params[:dry_run], ref: params[:ref] || user_project.default_branch)
|
2020-11-11 10:08:46 -05:00
|
|
|
|
|
|
|
status 200
|
2021-11-03 14:13:40 -04:00
|
|
|
present result, with: Entities::Ci::Lint::Result, current_user: current_user, include_jobs: params[:include_jobs]
|
2020-11-11 10:08:46 -05:00
|
|
|
end
|
|
|
|
end
|
2016-08-24 05:42:48 -04:00
|
|
|
end
|
|
|
|
end
|