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