2015-12-24 13:05:57 -05:00
|
|
|
module API
|
|
|
|
class Builds < Grape::API
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2015-12-24 13:05:57 -05:00
|
|
|
before { authenticate! }
|
|
|
|
|
2016-10-24 07:06:17 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2015-12-24 13:05:57 -05:00
|
|
|
resource :projects 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: ['pending', 'running', 'failed', 'success', 'canceled'],
|
|
|
|
coerce_with: ->(scope) {
|
|
|
|
if scope.is_a?(String)
|
|
|
|
[scope]
|
|
|
|
elsif scope.is_a?(Hashie::Mash)
|
|
|
|
scope.values
|
|
|
|
else
|
|
|
|
['unknown']
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get a project builds' do
|
|
|
|
success Entities::Build
|
|
|
|
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
|
2015-12-24 13:05:57 -05:00
|
|
|
get ':id/builds' do
|
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
|
|
|
|
|
|
|
present paginate(builds), with: Entities::Build,
|
2016-02-01 17:58:04 -05:00
|
|
|
user_can_download_artifacts: can?(current_user, :read_build, user_project)
|
2015-12-28 09:49:13 -05:00
|
|
|
end
|
2015-12-24 13:05:57 -05:00
|
|
|
|
2016-10-24 07:06:17 -04:00
|
|
|
desc 'Get builds for a specific commit of a project' do
|
|
|
|
success Entities::Build
|
|
|
|
end
|
|
|
|
params do
|
2016-12-04 12:11:19 -05:00
|
|
|
requires :sha, type: String, desc: 'The SHA id of a commit'
|
2016-10-24 07:06:17 -04:00
|
|
|
use :optional_scope
|
2016-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
2016-10-24 07:06:17 -04:00
|
|
|
end
|
2016-01-11 04:20:45 -05:00
|
|
|
get ':id/repository/commits/:sha/builds' do
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize_read_builds!
|
|
|
|
|
2016-06-28 06:22:31 -04:00
|
|
|
return not_found! unless user_project.commit(params[:sha])
|
|
|
|
|
2016-06-22 02:53:05 -04:00
|
|
|
pipelines = user_project.pipelines.where(sha: params[:sha])
|
|
|
|
builds = user_project.builds.where(pipeline: pipelines).order('id DESC')
|
2015-12-28 09:49:13 -05:00
|
|
|
builds = filter_builds(builds, params[:scope])
|
2016-01-13 09:17:59 -05:00
|
|
|
|
2016-01-08 16:57:42 -05:00
|
|
|
present paginate(builds), with: Entities::Build,
|
2016-02-01 17:58:04 -05:00
|
|
|
user_can_download_artifacts: can?(current_user, :read_build, user_project)
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
|
|
|
|
2016-10-24 07:06:17 -04:00
|
|
|
desc 'Get a specific build of a project' do
|
|
|
|
success Entities::Build
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :build_id, type: Integer, desc: 'The ID of a build'
|
|
|
|
end
|
2015-12-24 13:05:57 -05:00
|
|
|
get ':id/builds/:build_id' do
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize_read_builds!
|
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
build = get_build!(params[:build_id])
|
2015-12-28 10:38:02 -05:00
|
|
|
|
2016-01-08 16:57:42 -05:00
|
|
|
present build, with: Entities::Build,
|
2016-02-01 17:58:04 -05:00
|
|
|
user_can_download_artifacts: can?(current_user, :read_build, user_project)
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
|
|
|
|
2016-10-24 07:06:17 -04:00
|
|
|
desc 'Download the artifacts file from build' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.5'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :build_id, type: Integer, desc: 'The ID of a build'
|
|
|
|
end
|
2016-02-08 16:32:36 -05:00
|
|
|
get ':id/builds/:build_id/artifacts' do
|
|
|
|
authorize_read_builds!
|
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
build = get_build!(params[:build_id])
|
2016-02-08 16:32:36 -05:00
|
|
|
|
2016-07-19 12:23:45 -04:00
|
|
|
present_artifacts!(build.artifacts_file)
|
2016-07-19 08:59:38 -04:00
|
|
|
end
|
2016-02-08 16:32:36 -05:00
|
|
|
|
2016-10-24 07:06:17 -04:00
|
|
|
desc 'Download the artifacts file from build' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.10'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :ref_name, type: String, desc: 'The ref from repository'
|
|
|
|
requires :job, type: String, desc: 'The name for the build'
|
|
|
|
end
|
2016-07-19 08:59:38 -04:00
|
|
|
get ':id/builds/artifacts/:ref_name/download',
|
|
|
|
requirements: { ref_name: /.+/ } do
|
2016-07-20 10:40:49 -04:00
|
|
|
authorize_read_builds!
|
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
builds = user_project.latest_successful_builds_for(params[:ref_name])
|
|
|
|
latest_build = builds.find_by!(name: params[:job])
|
2016-02-08 16:32:36 -05:00
|
|
|
|
2016-07-19 12:23:45 -04:00
|
|
|
present_artifacts!(latest_build.artifacts_file)
|
2016-02-08 16:32:36 -05:00
|
|
|
end
|
|
|
|
|
2016-01-11 09:49:25 -05:00
|
|
|
# TODO: We should use `present_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.
|
2016-10-24 07:06:17 -04:00
|
|
|
desc 'Get a trace of a specific build of a project'
|
|
|
|
params do
|
|
|
|
requires :build_id, type: Integer, desc: 'The ID of a build'
|
|
|
|
end
|
2015-12-24 13:05:57 -05:00
|
|
|
get ':id/builds/:build_id/trace' do
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize_read_builds!
|
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
build = get_build!(params[:build_id])
|
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
|
|
|
|
2015-12-28 07:09:51 -05:00
|
|
|
trace = build.trace
|
|
|
|
body trace
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
2015-12-28 10:38:29 -05:00
|
|
|
|
2016-10-24 07:06:17 -04:00
|
|
|
desc 'Cancel a specific build of a project' do
|
|
|
|
success Entities::Build
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :build_id, type: Integer, desc: 'The ID of a build'
|
|
|
|
end
|
2015-12-28 10:38:29 -05:00
|
|
|
post ':id/builds/:build_id/cancel' do
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize_update_builds!
|
2015-12-28 10:38:29 -05:00
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
build = get_build!(params[:build_id])
|
2015-12-28 10:38:29 -05:00
|
|
|
|
|
|
|
build.cancel
|
|
|
|
|
2016-01-08 16:57:42 -05:00
|
|
|
present build, with: Entities::Build,
|
2016-02-01 17:58:04 -05:00
|
|
|
user_can_download_artifacts: can?(current_user, :read_build, user_project)
|
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
|
|
|
|
success Entities::Build
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :build_id, type: Integer, desc: 'The ID of a build'
|
|
|
|
end
|
2015-12-28 10:38:29 -05:00
|
|
|
post ':id/builds/:build_id/retry' do
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize_update_builds!
|
2015-12-28 10:38:29 -05:00
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
build = get_build!(params[:build_id])
|
2016-02-03 08:12:33 -05:00
|
|
|
return forbidden!('Build is not retryable') unless build.retryable?
|
2015-12-28 10:38:29 -05:00
|
|
|
|
2016-06-10 17:36:54 -04:00
|
|
|
build = Ci::Build.retry(build, current_user)
|
2015-12-28 10:38:29 -05:00
|
|
|
|
2016-01-08 16:57:42 -05:00
|
|
|
present build, with: Entities::Build,
|
2016-02-01 17:58:04 -05:00
|
|
|
user_can_download_artifacts: can?(current_user, :read_build, user_project)
|
2015-12-28 10:38:29 -05:00
|
|
|
end
|
2016-02-03 08:12:33 -05:00
|
|
|
|
2016-10-24 07:06:17 -04:00
|
|
|
desc 'Erase build (remove artifacts and build trace)' do
|
|
|
|
success Entities::Build
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :build_id, type: Integer, desc: 'The ID of a build'
|
|
|
|
end
|
2016-02-16 03:30:58 -05:00
|
|
|
post ':id/builds/:build_id/erase' do
|
2016-02-10 08:09:11 -05:00
|
|
|
authorize_update_builds!
|
2016-02-03 08:12:33 -05:00
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
build = get_build!(params[:build_id])
|
2016-02-05 02:22:29 -05:00
|
|
|
return forbidden!('Build 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)
|
2016-02-03 08:12:33 -05:00
|
|
|
present build, with: Entities::Build,
|
|
|
|
user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
|
|
|
|
end
|
2016-06-10 11:11:27 -04:00
|
|
|
|
2016-10-24 07:06:17 -04:00
|
|
|
desc 'Keep the artifacts to prevent them from being deleted' do
|
|
|
|
success Entities::Build
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :build_id, type: Integer, desc: 'The ID of a build'
|
|
|
|
end
|
2016-06-10 11:11:27 -04:00
|
|
|
post ':id/builds/:build_id/artifacts/keep' do
|
|
|
|
authorize_update_builds!
|
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
build = get_build!(params[:build_id])
|
|
|
|
return not_found!(build) unless build.artifacts?
|
2016-06-10 11:11:27 -04:00
|
|
|
|
|
|
|
build.keep_artifacts!
|
|
|
|
|
|
|
|
status 200
|
|
|
|
present build, with: Entities::Build,
|
2016-06-14 05:38:34 -04:00
|
|
|
user_can_download_artifacts: can?(current_user, :read_build, user_project)
|
2016-06-10 11:11:27 -04:00
|
|
|
end
|
2016-08-15 09:58:22 -04:00
|
|
|
|
|
|
|
desc 'Trigger a manual build' do
|
|
|
|
success Entities::Build
|
|
|
|
detail 'This feature was added in GitLab 8.11'
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :build_id, type: Integer, desc: 'The ID of a Build'
|
|
|
|
end
|
|
|
|
post ":id/builds/:build_id/play" do
|
|
|
|
authorize_read_builds!
|
|
|
|
|
|
|
|
build = get_build!(params[:build_id])
|
|
|
|
|
2017-01-26 06:52:58 -05:00
|
|
|
bad_request!("Unplayable Job") unless build.playable?
|
2016-08-18 05:42:37 -04:00
|
|
|
|
|
|
|
build.play(current_user)
|
|
|
|
|
|
|
|
status 200
|
|
|
|
present build, with: Entities::Build,
|
|
|
|
user_can_download_artifacts: can?(current_user, :read_build, user_project)
|
2016-08-15 09:58:22 -04:00
|
|
|
end
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def get_build(id)
|
2016-01-13 09:17:59 -05:00
|
|
|
user_project.builds.find_by(id: id.to_i)
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
2015-12-28 09:49:13 -05:00
|
|
|
|
2016-07-19 08:59:38 -04:00
|
|
|
def get_build!(id)
|
|
|
|
get_build(id) || not_found!
|
|
|
|
end
|
|
|
|
|
2016-07-19 12:23:45 -04:00
|
|
|
def present_artifacts!(artifacts_file)
|
2016-07-19 08:59:38 -04:00
|
|
|
if !artifacts_file.file_storage?
|
|
|
|
redirect_to(build.artifacts_file.url)
|
|
|
|
elsif artifacts_file.exists?
|
|
|
|
present_file!(artifacts_file.path, artifacts_file.filename)
|
|
|
|
else
|
|
|
|
not_found!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2015-12-28 10:38:29 -05:00
|
|
|
|
2016-02-01 17:58:04 -05:00
|
|
|
def authorize_read_builds!
|
|
|
|
authorize! :read_build, user_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_update_builds!
|
|
|
|
authorize! :update_build, user_project
|
2015-12-28 10:38:29 -05:00
|
|
|
end
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|