Commit 0a9d9f7d authored by Lin Jen-Shin's avatar Lin Jen-Shin

Fetch the current SHA if SHA was not passed

parent 4b559c9a
......@@ -1086,7 +1086,8 @@ class Project < ActiveRecord::Base
!namespace.share_with_group_lock
end
def pipeline_for(ref, sha)
def pipeline_for(ref, sha = commit(ref).try(:sha))
return unless sha
pipelines.order(id: :desc).find_by(sha: sha, ref: ref)
end
......
......@@ -684,23 +684,37 @@ describe Project, models: true do
end
end
describe '#pipeline' do
let(:project) { create :project }
let(:pipeline) { create :ci_pipeline, project: project, ref: 'master' }
subject { project.pipeline_for('master', pipeline.sha) }
describe '#pipeline_for' do
let(:project) { create(:project) }
let!(:pipeline) { create_pipeline }
shared_examples 'giving the correct pipeline' do
it { is_expected.to eq(pipeline) }
context 'return latest' do
let(:pipeline2) { create :ci_pipeline, project: project, ref: 'master' }
let!(:pipeline2) { create_pipeline }
before do
pipeline
pipeline2
it { is_expected.to eq(pipeline2) }
end
end
it { is_expected.to eq(pipeline2) }
context 'with explicit sha' do
subject { project.pipeline_for('master', pipeline.sha) }
it_behaves_like 'giving the correct pipeline'
end
context 'with implicit sha' do
subject { project.pipeline_for('master') }
it_behaves_like 'giving the correct pipeline'
end
def create_pipeline
create(:ci_pipeline,
project: project,
ref: 'master',
sha: project.commit('master').sha)
end
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