Commit 49534a65 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'fix/14388' into 'master'

Fix an issue when the target branch of a MR had been deleted

Before displaying the "diverged commits" note, we're checking if the MR
is open, but we should check if it's mergeable instead because this
check ensure the source and target branches exist.

This was introduced by !2217 and fixes #14388.

See merge request !3294
parents 5203a984 bc92de8f
......@@ -43,6 +43,7 @@ v 8.6.0 (unreleased)
- Increase the notes polling timeout over time (Roberto Dip)
- Add shortcut to toggle markdown preview (Florent Baldino)
- Show labels in dashboard and group milestone views
- Fix an issue when the target branch of a MR had been deleted
- Add main language of a project in the list of projects (Tiago Botelho)
- Add #upcoming filter to Milestone filter (Tiago Botelho)
- Add ability to show archived projects on dashboard, explore and group pages
......
......@@ -516,7 +516,7 @@ class MergeRequest < ActiveRecord::Base
end
def target_sha
@target_sha ||= target_project.repository.commit(target_branch).sha
@target_sha ||= target_project.repository.commit(target_branch).try(:sha)
end
def source_sha
......@@ -572,8 +572,11 @@ class MergeRequest < ActiveRecord::Base
end
def compute_diverged_commits_count
return 0 unless source_sha && target_sha
Gitlab::Git::Commit.between(target_project.repository.raw_repository, source_sha, target_sha).size
end
private :compute_diverged_commits_count
def diverged_from_target_branch?
diverged_commits_count > 0
......
......@@ -86,6 +86,16 @@ describe MergeRequest, models: true do
end
end
describe '#target_sha' do
context 'when the target branch does not exist anymore' do
subject { create(:merge_request).tap { |mr| mr.update_attribute(:target_branch, 'deleted') } }
it 'returns nil' do
expect(subject.target_sha).to be_nil
end
end
end
describe '#source_sha' do
let(:last_branch_commit) { subject.source_project.repository.commit(subject.source_branch) }
......@@ -310,6 +320,18 @@ describe MergeRequest, models: true do
let(:project) { create(:project) }
let(:fork_project) { create(:project, forked_from_project: project) }
context 'when the target branch does not exist anymore' do
subject { create(:merge_request).tap { |mr| mr.update_attribute(:target_branch, 'deleted') } }
it 'does not crash' do
expect{ subject.diverged_commits_count }.not_to raise_error
end
it 'returns 0' do
expect(subject.diverged_commits_count).to eq(0)
end
end
context 'diverged on same repository' do
subject(:merge_request_with_divergence) { create(:merge_request, :diverged, source_project: project, target_project: project) }
......
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