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