2018-12-05 23:21:45 +05:30
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
|
module API
|
2021-01-03 14:25:43 +05:30
|
|
|
|
class Lint < ::API::Base
|
2021-01-29 00:20:46 +05:30
|
|
|
|
feature_category :pipeline_authoring
|
|
|
|
|
|
2021-12-07 22:27:20 +05:30
|
|
|
|
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-09-29 09:46:39 +05:30
|
|
|
|
namespace :ci do
|
2023-01-13 00:05:48 +05:30
|
|
|
|
desc 'Validates the .gitlab-ci.yml content' do
|
|
|
|
|
detail 'Checks if CI/CD YAML configuration is valid'
|
|
|
|
|
success code: 200, model: Entities::Ci::Lint::Result
|
|
|
|
|
tags %w[ci_lint]
|
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
params do
|
2023-01-13 00:05:48 +05:30
|
|
|
|
requires :content, type: String, desc: 'The CI/CD configuration content'
|
|
|
|
|
optional :include_merged_yaml, type: Boolean, desc: 'If the expanded CI/CD configuration should be included in the response'
|
|
|
|
|
optional :include_jobs, type: Boolean, desc: 'If the list of jobs should be included in the response. This is
|
|
|
|
|
false by default'
|
2016-09-29 09:46:39 +05:30
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
|
post '/lint', urgency: :low do
|
2021-12-07 22:27:20 +05:30
|
|
|
|
unauthorized! unless can_lint_ci?
|
2021-02-11 23:33:58 +05:30
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
|
result = Gitlab::Ci::Lint.new(project: nil, current_user: current_user)
|
|
|
|
|
.validate(params[:content], dry_run: false)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
|
|
status 200
|
2021-12-11 22:18:48 +05:30
|
|
|
|
Entities::Ci::Lint::Result.represent(result, current_user: current_user, include_jobs: params[:include_jobs]).serializable_hash.tap do |presented_result|
|
2021-11-11 11:23:49 +05:30
|
|
|
|
presented_result[:status] = presented_result[:valid] ? 'valid' : 'invalid'
|
|
|
|
|
presented_result.delete(:merged_yaml) unless params[:include_merged_yaml]
|
2016-09-29 09:46:39 +05:30
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2023-01-13 00:05:48 +05:30
|
|
|
|
desc 'Validates a CI YAML configuration with a namespace' do
|
|
|
|
|
detail 'Checks if a project’s latest (HEAD of the project’s default branch) .gitlab-ci.yml configuration is
|
|
|
|
|
valid'
|
|
|
|
|
success Entities::Ci::Lint::Result
|
|
|
|
|
tags %w[ci_lint]
|
2021-01-03 14:25:43 +05:30
|
|
|
|
end
|
|
|
|
|
params do
|
2023-01-13 00:05:48 +05:30
|
|
|
|
optional :dry_run, type: Boolean, default: false, desc: 'Run pipeline creation simulation, or only do static check. This is false by default'
|
|
|
|
|
optional :include_jobs, type: Boolean, desc: 'If the list of jobs that would exist in a static check or pipeline
|
|
|
|
|
simulation should be included in the response. This is false by default'
|
2022-04-04 11:22:00 +05:30
|
|
|
|
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'
|
2021-01-03 14:25:43 +05:30
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
|
get ':id/ci/lint', urgency: :low do
|
2023-01-13 00:05:48 +05:30
|
|
|
|
authorize! :read_code, user_project
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
|
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
|
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
|
result = Gitlab::Ci::Lint
|
|
|
|
|
.new(project: user_project, current_user: current_user)
|
2022-04-04 11:22:00 +05:30
|
|
|
|
.validate(content, dry_run: params[:dry_run], ref: params[:ref] || user_project.default_branch)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
|
present result, with: Entities::Ci::Lint::Result, current_user: current_user, include_jobs: params[:include_jobs]
|
2021-01-03 14:25:43 +05:30
|
|
|
|
end
|
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2023-01-13 00:05:48 +05:30
|
|
|
|
desc 'Validate a CI YAML configuration with a namespace' do
|
|
|
|
|
detail 'Checks if CI/CD YAML configuration is valid. This endpoint has namespace specific context'
|
|
|
|
|
success code: 200, model: Entities::Ci::Lint::Result
|
|
|
|
|
tags %w[ci_lint]
|
2021-01-29 00:20:46 +05:30
|
|
|
|
end
|
|
|
|
|
params do
|
|
|
|
|
requires :content, type: String, desc: 'Content of .gitlab-ci.yml'
|
2023-01-13 00:05:48 +05:30
|
|
|
|
optional :dry_run, type: Boolean, default: false, desc: 'Run pipeline creation simulation, or only do static check. This is false by default'
|
|
|
|
|
optional :include_jobs, type: Boolean, desc: 'If the list of jobs that would exist in a static check or pipeline
|
|
|
|
|
simulation should be included in the response. This is false by default'
|
|
|
|
|
optional :ref, type: String, desc: 'When dry_run is true, sets the branch or tag to use. Defaults to the project’s default branch when not set'
|
2021-01-29 00:20:46 +05:30
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
|
post ':id/ci/lint', urgency: :low do
|
2021-02-11 23:33:58 +05:30
|
|
|
|
authorize! :create_pipeline, user_project
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
|
|
result = Gitlab::Ci::Lint
|
|
|
|
|
.new(project: user_project, current_user: current_user)
|
2022-04-04 11:22:00 +05:30
|
|
|
|
.validate(params[:content], dry_run: params[:dry_run], ref: params[:ref] || user_project.default_branch)
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
|
|
status 200
|
2021-12-11 22:18:48 +05:30
|
|
|
|
present result, with: Entities::Ci::Lint::Result, current_user: current_user, include_jobs: params[:include_jobs]
|
2021-01-29 00:20:46 +05:30
|
|
|
|
end
|
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
end
|
|
|
|
|
end
|