diff --git a/app/controllers/projects/artifacts_controller.rb b/app/controllers/projects/artifacts_controller.rb index 3e487c24cbd901fcfdf50849b78d2559a80d96e8..1b3c4ec9bd811f0396e803eab6e586e1fc33ddc9 100644 --- a/app/controllers/projects/artifacts_controller.rb +++ b/app/controllers/projects/artifacts_controller.rb @@ -61,9 +61,9 @@ class Projects::ArtifactsController < Projects::ApplicationController def build_from_ref if params[:ref_name] - builds = project.builds_for(params[:job], params[:ref_name]) + builds = project.latest_success_builds_for(params[:ref_name]) - builds.success.latest.first + builds.where(name: params[:job]).first end end diff --git a/app/models/project.rb b/app/models/project.rb index 793cf2d70fb7929b8716456d473f4841a0850778..384841dbb9ae6f57f00c7f3f100f1297cbc30101 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -429,15 +429,18 @@ class Project < ActiveRecord::Base repository.commit(ref) end - def builds_for(build_name, ref = 'HEAD') + def latest_success_builds_for(ref = 'HEAD') + builds_for(ref).success.latest + end + + def builds_for(ref = 'HEAD') commit_object = commit(ref) if commit_object.nil? builds.none else builds.joins(:pipeline). - merge(Ci::Pipeline.where(sha: commit_object.sha)). - where(name: build_name) + merge(Ci::Pipeline.where(sha: commit_object.sha)) end end diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 237a88adcc7867ee4d307ea0d37ebffa9508444b..53774b5c10f9ad2593071319027ae993b4b9ad14 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -83,9 +83,8 @@ module API # GET /projects/:id/artifacts/:ref_name/download?job=name get ':id/builds/artifacts/:ref_name/download', requirements: { ref_name: /.+/ } do - builds = user_project.builds_for(params[:job], params[:ref_name]) - - latest_build = builds.success.latest.first + builds = user_project.latest_success_builds_for(params[:ref_name]) + latest_build = builds.where(name: params[:job]).first if latest_build present_artifact!(latest_build.artifacts_file) diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index b1354faa722112f0b58c6892ac7bc677c6c5ee17..7c95463a57142fe535ca910b82dc5fb06f05b290 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -691,15 +691,19 @@ describe Ci::Build, models: true do end end - describe 'Project#builds_for' do - it 'returns builds from ref and build name' do - build_ids = project.builds_for(build.name, 'HEAD').map(&:id) + describe 'Project#latest_success_builds_for' do + before do + build.update(status: 'success') + end + + it 'returns builds from ref' do + build_ids = project.latest_success_builds_for('HEAD').map(&:id) expect(build_ids).to eq([build.id]) end it 'returns empty relation if the build cannot be found' do - builds = project.builds_for(build.name, 'TAIL').all + builds = project.latest_success_builds_for('TAIL').all expect(builds).to be_empty end