gitlab-org--gitlab-foss/lib/api/jobs.rb

187 lines
5.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module API
class Jobs < ::API::Base
2016-12-04 12:11:19 -05:00
include PaginationParams
before { authenticate! }
feature_category :continuous_integration
2016-10-24 07:06:17 -04:00
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
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',
values: ::CommitStatus::AVAILABLE_STATUSES,
2016-10-24 07:06:17 -04:00
coerce_with: ->(scope) {
case scope
when String
2016-10-24 07:06:17 -04:00
[scope]
when ::Hash
2016-10-24 07:06:17 -04:00
scope.values
when ::Array
scope
2016-10-24 07:06:17 -04:00
else
['unknown']
end
}
end
end
desc 'Get a projects jobs' do
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
# rubocop: disable CodeReuse/ActiveRecord
get ':id/jobs' do
authorize_read_builds!
builds = user_project.builds.order('id DESC')
builds = filter_builds(builds, params[:scope])
2016-01-08 16:57:42 -05:00
builds = builds.preload(:user, :job_artifacts_archive, :job_artifacts, :runner, :tags, pipeline: :project)
present paginate(builds), with: Entities::Ci::Job
end
# rubocop: enable CodeReuse/ActiveRecord
desc 'Get a specific job of a project' do
success Entities::Ci::Job
2016-10-24 07:06:17 -04:00
end
params do
requires :job_id, type: Integer, desc: 'The ID of a job'
2016-10-24 07:06:17 -04:00
end
get ':id/jobs/:job_id' do
authorize_read_builds!
build = find_build!(params[:job_id])
present build, with: Entities::Ci::Job
end
# TODO: We should use `present_disk_file!` and leave this implementation for backward compatibility (when build trace
# 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'
2016-10-24 07:06:17 -04:00
params do
requires :job_id, type: Integer, desc: 'The ID of a job'
2016-10-24 07:06:17 -04:00
end
get ':id/jobs/:job_id/trace' do
authorize_read_builds!
build = find_build!(params[:job_id])
2015-12-28 07:09:51 -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
# 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
end
desc 'Cancel a specific job of a project' do
success Entities::Ci::Job
2016-10-24 07:06:17 -04:00
end
params do
requires :job_id, type: Integer, desc: 'The ID of a job'
2016-10-24 07:06:17 -04:00
end
post ':id/jobs/:job_id/cancel' do
authorize_update_builds!
build = find_build!(params[:job_id])
authorize!(:update_build, build)
build.cancel
present build, with: Entities::Ci::Job
end
2016-10-24 07:06:17 -04:00
desc 'Retry a specific build of a project' do
success Entities::Ci::Job
2016-10-24 07:06:17 -04:00
end
params do
requires :job_id, type: Integer, desc: 'The ID of a build'
2016-10-24 07:06:17 -04:00
end
post ':id/jobs/:job_id/retry' do
authorize_update_builds!
build = find_build!(params[:job_id])
authorize!(:update_build, build)
break forbidden!('Job is not retryable') unless build.retryable?
build = ::Ci::Build.retry(build, current_user)
present build, with: Entities::Ci::Job
end
desc 'Erase job (remove artifacts and the trace)' do
success Entities::Ci::Job
2016-10-24 07:06:17 -04:00
end
params do
requires :job_id, type: Integer, desc: 'The ID of a build'
2016-10-24 07:06:17 -04:00
end
post ':id/jobs/:job_id/erase' do
2016-02-10 08:09:11 -05:00
authorize_update_builds!
build = find_build!(params[:job_id])
2017-11-06 08:20:44 -05:00
authorize!(:erase_build, build)
break forbidden!('Job is not erasable!') unless build.erasable?
build.erase(erased_by: current_user)
present build, with: Entities::Ci::Job
end
2016-06-10 11:11:27 -04: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
requires :job_id, type: Integer, desc: 'The ID of a Job'
2016-08-15 09:58:22 -04:00
end
post ":id/jobs/:job_id/play" do
2016-08-15 09:58:22 -04:00
authorize_read_builds!
job = find_job!(params[:job_id])
2016-08-15 09:58:22 -04:00
authorize!(:play_job, job)
2016-08-18 05:42:37 -04:00
bad_request!("Unplayable Job") unless job.playable?
job.play(current_user)
2016-08-18 05:42:37 -04:00
status 200
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
end
helpers do
# rubocop: disable CodeReuse/ActiveRecord
def filter_builds(builds, scope)
2016-01-13 09:17:59 -05:00
return builds if scope.nil? || scope.empty?
available_statuses = ::CommitStatus::AVAILABLE_STATUSES
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-13 09:17:59 -05:00
builds.where(status: available_statuses && scope)
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end