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