From 7ac32ae282fa2d35a3651de08f35aad1b85ffca0 Mon Sep 17 00:00:00 2001 From: Steve Azzopardi Date: Thu, 6 Dec 2018 11:25:25 +0100 Subject: Refactor project.latest_successful_builds_for def `project.latest_successful_builds_for(ref)` is being used to find a single job all the time. This results into us having to call `find_by` inside of the controller which violates our CodeReuse/ActiveRecord rubocop rule. Refactor `project.latest_successful_builds_for(ref)` to `project.latest_successful_build_for(job_name, ref)` which will execute the `find_by` inside of the model. Also create `project.latest_successful_build_for!(job_name, ref)` which raises an exception instead of returning nil. --- lib/api/job_artifacts.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/api/job_artifacts.rb b/lib/api/job_artifacts.rb index a4068a200b3..933bd067e26 100644 --- a/lib/api/job_artifacts.rb +++ b/lib/api/job_artifacts.rb @@ -23,17 +23,14 @@ module API requires :job, type: String, desc: 'The name for the job' end route_setting :authentication, job_token_allowed: true - # rubocop: disable CodeReuse/ActiveRecord get ':id/jobs/artifacts/:ref_name/download', requirements: { ref_name: /.+/ } do authorize_download_artifacts! - builds = user_project.latest_successful_builds_for(params[:ref_name]) - latest_build = builds.find_by!(name: params[:job]) + latest_build = user_project.latest_successful_build_for!(params[:job], params[:ref_name]) present_carrierwave_file!(latest_build.artifacts_file) end - # rubocop: enable CodeReuse/ActiveRecord desc 'Download a specific file from artifacts archive from a ref' do detail 'This feature was introduced in GitLab 11.5' @@ -48,7 +45,7 @@ module API requirements: { ref_name: /.+/ } do authorize_download_artifacts! - build = user_project.latest_successful_build_for(params[:job], params[:ref_name]) + build = user_project.latest_successful_build_for!(params[:job], params[:ref_name]) path = Gitlab::Ci::Build::Artifacts::Path .new(params[:artifact_path]) -- cgit v1.2.3