Commit 3eae0641 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Introduce Pipeline#latest and Pipeline.latest_for:

So that we could easily access it for the view
parent 6234b327
...@@ -21,8 +21,12 @@ module Ci ...@@ -21,8 +21,12 @@ module Ci
after_save :keep_around_commits after_save :keep_around_commits
# ref can't be HEAD or SHA, can only be branch/tag name # ref can't be HEAD or SHA, can only be branch/tag name
scope :latest_successful_for, ->(ref = default_branch) do scope :latest_successful_for, ->(ref) do
where(ref: ref).success.order(id: :desc).limit(1) latest(ref).success
end
scope :latest_for, ->(ref) do
where(ref: ref).order(id: :desc).limit(1)
end end
def self.truncate_sha(sha) def self.truncate_sha(sha)
...@@ -98,6 +102,10 @@ module Ci ...@@ -98,6 +102,10 @@ module Ci
end end
end end
def latest
project.pipelines.latest_for(ref).first
end
def latest? def latest?
return false unless ref return false unless ref
commit = project.commit(ref) commit = project.commit(ref)
......
require 'spec_helper' require 'spec_helper'
describe Ci::Pipeline, models: true do describe Ci::Pipeline, models: true do
let(:project) { FactoryGirl.create :empty_project } let(:project) { create(:empty_project) }
let(:pipeline) { FactoryGirl.create :ci_pipeline, project: project } let(:pipeline) { create(:ci_pipeline, project: project) }
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:user) }
...@@ -481,6 +481,50 @@ describe Ci::Pipeline, models: true do ...@@ -481,6 +481,50 @@ describe Ci::Pipeline, models: true do
end end
end end
context 'with non-empty project' do
let(:project) { create(:project) }
let(:pipeline) { create_pipeline }
describe '#latest?' do
context 'with latest sha' do
it 'returns true' do
expect(pipeline).to be_latest
end
end
context 'with not latest sha' do
before do
pipeline.update(
sha: project.commit("#{project.default_branch}~1").sha)
end
it 'returns false' do
expect(pipeline).not_to be_latest
end
end
end
describe '#latest' do
let(:previous_pipeline) { create_pipeline }
before do
previous_pipeline
pipeline
end
it 'gives the latest pipeline' do
expect(previous_pipeline.latest).to eq(pipeline)
end
end
def create_pipeline
create(:ci_pipeline,
project: project,
ref: project.default_branch,
sha: project.commit.sha)
end
end
describe '#manual_actions' do describe '#manual_actions' do
subject { pipeline.manual_actions } subject { pipeline.manual_actions }
......
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