Commit 53a9dee6 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Introduce Project#latest_success_builds_for:

So it's more accessible for views to access the names of jobs.
Only filter Build#name from where we really need to download it.
parent 4bb3787e
......@@ -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
......
......@@ -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
......
......@@ -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)
......
......@@ -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
......
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