diff options
| author | Lin Jen-Shin <godfat@godfat.org> | 2016-08-10 18:40:10 +0800 |
|---|---|---|
| committer | Lin Jen-Shin <godfat@godfat.org> | 2016-08-10 18:40:10 +0800 |
| commit | 3eae0641ef0708f9b223abbe0070e332ea0b20ac (patch) | |
| tree | 89e0983b50ae3584b58e99dec065857bdef4a270 | |
| parent | 6234b32785061330e62c5681509cec5bd98e5302 (diff) | |
| download | gitlab-ce-3eae0641ef0708f9b223abbe0070e332ea0b20ac.tar.gz | |
Introduce Pipeline#latest and Pipeline.latest_for:
So that we could easily access it for the view
| -rw-r--r-- | app/models/ci/pipeline.rb | 12 | ||||
| -rw-r--r-- | spec/models/ci/pipeline_spec.rb | 48 |
2 files changed, 56 insertions, 4 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index bce6a992af6..bc1190537da 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -21,8 +21,12 @@ module Ci after_save :keep_around_commits # ref can't be HEAD or SHA, can only be branch/tag name - scope :latest_successful_for, ->(ref = default_branch) do - where(ref: ref).success.order(id: :desc).limit(1) + scope :latest_successful_for, ->(ref) do + latest(ref).success + end + + scope :latest_for, ->(ref) do + where(ref: ref).order(id: :desc).limit(1) end def self.truncate_sha(sha) @@ -98,6 +102,10 @@ module Ci end end + def latest + project.pipelines.latest_for(ref).first + end + def latest? return false unless ref commit = project.commit(ref) diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index ccee591cf7a..556a6e1b59a 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' describe Ci::Pipeline, models: true do - let(:project) { FactoryGirl.create :empty_project } - let(:pipeline) { FactoryGirl.create :ci_pipeline, project: project } + let(:project) { create(:empty_project) } + let(:pipeline) { create(:ci_pipeline, project: project) } it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:user) } @@ -481,6 +481,50 @@ describe Ci::Pipeline, models: true do 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 subject { pipeline.manual_actions } |
