Commit cae0fa7c authored by Kamil Trzcinski's avatar Kamil Trzcinski

Properly select a list of Pipelines for a Merge Requests

parent 0d6d7f6e
......@@ -137,7 +137,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end
def pipelines
@pipelines = Ci::Pipeline.where(ref: @merge_request.source_branch)
@pipelines = @merge_request.all_pipelines
respond_to do |format|
format.html do
......
......@@ -642,10 +642,21 @@ class MergeRequest < ActiveRecord::Base
diverged_commits_count > 0
end
def commits_sha
commits.map(&:sha)
end
def pipeline
@pipeline ||= source_project.pipeline(diff_head_sha, source_branch) if diff_head_sha && source_project
end
def all_pipelines
@all_pipelines ||=
if diff_head_sha && source_project
source_project.pipelines.order(id: :desc).where(sha: commits_sha, ref: source_branch)
end
end
def merge_commit
@merge_commit ||= project.commit(merge_commit_sha) if merge_commit_sha
end
......
......@@ -419,6 +419,20 @@ describe MergeRequest, models: true do
subject { create :merge_request, :simple }
end
describe '#commits_sha' do
let(:commit0) { double('commit0', sha: 'sha1') }
let(:commit1) { double('commit1', sha: 'sha2') }
let(:commit2) { double('commit2', sha: 'sha3') }
before do
allow(subject).to receive(:commits).and_return([commit0, commit1, commit2])
end
it 'returns sha of commits' do
expect(subject.commits_sha).to contain_exactly('sha1', 'sha2', 'sha3')
end
end
describe '#pipeline' do
describe 'when the source project exists' do
it 'returns the latest pipeline' do
......@@ -443,6 +457,24 @@ describe MergeRequest, models: true do
end
end
describe '#all_pipelines' do
let(:commit0) { double('commit0', sha: 'sha1') }
let(:commit1) { double('commit1', sha: 'sha2') }
let(:commit2) { double('commit2', sha: 'sha3') }
let!(:pipeline) { create(:ci_empty_pipeline, project: subject.source_project, sha: 'sha1', ref: subject.source_branch) }
let!(:pipeline2) { create(:ci_empty_pipeline, project: subject.source_project, sha: 'sha1', ref: subject.source_branch) }
let!(:pipeline3) { create(:ci_empty_pipeline, project: subject.source_project, sha: 'sha2', ref: subject.source_branch) }
let!(:pipeline4) { create(:ci_empty_pipeline, project: subject.target_project, sha: 'sha1', ref: subject.target_branch) }
before do
allow(subject).to receive(:commits).and_return([commit0, commit1, commit2])
end
it 'returns a pipelines from source projects' do
expect(subject.all_pipelines).to eq([pipeline3, pipeline2, pipeline])
end
end
describe '#participants' do
let(:project) { create(:project, :public) }
......
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