debian-mirror-gitlab/app/controllers/projects/builds_controller.rb

132 lines
3.1 KiB
Ruby
Raw Normal View History

2015-10-24 18:46:33 +05:30
class Projects::BuildsController < Projects::ApplicationController
before_action :build, except: [:index, :cancel_all]
2017-08-17 22:00:37 +05:30
before_action :authorize_read_build!,
only: [:index, :show, :status, :raw, :trace]
before_action :authorize_update_build!,
except: [:index, :show, :status, :raw, :trace, :cancel_all]
2016-04-02 18:10:28 +05:30
layout 'project'
2015-10-24 18:46:33 +05:30
def index
@scope = params[:scope]
2016-09-13 17:45:13 +05:30
@all_builds = project.builds.relevant
2015-11-26 14:37:03 +05:30
@builds = @all_builds.order('created_at DESC')
2015-10-24 18:46:33 +05:30
@builds =
case @scope
2016-08-24 12:49:21 +05:30
when 'pending'
@builds.pending.reverse_order
when 'running'
2016-08-24 12:49:21 +05:30
@builds.running.reverse_order
2015-10-24 18:46:33 +05:30
when 'finished'
2015-11-26 14:37:03 +05:30
@builds.finished
2015-10-24 18:46:33 +05:30
else
@builds
2015-10-24 18:46:33 +05:30
end
2017-08-17 22:00:37 +05:30
@builds = @builds.includes([
{ pipeline: :project },
:project,
:tags
])
2015-11-26 14:37:03 +05:30
@builds = @builds.page(params[:page]).per(30)
2015-10-24 18:46:33 +05:30
end
def cancel_all
2017-08-17 22:00:37 +05:30
return access_denied! unless can?(current_user, :update_build, project)
@project.builds.running_or_pending.each do |build|
build.cancel if can?(current_user, :update_build, build)
end
2015-10-24 18:46:33 +05:30
redirect_to namespace_project_builds_path(project.namespace, project)
end
def show
@builds = @project.pipelines.find_by_sha(@build.sha).builds.order('id DESC')
2015-11-26 14:37:03 +05:30
@builds = @builds.where("id not in (?)", @build.id)
@pipeline = @build.pipeline
2015-10-24 18:46:33 +05:30
end
2016-06-02 11:05:42 +05:30
def trace
2017-08-17 22:00:37 +05:30
build.trace.read do |stream|
respond_to do |format|
format.json do
result = {
id: @build.id, status: @build.status, complete: @build.complete?
}
if stream.valid?
stream.limit
state = params[:state].presence
trace = stream.html_with_state(state)
result.merge!(trace.to_h)
end
render json: result
end
2016-06-02 11:05:42 +05:30
end
end
end
2015-10-24 18:46:33 +05:30
def retry
2017-08-17 22:00:37 +05:30
return respond_422 unless @build.retryable?
2015-10-24 18:46:33 +05:30
build = Ci::Build.retry(@build, current_user)
2015-11-26 14:37:03 +05:30
redirect_to build_path(build)
end
2016-08-24 12:49:21 +05:30
def play
2017-08-17 22:00:37 +05:30
return respond_422 unless @build.playable?
2016-08-24 12:49:21 +05:30
build = @build.play(current_user)
redirect_to build_path(build)
end
2016-04-02 18:10:28 +05:30
def cancel
2017-08-17 22:00:37 +05:30
return respond_422 unless @build.cancelable?
2016-04-02 18:10:28 +05:30
@build.cancel
redirect_to build_path(@build)
end
2015-10-24 18:46:33 +05:30
def status
2017-08-17 22:00:37 +05:30
render json: BuildSerializer
.new(project: @project, current_user: @current_user)
.represent_status(@build)
2015-10-24 18:46:33 +05:30
end
2016-04-02 18:10:28 +05:30
def erase
2017-08-17 22:00:37 +05:30
if @build.erase(erased_by: current_user)
redirect_to namespace_project_build_path(project.namespace, project, @build),
2016-09-29 09:46:39 +05:30
notice: "Build has been successfully erased!"
2017-08-17 22:00:37 +05:30
else
respond_422
end
2015-10-24 18:46:33 +05:30
end
2016-06-02 11:05:42 +05:30
def raw
2017-08-17 22:00:37 +05:30
build.trace.read do |stream|
if stream.file?
send_file stream.path, type: 'text/plain; charset=utf-8', disposition: 'inline'
else
render_404
end
2016-06-02 11:05:42 +05:30
end
end
2015-10-24 18:46:33 +05:30
private
2017-08-17 22:00:37 +05:30
def authorize_update_build!
return access_denied! unless can?(current_user, :update_build, build)
end
2015-10-24 18:46:33 +05:30
def build
2017-08-17 22:00:37 +05:30
@build ||= project.builds.find(params[:id])
.present(current_user: current_user)
2015-10-24 18:46:33 +05:30
end
def build_path(build)
2015-12-23 02:04:40 +05:30
namespace_project_build_path(build.project.namespace, build.project, build)
2015-10-24 18:46:33 +05:30
end
end