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
|
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,
|
|
|
|
user_can_download_artifacts: can?(current_user, :download_build_artifacts, 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:
|
|
|
|
# GET /projects/:id/builds/commit/:sha
|
|
|
|
get ':id/builds/commit/:sha' do
|
2015-12-29 17:12:36 -05:00
|
|
|
commit = user_project.ci_commits.find_by_sha(params[:sha])
|
|
|
|
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-08 16:57:42 -05:00
|
|
|
present paginate(builds), with: Entities::Build,
|
|
|
|
user_can_download_artifacts: can?(current_user, :download_build_artifacts, 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
|
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,
|
|
|
|
user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
get ':id/builds/:build_id/trace' do
|
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
|
|
|
|
authorize_manage_builds!
|
|
|
|
|
|
|
|
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,
|
|
|
|
user_can_download_artifacts: can?(current_user, :download_build_artifacts, 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
|
|
|
|
authorize_manage_builds!
|
|
|
|
|
|
|
|
build = get_build(params[:build_id])
|
2016-01-08 17:33:45 -05:00
|
|
|
return forbidden!('Build is not retryable') unless build && build.retryable?
|
2015-12-28 10:38:29 -05:00
|
|
|
|
|
|
|
build = Ci::Build.retry(build)
|
|
|
|
|
2016-01-08 16:57:42 -05:00
|
|
|
present build, with: Entities::Build,
|
|
|
|
user_can_download_artifacts: can?(current_user, :download_build_artifacts, user_project)
|
2015-12-28 10:38:29 -05:00
|
|
|
end
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def get_build(id)
|
|
|
|
user_project.builds.where(id: id).first
|
|
|
|
end
|
2015-12-28 09:49:13 -05:00
|
|
|
|
|
|
|
def filter_builds(builds, scope)
|
2016-01-08 08:01:31 -05:00
|
|
|
available_scopes = Ci::Build.available_statuses
|
|
|
|
scope =
|
|
|
|
if scope.is_a?(String) || scope.is_a?(Symbol)
|
|
|
|
available_scopes & [scope.to_s]
|
|
|
|
elsif scope.is_a?(Array)
|
|
|
|
available_scopes & scope
|
|
|
|
elsif scope.respond_to?(:to_h)
|
|
|
|
available_scopes & scope.to_h.values
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
return builds if scope.empty?
|
|
|
|
|
|
|
|
builds.where(status: scope)
|
2015-12-28 09:49:13 -05:00
|
|
|
end
|
2015-12-28 10:38:29 -05:00
|
|
|
|
|
|
|
def authorize_manage_builds!
|
|
|
|
authorize! :manage_builds, user_project
|
|
|
|
end
|
2015-12-24 13:05:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|