Commit 1c7871e9 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Introduce get_build! so we could omit one early return

parent fab8bc53
...@@ -52,8 +52,7 @@ module API ...@@ -52,8 +52,7 @@ module API
get ':id/builds/:build_id' do get ':id/builds/:build_id' do
authorize_read_builds! authorize_read_builds!
build = get_build(params[:build_id]) build = get_build!(params[:build_id])
return not_found!(build) unless build
present build, with: Entities::Build, present build, with: Entities::Build,
user_can_download_artifacts: can?(current_user, :read_build, user_project) user_can_download_artifacts: can?(current_user, :read_build, user_project)
...@@ -69,8 +68,7 @@ module API ...@@ -69,8 +68,7 @@ module API
get ':id/builds/:build_id/artifacts' do get ':id/builds/:build_id/artifacts' do
authorize_read_builds! authorize_read_builds!
build = get_build(params[:build_id]) build = get_build!(params[:build_id])
return not_found!(build) unless build
artifacts_file = build.artifacts_file artifacts_file = build.artifacts_file
...@@ -97,8 +95,7 @@ module API ...@@ -97,8 +95,7 @@ module API
get ':id/builds/:build_id/trace' do get ':id/builds/:build_id/trace' do
authorize_read_builds! authorize_read_builds!
build = get_build(params[:build_id]) build = get_build!(params[:build_id])
return not_found!(build) unless build
header 'Content-Disposition', "infile; filename=\"#{build.id}.log\"" header 'Content-Disposition', "infile; filename=\"#{build.id}.log\""
content_type 'text/plain' content_type 'text/plain'
...@@ -118,8 +115,7 @@ module API ...@@ -118,8 +115,7 @@ module API
post ':id/builds/:build_id/cancel' do post ':id/builds/:build_id/cancel' do
authorize_update_builds! authorize_update_builds!
build = get_build(params[:build_id]) build = get_build!(params[:build_id])
return not_found!(build) unless build
build.cancel build.cancel
...@@ -137,8 +133,7 @@ module API ...@@ -137,8 +133,7 @@ module API
post ':id/builds/:build_id/retry' do post ':id/builds/:build_id/retry' do
authorize_update_builds! authorize_update_builds!
build = get_build(params[:build_id]) build = get_build!(params[:build_id])
return not_found!(build) unless build
return forbidden!('Build is not retryable') unless build.retryable? return forbidden!('Build is not retryable') unless build.retryable?
build = Ci::Build.retry(build, current_user) build = Ci::Build.retry(build, current_user)
...@@ -157,8 +152,7 @@ module API ...@@ -157,8 +152,7 @@ module API
post ':id/builds/:build_id/erase' do post ':id/builds/:build_id/erase' do
authorize_update_builds! authorize_update_builds!
build = get_build(params[:build_id]) build = get_build!(params[:build_id])
return not_found!(build) unless build
return forbidden!('Build is not erasable!') unless build.erasable? return forbidden!('Build is not erasable!') unless build.erasable?
build.erase(erased_by: current_user) build.erase(erased_by: current_user)
...@@ -176,8 +170,8 @@ module API ...@@ -176,8 +170,8 @@ module API
post ':id/builds/:build_id/artifacts/keep' do post ':id/builds/:build_id/artifacts/keep' do
authorize_update_builds! authorize_update_builds!
build = get_build(params[:build_id]) build = get_build!(params[:build_id])
return not_found!(build) unless build && build.artifacts? return not_found!(build) unless build.artifacts?
build.keep_artifacts! build.keep_artifacts!
...@@ -192,6 +186,10 @@ module API ...@@ -192,6 +186,10 @@ module API
user_project.builds.find_by(id: id.to_i) user_project.builds.find_by(id: id.to_i)
end end
def get_build!(id)
get_build(id) || not_found!
end
def filter_builds(builds, scope) def filter_builds(builds, scope)
return builds if scope.nil? || scope.empty? return builds if scope.nil? || scope.empty?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment