forgeplus/app/controllers/ci/builds_controller.rb

55 lines
1.9 KiB
Ruby
Raw Permalink Normal View History

2020-08-13 23:38:32 +08:00
class Ci::BuildsController < Ci::BaseController
include RepositoriesHelper
2020-09-10 11:52:54 +08:00
before_action :load_project
2020-10-16 11:57:20 +08:00
before_action :authorize_owner!, only: [:restart, :stop]
2020-08-20 18:37:53 +08:00
before_action :load_repo
2020-08-21 14:53:48 +08:00
before_action :find_cloud_account, except: [:index, :show]
2020-08-13 23:38:32 +08:00
def index
2020-09-18 15:26:14 +08:00
@user = current_user
2020-08-20 18:37:53 +08:00
scope = @repo.builds
2020-08-13 23:38:32 +08:00
2020-08-20 18:37:53 +08:00
scope = Ci::Builds::ListQuery.call(@repo, params)
2020-08-21 14:53:48 +08:00
2020-08-20 18:37:53 +08:00
@total_count = scope.map(&:build_id).size
@builds = paginate scope
end
def show
@build = @repo.builds.includes(stages: [:steps]).find_by(build_number: params[:build])
2020-08-13 23:38:32 +08:00
end
def restart
2020-09-18 15:26:14 +08:00
result = Ci::Drone::API.new(@ci_user.user_hash, @cloud_account.drone_url, @repo.repo_namespace, @repo.repo_name, number: params[:build]).restart
2020-08-13 23:38:32 +08:00
render json: result
end
2020-08-21 14:53:48 +08:00
def stop
2020-09-18 15:26:14 +08:00
result = Ci::Drone::API.new(@ci_user.user_hash, @cloud_account.drone_url, @repo.repo_namespace, @repo.repo_name, number: params[:build]).stop
2020-08-13 23:38:32 +08:00
render json: result
end
def logs
2020-09-11 15:05:03 +08:00
# TODO **待优化**
# 因直接操作ci库如下查询待优化可直接根据log id查询即可
build = @repo.builds.find_by(build_number: params[:build])
return render_not_found("Couldn't found build with 'number'= #{params[:build]}") if build.blank?
stage = build.stages.includes(steps: [:log]).find_by(stage_number: params[:stage])
return render_not_found("Couldn't found build with 'number'= #{params[:stage]}") if stage.blank?
step = stage.steps.find_by(step_number: params[:step])
return render_not_found("Couldn't found build with 'number'= #{params[:step]}") if step.blank?
log = step.log
2020-09-11 17:08:40 +08:00
result = log.blank? ? nil : (log.log_data[0..5].include?('null') ? nil : JSON.parse(log.log_data))
2020-09-11 15:05:03 +08:00
# result = Ci::Drone::API.new(@user.user_hash, @cloud_account.drone_url, @repo.repo_namespace, @repo.repo_name, build: params[:build], stage: params[:stage], step: params[:step]).logs
2020-08-13 23:38:32 +08:00
render json: result
end
end