Render coverage badge for latest successful pipeline
This commit is contained in:
		
							parent
							
								
									ac73de508e
								
							
						
					
					
						commit
						42496ecef7
					
				| 
						 | 
					@ -13,8 +13,7 @@ module Gitlab
 | 
				
			||||||
          @job = job
 | 
					          @job = job
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          @pipeline = @project.pipelines
 | 
					          @pipeline = @project.pipelines
 | 
				
			||||||
            .where(ref: @ref)
 | 
					            .latest_successful_for(@ref)
 | 
				
			||||||
            .where(sha: @project.commit(@ref).try(:sha))
 | 
					 | 
				
			||||||
            .first
 | 
					            .first
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -44,45 +44,49 @@ describe Gitlab::Badge::Coverage::Report do
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  context 'pipeline exists' do
 | 
					  context 'when latest successful pipeline exists' do
 | 
				
			||||||
    let!(:pipeline) do
 | 
					    before do
 | 
				
			||||||
      create(:ci_pipeline, project: project,
 | 
					      create_pipeline do |pipeline|
 | 
				
			||||||
                           sha: project.commit.id,
 | 
					        create(:ci_build, :success, pipeline: pipeline, name: 'first', coverage: 40)
 | 
				
			||||||
                           ref: 'master')
 | 
					        create(:ci_build, :success, pipeline: pipeline, coverage: 60)
 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    context 'builds exist' do
 | 
					 | 
				
			||||||
      before do
 | 
					 | 
				
			||||||
        create(:ci_build, name: 'first', pipeline: pipeline, coverage: 40)
 | 
					 | 
				
			||||||
        create(:ci_build, pipeline: pipeline, coverage: 60)
 | 
					 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      context 'particular job specified' do
 | 
					      create_pipeline do |pipeline|
 | 
				
			||||||
        let(:job_name) { 'first' }
 | 
					        create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
 | 
				
			||||||
 | 
					 | 
				
			||||||
        it 'returns coverage for the particular job' do
 | 
					 | 
				
			||||||
          expect(badge.status).to eq 40
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      context 'particular job not specified' do
 | 
					 | 
				
			||||||
        let(:job_name) { '' }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        it 'returns arithemetic mean for the pipeline' do
 | 
					 | 
				
			||||||
          expect(badge.status).to eq 50
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    context 'builds do not exist' do
 | 
					    context 'when particular job specified' do
 | 
				
			||||||
      it_behaves_like 'unknown coverage report'
 | 
					      let(:job_name) { 'first' }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      context 'particular job specified' do
 | 
					      it 'returns coverage for the particular job' do
 | 
				
			||||||
        let(:job_name) { 'nonexistent' }
 | 
					        expect(badge.status).to eq 40
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        it 'retruns nil' do
 | 
					    context 'when particular job not specified' do
 | 
				
			||||||
          expect(badge.status).to be_nil
 | 
					      let(:job_name) { '' }
 | 
				
			||||||
        end
 | 
					
 | 
				
			||||||
 | 
					      it 'returns arithemetic mean for the pipeline' do
 | 
				
			||||||
 | 
					        expect(badge.status).to eq 50
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  context 'when only failed pipeline exists' do
 | 
				
			||||||
 | 
					    before do
 | 
				
			||||||
 | 
					      create_pipeline do |pipeline|
 | 
				
			||||||
 | 
					        create(:ci_build, :failed, pipeline: pipeline, coverage: 10)
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it_behaves_like 'unknown coverage report'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    context 'particular job specified' do
 | 
				
			||||||
 | 
					      let(:job_name) { 'nonexistent' }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it 'retruns nil' do
 | 
				
			||||||
 | 
					        expect(badge.status).to be_nil
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
| 
						 | 
					@ -90,4 +94,13 @@ describe Gitlab::Badge::Coverage::Report do
 | 
				
			||||||
  context 'pipeline does not exist' do
 | 
					  context 'pipeline does not exist' do
 | 
				
			||||||
    it_behaves_like 'unknown coverage report'
 | 
					    it_behaves_like 'unknown coverage report'
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def create_pipeline
 | 
				
			||||||
 | 
					    opts = { project: project, sha: project.commit.id, ref: 'master' }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    create(:ci_pipeline, opts).tap do |pipeline|
 | 
				
			||||||
 | 
					      yield pipeline
 | 
				
			||||||
 | 
					      pipeline.build_updated
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue