debian-mirror-gitlab/lib/api/jobs.rb

205 lines
6.4 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module API
2021-01-03 14:25:43 +05:30
class Jobs < ::API::Base
2017-08-17 22:00:37 +05:30
include PaginationParams
before { authenticate! }
2019-02-15 15:39:39 +05:30
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2021-04-17 20:07:23 +05:30
params do
requires :id, type: String, desc: 'The ID of a project'
end
2017-08-17 22:00:37 +05:30
helpers do
params :optional_scope do
optional :scope, types: [String, Array[String]], desc: 'The scope of builds to show',
values: ::CommitStatus::AVAILABLE_STATUSES,
coerce_with: ->(scope) {
case scope
when String
[scope]
2018-03-17 18:26:18 +05:30
when ::Hash
2017-08-17 22:00:37 +05:30
scope.values
2018-03-17 18:26:18 +05:30
when ::Array
2017-08-17 22:00:37 +05:30
scope
else
['unknown']
end
}
end
end
desc 'Get a projects jobs' do
2020-10-24 23:57:45 +05:30
success Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
params do
use :optional_scope
use :pagination
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-04-29 21:17:54 +05:30
get ':id/jobs', feature_category: :continuous_integration do
2019-01-03 12:48:30 +05:30
authorize_read_builds!
2017-08-17 22:00:37 +05:30
builds = user_project.builds.order('id DESC')
builds = filter_builds(builds, params[:scope])
2021-03-11 19:13:27 +05:30
builds = builds.preload(:user, :job_artifacts_archive, :job_artifacts, :runner, :tags, pipeline: :project)
2020-10-24 23:57:45 +05:30
present paginate(builds), with: Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
desc 'Get a specific job of a project' do
2020-10-24 23:57:45 +05:30
success Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
params do
requires :job_id, type: Integer, desc: 'The ID of a job'
end
2021-04-29 21:17:54 +05:30
get ':id/jobs/:job_id', feature_category: :continuous_integration do
2017-08-17 22:00:37 +05:30
authorize_read_builds!
2018-03-17 18:26:18 +05:30
build = find_build!(params[:job_id])
2017-08-17 22:00:37 +05:30
2020-10-24 23:57:45 +05:30
present build, with: Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
2018-05-09 12:01:36 +05:30
# TODO: We should use `present_disk_file!` and leave this implementation for backward compatibility (when build trace
2017-08-17 22:00:37 +05:30
# is saved in the DB instead of file). But before that, we need to consider how to replace the value of
# `runners_token` with some mask (like `xxxxxx`) when sending trace file directly by workhorse.
desc 'Get a trace of a specific job of a project'
params do
requires :job_id, type: Integer, desc: 'The ID of a job'
end
2021-04-29 21:17:54 +05:30
get ':id/jobs/:job_id/trace', feature_category: :continuous_integration do
2017-08-17 22:00:37 +05:30
authorize_read_builds!
2018-03-17 18:26:18 +05:30
build = find_build!(params[:job_id])
2017-08-17 22:00:37 +05:30
2021-02-22 17:27:13 +05:30
authorize_read_build_trace!(build) if build
2017-08-17 22:00:37 +05:30
header 'Content-Disposition', "infile; filename=\"#{build.id}.log\""
content_type 'text/plain'
env['api.format'] = :binary
2021-03-11 19:13:27 +05:30
# The trace can be nil bu body method expects a string as an argument.
trace = build.trace.raw || ''
2017-08-17 22:00:37 +05:30
body trace
end
desc 'Cancel a specific job of a project' do
2020-10-24 23:57:45 +05:30
success Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
params do
requires :job_id, type: Integer, desc: 'The ID of a job'
end
2021-04-29 21:17:54 +05:30
post ':id/jobs/:job_id/cancel', feature_category: :continuous_integration do
2017-08-17 22:00:37 +05:30
authorize_update_builds!
2018-03-17 18:26:18 +05:30
build = find_build!(params[:job_id])
2017-08-17 22:00:37 +05:30
authorize!(:update_build, build)
build.cancel
2020-10-24 23:57:45 +05:30
present build, with: Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
desc 'Retry a specific build of a project' do
2020-10-24 23:57:45 +05:30
success Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
params do
requires :job_id, type: Integer, desc: 'The ID of a build'
end
2021-04-29 21:17:54 +05:30
post ':id/jobs/:job_id/retry', feature_category: :continuous_integration do
2017-08-17 22:00:37 +05:30
authorize_update_builds!
2018-03-17 18:26:18 +05:30
build = find_build!(params[:job_id])
2017-08-17 22:00:37 +05:30
authorize!(:update_build, build)
2018-10-15 14:42:47 +05:30
break forbidden!('Job is not retryable') unless build.retryable?
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
build = ::Ci::Build.retry(build, current_user)
2017-08-17 22:00:37 +05:30
2020-10-24 23:57:45 +05:30
present build, with: Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
desc 'Erase job (remove artifacts and the trace)' do
2020-10-24 23:57:45 +05:30
success Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
params do
requires :job_id, type: Integer, desc: 'The ID of a build'
end
2021-04-29 21:17:54 +05:30
post ':id/jobs/:job_id/erase', feature_category: :continuous_integration do
2017-08-17 22:00:37 +05:30
authorize_update_builds!
2018-03-17 18:26:18 +05:30
build = find_build!(params[:job_id])
authorize!(:erase_build, build)
2018-10-15 14:42:47 +05:30
break forbidden!('Job is not erasable!') unless build.erasable?
2017-08-17 22:00:37 +05:30
build.erase(erased_by: current_user)
2020-10-24 23:57:45 +05:30
present build, with: Entities::Ci::Job
2017-08-17 22:00:37 +05:30
end
2021-03-08 18:12:59 +05:30
desc 'Trigger an actionable job (manual, delayed, etc)' do
success Entities::Ci::JobBasic
2017-08-17 22:00:37 +05:30
detail 'This feature was added in GitLab 8.11'
end
params do
requires :job_id, type: Integer, desc: 'The ID of a Job'
end
2021-03-08 18:12:59 +05:30
2021-04-29 21:17:54 +05:30
post ":id/jobs/:job_id/play", feature_category: :continuous_integration do
2017-08-17 22:00:37 +05:30
authorize_read_builds!
2021-03-08 18:12:59 +05:30
job = find_job!(params[:job_id])
2017-08-17 22:00:37 +05:30
2021-03-08 18:12:59 +05:30
authorize!(:play_job, job)
2017-08-17 22:00:37 +05:30
2021-03-08 18:12:59 +05:30
bad_request!("Unplayable Job") unless job.playable?
job.play(current_user)
2017-08-17 22:00:37 +05:30
status 200
2021-03-08 18:12:59 +05:30
if job.is_a?(::Ci::Build)
present job, with: Entities::Ci::Job
else
present job, with: Entities::Ci::Bridge
end
2017-08-17 22:00:37 +05:30
end
end
2021-04-17 20:07:23 +05:30
resource :job do
desc 'Get current project using job token' do
success Entities::Ci::Job
end
route_setting :authentication, job_token_allowed: true
2021-04-29 21:17:54 +05:30
get '', feature_category: :continuous_integration do
validate_current_authenticated_job
2021-04-17 20:07:23 +05:30
present current_authenticated_job, with: Entities::Ci::Job
end
end
2017-08-17 22:00:37 +05:30
helpers do
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def filter_builds(builds, scope)
return builds if scope.nil? || scope.empty?
available_statuses = ::CommitStatus::AVAILABLE_STATUSES
unknown = scope - available_statuses
render_api_error!('Scope contains invalid value(s)', 400) unless unknown.empty?
builds.where(status: available_statuses && scope)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2021-04-29 21:17:54 +05:30
def validate_current_authenticated_job
# current_authenticated_job will be nil if user is using
# a valid authentication (like PRIVATE-TOKEN) that is not CI_JOB_TOKEN
not_found!('Job') unless current_authenticated_job
end
2017-08-17 22:00:37 +05:30
end
end
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
API::Jobs.prepend_mod_with('API::Jobs')