Rename latest_successful to be more explicit

* Reword Project#latest_successful_build_for to
Project#latest_successful_build_for_ref
* Reword Ci::Pipeline#latest_successful_for to
Ci::Pipeline#latest_successful_build_for_ref
This commit is contained in:
Matija Čupić 2019-07-23 21:29:12 +02:00
parent 1eab065714
commit 38ab1ae2f2
No known key found for this signature in database
GPG key ID: 4BAF84FFACD2E5DE
7 changed files with 29 additions and 28 deletions

View file

@ -51,6 +51,6 @@ class Projects::BuildArtifactsController < Projects::ApplicationController
def job_from_ref
return unless @ref_name
project.latest_successful_build_for(params[:job], @ref_name)
project.latest_successful_build_for_ref(params[:job], @ref_name)
end
end

View file

@ -229,6 +229,7 @@ module Ci
#
# ref - The name (or names) of the branch(es)/tag(s) to limit the list of
# pipelines to.
# sha - The commit SHA (or mutliple SHAs) to limit the list of pipelines to.
# limit - This limits a backlog search, default to 100.
def self.newest_first(ref: nil, sha: nil, limit: 100)
relation = order(id: :desc)
@ -249,7 +250,7 @@ module Ci
newest_first(ref: ref).pluck(:status).first
end
def self.latest_successful_for(ref)
def self.latest_successful_for_ref(ref)
newest_first(ref: ref).success.take
end

View file

@ -720,16 +720,16 @@ class Project < ApplicationRecord
end
# ref can't be HEAD, can only be branch/tag name
def latest_successful_build_for(job_name, ref = default_branch)
def latest_successful_build_for_ref(job_name, ref = default_branch)
return unless ref
latest_pipeline = ci_pipelines.latest_successful_for(ref)
latest_pipeline = ci_pipelines.latest_successful_for_ref(ref)
return unless latest_pipeline
latest_pipeline.builds.latest.with_artifacts_archive.find_by(name: job_name)
end
def latest_successful_build_for_sha(job_name, sha = commit(default_branch).id)
def latest_successful_build_for_sha(job_name, sha)
return unless sha
latest_pipeline = ci_pipelines.latest_successful_for_sha(sha)
@ -738,8 +738,8 @@ class Project < ApplicationRecord
latest_pipeline.builds.latest.with_artifacts_archive.find_by(name: job_name)
end
def latest_successful_build_for!(job_name, ref = default_branch)
latest_successful_build_for(job_name, ref) || raise(ActiveRecord::RecordNotFound.new("Couldn't find job #{job_name}"))
def latest_successful_build_for_ref!(job_name, ref = default_branch)
latest_successful_build_for_ref(job_name, ref) || raise(ActiveRecord::RecordNotFound.new("Couldn't find job #{job_name}"))
end
def merge_base_commit(first_commit_id, second_commit_id)
@ -1514,12 +1514,12 @@ class Project < ApplicationRecord
end
@latest_successful_pipeline_for_default_branch =
ci_pipelines.latest_successful_for(default_branch)
ci_pipelines.latest_successful_for_ref(default_branch)
end
def latest_successful_pipeline_for(ref = nil)
if ref && ref != default_branch
ci_pipelines.latest_successful_for(ref)
ci_pipelines.latest_successful_for_ref(ref)
else
latest_successful_pipeline_for_default_branch
end

View file

@ -27,7 +27,7 @@ module API
requirements: { ref_name: /.+/ } do
authorize_download_artifacts!
latest_build = user_project.latest_successful_build_for!(params[:job], params[:ref_name])
latest_build = user_project.latest_successful_build_for_ref!(params[:job], params[:ref_name])
present_carrierwave_file!(latest_build.artifacts_file)
end
@ -45,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_ref!(params[:job], params[:ref_name])
path = Gitlab::Ci::Build::Artifacts::Path
.new(params[:artifact_path])

View file

@ -14,7 +14,7 @@ module Gitlab
@ref = ref
@job = job
@pipeline = @project.ci_pipelines.latest_successful_for(@ref)
@pipeline = @project.ci_pipelines.latest_successful_for_ref(@ref)
end
def entity

View file

@ -1799,7 +1799,7 @@ describe Ci::Pipeline, :mailer do
end
end
describe '.latest_successful_for' do
describe '.latest_successful_for_ref' do
include_context 'with some outdated pipelines'
let!(:latest_successful_pipeline) do
@ -1807,7 +1807,7 @@ describe Ci::Pipeline, :mailer do
end
it 'returns the latest successful pipeline' do
expect(described_class.latest_successful_for('ref'))
expect(described_class.latest_successful_for_ref('ref'))
.to eq(latest_successful_pipeline)
end
end

View file

@ -2019,7 +2019,7 @@ describe Project do
end
end
describe '#latest_successful_build_for' do
describe '#latest_successful_build_for_ref' do
let(:project) { create(:project, :repository) }
let(:pipeline) { create_pipeline(project) }
@ -2032,7 +2032,7 @@ describe Project do
build1_p2 = create_build(pipeline2, 'test')
create_build(pipeline2, 'test2')
expect(project.latest_successful_build_for(build1_p2.name))
expect(project.latest_successful_build_for_ref(build1_p2.name))
.to eq(build1_p2)
end
end
@ -2042,12 +2042,12 @@ describe Project do
context 'standalone pipeline' do
it 'returns builds for ref for default_branch' do
expect(project.latest_successful_build_for(build.name))
expect(project.latest_successful_build_for_ref(build.name))
.to eq(build)
end
it 'returns empty relation if the build cannot be found' do
expect(project.latest_successful_build_for('TAIL'))
expect(project.latest_successful_build_for_ref('TAIL'))
.to be_nil
end
end
@ -2058,7 +2058,7 @@ describe Project do
end
it 'gives the latest build from latest pipeline' do
expect(project.latest_successful_build_for(build.name))
expect(project.latest_successful_build_for_ref(build.name))
.to eq(build)
end
end
@ -2069,7 +2069,7 @@ describe Project do
pipeline.update(status: 'pending')
pending_build = create_build(pipeline)
expect(project.latest_successful_build_for(pending_build.name)).to be_nil
expect(project.latest_successful_build_for_ref(pending_build.name)).to be_nil
end
end
end
@ -2129,7 +2129,7 @@ describe Project do
end
end
describe '#latest_successful_build_for!' do
describe '#latest_successful_build_for_ref!' do
let(:project) { create(:project, :repository) }
let(:pipeline) { create_pipeline(project) }
@ -2142,7 +2142,7 @@ describe Project do
build1_p2 = create_build(pipeline2, 'test')
create_build(pipeline2, 'test2')
expect(project.latest_successful_build_for(build1_p2.name))
expect(project.latest_successful_build_for_ref!(build1_p2.name))
.to eq(build1_p2)
end
end
@ -2152,12 +2152,12 @@ describe Project do
context 'standalone pipeline' do
it 'returns builds for ref for default_branch' do
expect(project.latest_successful_build_for!(build.name))
expect(project.latest_successful_build_for_ref!(build.name))
.to eq(build)
end
it 'returns exception if the build cannot be found' do
expect { project.latest_successful_build_for!(build.name, 'TAIL') }
expect { project.latest_successful_build_for_ref!(build.name, 'TAIL') }
.to raise_error(ActiveRecord::RecordNotFound)
end
end
@ -2168,7 +2168,7 @@ describe Project do
end
it 'gives the latest build from latest pipeline' do
expect(project.latest_successful_build_for!(build.name))
expect(project.latest_successful_build_for_ref!(build.name))
.to eq(build)
end
end
@ -2179,7 +2179,7 @@ describe Project do
pipeline.update(status: 'pending')
pending_build = create_build(pipeline)
expect { project.latest_successful_build_for!(pending_build.name) }
expect { project.latest_successful_build_for_ref!(pending_build.name) }
.to raise_error(ActiveRecord::RecordNotFound)
end
end
@ -4091,7 +4091,7 @@ describe Project do
context 'with a ref that is not the default branch' do
it 'returns the latest successful pipeline for the given ref' do
expect(project.ci_pipelines).to receive(:latest_successful_for).with('foo')
expect(project.ci_pipelines).to receive(:latest_successful_for_ref).with('foo')
project.latest_successful_pipeline_for('foo')
end
@ -4119,7 +4119,7 @@ describe Project do
it 'memoizes and returns the latest successful pipeline for the default branch' do
pipeline = double(:pipeline)
expect(project.ci_pipelines).to receive(:latest_successful_for)
expect(project.ci_pipelines).to receive(:latest_successful_for_ref)
.with(project.default_branch)
.and_return(pipeline)
.once