2015-12-24 13:05:57 -05:00
|
|
|
module API
|
|
|
|
# Projects builds API
|
|
|
|
class Builds < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
resource :projects do
|
2015-12-28 09:49:13 -05:00
|
|
|
# Get a project builds
|
2015-12-24 13:05:57 -05:00
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
2016-01-08 08:01:31 -05:00
|
|
|
# scope (optional) - The scope of builds to show (one or array of: pending, running, failed, success, canceled;
|
|
|
|
# if none provided showing all builds)
|
2015-12-24 13:05:57 -05:00
|
|
|
# Example Request:
|
2015-12-24 13:18:01 -05:00
|
|
|
# GET /projects/:id/builds
|
2015-12-24 13:05:57 -05:00
|
|
|
get ':id/builds' do
|
2016-02-01 17:58:04 -05:00
|
|
|
|
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
|
|
|
|
2015-12-28 10:38:02 -05:00
|
|
|
# Get builds for a specific commit of a project
|
2015-12-28 09:49:13 -05:00
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# sha (required) - The SHA id of a commit
|
2016-01-08 08:01:31 -05:00
|
|
|
# scope (optional) - The scope of builds to show (one or array of: pending, running, failed, success, canceled;
|
|
|
|
# if none provided showing all builds)
|
2015-12-28 09:49:13 -05:00
|
|
|
# Example Request:
|
2016-01-11 04:20:45 -05:00
|
|
|
# GET /projects/:id/repository/commits/:sha/builds
|
|
|
|
get ':id/repository/commits/:sha/builds' do
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize_read_builds!
|
|
|
|
|
2016-06-03 07:34:03 -04:00
|
|
|
commit = user_project.pipelines.find_by_sha(params[:sha])
|
2015-12-29 17:12:36 -05:00
|
|
|
return not_found! unless commit
|
|
|
|
|
|
|
|
builds = commit.builds.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
|
|
|
|
|
|
|
|
# Get a specific build of a project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# build_id (required) - The ID of a build
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/builds/:build_id
|
|
|
|
get ':id/builds/:build_id' do
|
2016-02-01 17:58:04 -05:00
|
|
|
authorize_read_builds!
|
|
|
|
|
2015-12-28 10:38:02 -05:00
|
|
|
build = get_build(params[:build_id])
|
|
|
|
return not_found!(build) unless build
|
|
|
|
|
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-02-08 16:32:36 -05:00
|
|
|
# Download the artifacts file from build
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a build
|
|
|
|
# token (required) - The build authorization token
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/builds/:build_id/artifacts
|
|
|
|
get ':id/builds/:build_id/artifacts' do
|
|
|
|
authorize_read_builds!
|
|
|
|
|
|
|
|
build = get_build(params[:build_id])
|
|
|
|
return not_found!(build) unless build
|
|
|
|
|
|
|
|
artifacts_file = build.artifacts_file
|
|
|
|
|
|
|
|
unless artifacts_file.file_storage?
|
|
|
|
return redirect_to build.artifacts_file.url
|
|
|
|
end
|
|
|
|
|
2016-02-19 12:07:05 -05:00
|
|
|
return not_found! unless artifacts_file.exists?
|
2016-02-08 16:32:36 -05:00
|
|
|
|
|
|
|
present_file!(artifacts_file.path, artifacts_file.filename)
|
|
|
|
end
|
|
|
|
|
2015-12-24 13:05:57 -05:00
|
|
|
# Get a trace of a specific build of a project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# build_id (required) - The ID of a build
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/build/:build_id/trace
|
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.
|
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!
|
|
|
|
|
2015-12-28 07:09:51 -05:00
|
|
|
build = get_build(params[:build_id])
|
2015-12-28 10:38:02 -05:00
|
|
|
return not_found!(build) unless 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
|
|
|
|
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
|
|
|
|
2015-12-29 17:12:36 -05:00
|
|
|
# Cancel a specific build of a project
|
2015-12-28 10:38:29 -05:00
|
|
|
#
|
|
|
|
# parameters:
|
|
|
|
# id (required) - the id of a project
|
|
|
|
# build_id (required) - the id of a build
|
|
|
|
# example request:
|
|
|
|
# post /projects/:id/build/:build_id/cancel
|
|
|
|
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
|
|
|
|
|
|
|
build = get_build(params[:build_id])
|
|
|
|
return not_found!(build) unless build
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-12-29 17:12:36 -05:00
|
|
|
# Retry a specific build of a project
|
2015-12-28 10:38:29 -05:00
|
|
|
#
|
|
|
|
# parameters:
|
|
|
|
# id (required) - the id of a project
|
|
|
|
# build_id (required) - the id of a build
|
|
|
|
# example request:
|
|
|
|
# post /projects/:id/build/:build_id/retry
|
|
|
|
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
|
|
|
|
|
|
|
build = get_build(params[:build_id])
|
2016-02-03 08:12:33 -05:00
|
|
|
return not_found!(build) unless build
|
|
|
|
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
|
|
|
|
|
|
|
# Erase build (remove artifacts and build trace)
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - the id of a project
|
|
|
|
# build_id (required) - the id of a build
|
|
|
|
# example Request:
|
2016-02-16 03:30:58 -05:00
|
|
|
# post /projects/:id/build/:build_id/erase
|
|
|
|
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
|
|
|
|
|
|
|
build = get_build(params[:build_id])
|
|
|
|
return not_found!(build) unless build
|
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-06-10 15:45:06 -04:00
|
|
|
# Keep the artifacts to prevent them from being deleted
|
2016-06-10 11:11:27 -04:00
|
|
|
#
|
|
|
|
# Parameters:
|
2016-06-10 15:45:06 -04:00
|
|
|
# id (required) - the id of a project
|
|
|
|
# build_id (required) - The ID of a build
|
2016-06-10 11:11:27 -04:00
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/builds/:build_id/artifacts/keep
|
|
|
|
post ':id/builds/:build_id/artifacts/keep' do
|
|
|
|
authorize_update_builds!
|
|
|
|
|
|
|
|
build = get_build(params[:build_id])
|
|
|
|
return not_found!(build) unless build && build.artifacts?
|
|
|
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
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
|
|
|
scope =
|
2016-01-13 09:17:59 -05:00
|
|
|
if scope.is_a?(String)
|
|
|
|
[scope]
|
|
|
|
elsif scope.is_a?(Hashie::Mash)
|
|
|
|
scope.values
|
2016-01-08 08:01:31 -05:00
|
|
|
else
|
2016-01-13 09:17:59 -05:00
|
|
|
['unknown']
|
2016-01-08 08:01:31 -05:00
|
|
|
end
|
|
|
|
|
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
|