Commit 9bcc9ec1 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'display-mr-link-if-thre-is-one' into 'master'

Replaces "Create merge request" link with one to the MR when one exists

Fixes #3716

* Without MR:

![before-1](/uploads/abcb2eb5bb5927a83c2f1700528f852c/before-1.png)

* With MR:

![after-1](/uploads/9020b871e1aa08d3e5a45359336ef0f2/after-1.png)

* Without MR:

![before-2](/uploads/bb3bd1a74efc193ad31af68f1e04ef5f/before-2.png)

* With MR:

![after-2](/uploads/789d32aef12f39c605e665d0c4bc9fbe/after-2.png)

* Without MR:

![before-3](/uploads/a9aff7a11eb1069be24c37a5455be809/before-3.png)

* With MR:

![after-3](/uploads/48fe81ea50657fe5b2de0d25260d8e4b/after-3.png)

See merge request !2670
parents 42607a7f c1541fdf
...@@ -47,6 +47,7 @@ v 8.5.0 (unreleased) ...@@ -47,6 +47,7 @@ v 8.5.0 (unreleased)
- Faster snippet search - Faster snippet search
- Title for milestones should be unique (Zeger-Jan van de Weg) - Title for milestones should be unique (Zeger-Jan van de Weg)
- Validate correctness of maximum attachment size application setting - Validate correctness of maximum attachment size application setting
- Replaces "Create merge request" link with one to the "Merge Request" when one exists
v 8.4.4 v 8.4.4
- Update omniauth-saml gem to 1.4.2 - Update omniauth-saml gem to 1.4.2
......
...@@ -21,6 +21,9 @@ class Projects::CommitsController < Projects::ApplicationController ...@@ -21,6 +21,9 @@ class Projects::CommitsController < Projects::ApplicationController
@note_counts = project.notes.where(commit_id: @commits.map(&:id)). @note_counts = project.notes.where(commit_id: @commits.map(&:id)).
group(:commit_id).count group(:commit_id).count
@merge_request = @project.merge_requests.opened.
find_by(source_project: @project, source_branch: @ref, target_branch: @repository.root_ref)
respond_to do |format| respond_to do |format|
format.html format.html
format.json { pager_json("projects/commits/_commits", @commits.size) } format.json { pager_json("projects/commits/_commits", @commits.size) }
......
...@@ -4,24 +4,23 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -4,24 +4,23 @@ class Projects::CompareController < Projects::ApplicationController
# Authorize # Authorize
before_action :require_non_empty_project before_action :require_non_empty_project
before_action :authorize_download_code! before_action :authorize_download_code!
before_action :assign_ref_vars, only: [:index, :show]
before_action :merge_request, only: [:index, :show]
def index def index
@ref = Addressable::URI.unescape(params[:to])
end end
def show def show
base_ref = Addressable::URI.unescape(params[:from])
@ref = head_ref = Addressable::URI.unescape(params[:to])
diff_options = { ignore_whitespace_change: true } if params[:w] == '1' diff_options = { ignore_whitespace_change: true } if params[:w] == '1'
compare_result = CompareService.new. compare_result = CompareService.new.
execute(@project, head_ref, @project, base_ref, diff_options) execute(@project, @head_ref, @project, @base_ref, diff_options)
if compare_result if compare_result
@commits = Commit.decorate(compare_result.commits, @project) @commits = Commit.decorate(compare_result.commits, @project)
@diffs = compare_result.diffs @diffs = compare_result.diffs
@commit = @project.commit(head_ref) @commit = @project.commit(@head_ref)
@base_commit = @project.merge_base_commit(base_ref, head_ref) @base_commit = @project.merge_base_commit(@base_ref, @head_ref)
@diff_refs = [@base_commit, @commit] @diff_refs = [@base_commit, @commit]
@line_notes = [] @line_notes = []
end end
...@@ -31,4 +30,16 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -31,4 +30,16 @@ class Projects::CompareController < Projects::ApplicationController
redirect_to namespace_project_compare_path(@project.namespace, @project, redirect_to namespace_project_compare_path(@project.namespace, @project,
params[:from], params[:to]) params[:from], params[:to])
end end
private
def assign_ref_vars
@base_ref = Addressable::URI.unescape(params[:from])
@ref = @head_ref = Addressable::URI.unescape(params[:to])
end
def merge_request
@merge_request ||= @project.merge_requests.opened.
find_by(source_project: @project, source_branch: @head_ref, target_branch: @base_ref)
end
end end
...@@ -137,6 +137,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -137,6 +137,7 @@ class MergeRequest < ActiveRecord::Base
scope :by_milestone, ->(milestone) { where(milestone_id: milestone) } scope :by_milestone, ->(milestone) { where(milestone_id: milestone) }
scope :in_projects, ->(project_ids) { where("source_project_id in (:project_ids) OR target_project_id in (:project_ids)", project_ids: project_ids) } scope :in_projects, ->(project_ids) { where("source_project_id in (:project_ids) OR target_project_id in (:project_ids)", project_ids: project_ids) }
scope :of_projects, ->(ids) { where(target_project_id: ids) } scope :of_projects, ->(ids) { where(target_project_id: ids) }
scope :opened, -> { with_state(:opened) }
scope :merged, -> { with_state(:merged) } scope :merged, -> { with_state(:merged) }
scope :closed, -> { with_state(:closed) } scope :closed, -> { with_state(:closed) }
scope :closed_and_merged, -> { with_states(:closed, :merged) } scope :closed_and_merged, -> { with_states(:closed, :merged) }
......
...@@ -11,7 +11,10 @@ ...@@ -11,7 +11,10 @@
= render 'shared/ref_switcher', destination: 'commits' = render 'shared/ref_switcher', destination: 'commits'
.block-controls.hidden-xs.hidden-sm .block-controls.hidden-xs.hidden-sm
- if create_mr_button?(@repository.root_ref, @ref) - if @merge_request.present?
.control
= link_to "View Open Merge Request", namespace_project_merge_request_path(@project.namespace, @project, @merge_request), class: 'btn'
- elsif create_mr_button?(@repository.root_ref, @ref)
.control .control
= link_to create_mr_path(@repository.root_ref, @ref), class: 'btn btn-success' do = link_to create_mr_path(@repository.root_ref, @ref), class: 'btn btn-success' do
= icon('plus') = icon('plus')
......
...@@ -13,12 +13,13 @@ ...@@ -13,12 +13,13 @@
= text_field_tag :to, params[:to], class: "form-control", required: true = text_field_tag :to, params[:to], class: "form-control", required: true
&nbsp; &nbsp;
= button_tag "Compare", class: "btn btn-create commits-compare-btn" = button_tag "Compare", class: "btn btn-create commits-compare-btn"
- if create_mr_button? - if @merge_request.present?
= link_to "View Open Merge Request", namespace_project_merge_request_path(@project.namespace, @project, @merge_request), class: 'prepend-left-10 btn'
- elsif create_mr_button?
= link_to create_mr_path, class: 'prepend-left-10 btn' do = link_to create_mr_path, class: 'prepend-left-10 btn' do
= icon("plus") = icon("plus")
Create Merge Request Create Merge Request
:javascript :javascript
var availableTags = #{@project.repository.ref_names.to_json}; var availableTags = #{@project.repository.ref_names.to_json};
......
...@@ -7,6 +7,26 @@ Feature: Project Commits ...@@ -7,6 +7,26 @@ Feature: Project Commits
Scenario: I browse commits list for master branch Scenario: I browse commits list for master branch
Then I see project commits Then I see project commits
And I should not see button to create a new merge request
Then I click the "Compare" tab
And I should not see button to create a new merge request
Scenario: I browse commits list for feature branch without a merge request
Given I visit commits list page for feature branch
Then I see feature branch commits
And I see button to create a new merge request
Then I click the "Compare" tab
And I see button to create a new merge request
Scenario: I browse commits list for feature branch with an open merge request
Given project have an open merge request
And I visit commits list page for feature branch
Then I see feature branch commits
And I should not see button to create a new merge request
And I should see button to the merge request
Then I click the "Compare" tab
And I should not see button to create a new merge request
And I should see button to the merge request
Scenario: I browse atom feed of commits list for master branch Scenario: I browse atom feed of commits list for master branch
Given I click atom feed link Given I click atom feed link
...@@ -30,6 +50,22 @@ Feature: Project Commits ...@@ -30,6 +50,22 @@ Feature: Project Commits
And I click side-by-side diff button And I click side-by-side diff button
Then I see inline diff button Then I see inline diff button
@javascript
Scenario: I compare branches without a merge request
Given I visit compare refs page
And I fill compare fields with branches
Then I see compared branches
And I see button to create a new merge request
@javascript
Scenario: I compare branches with an open merge request
Given project have an open merge request
And I visit compare refs page
And I fill compare fields with branches
Then I see compared branches
And I should not see button to create a new merge request
And I should see button to the merge request
@javascript @javascript
Scenario: I compare refs Scenario: I compare refs
Given I visit compare refs page Given I visit compare refs page
......
...@@ -33,6 +33,13 @@ class Spinach::Features::ProjectCommits < Spinach::FeatureSteps ...@@ -33,6 +33,13 @@ class Spinach::Features::ProjectCommits < Spinach::FeatureSteps
expect(page).to have_content "Showing #{sample_commit.files_changed_count} changed files" expect(page).to have_content "Showing #{sample_commit.files_changed_count} changed files"
end end
step 'I fill compare fields with branches' do
fill_in 'from', with: 'feature'
fill_in 'to', with: 'master'
click_button 'Compare'
end
step 'I fill compare fields with refs' do step 'I fill compare fields with refs' do
fill_in "from", with: sample_commit.parent_id fill_in "from", with: sample_commit.parent_id
fill_in "to", with: sample_commit.id fill_in "to", with: sample_commit.id
...@@ -56,6 +63,56 @@ class Spinach::Features::ProjectCommits < Spinach::FeatureSteps ...@@ -56,6 +63,56 @@ class Spinach::Features::ProjectCommits < Spinach::FeatureSteps
expect(page).to have_content "Showing 2 changed files" expect(page).to have_content "Showing 2 changed files"
end end
step 'I visit commits list page for feature branch' do
visit namespace_project_commits_path(@project.namespace, @project, 'feature', { limit: 5 })
end
step 'I see feature branch commits' do
commit = @project.repository.commit('0b4bc9a')
expect(page).to have_content(@project.name)
expect(page).to have_content(commit.message[0..12])
expect(page).to have_content(commit.short_id)
end
step 'project have an open merge request' do
create(:merge_request,
title: 'Feature',
source_project: @project,
source_branch: 'feature',
target_branch: 'master',
author: @project.users.first
)
end
step 'I click the "Compare" tab' do
click_link('Compare')
end
step 'I fill compare fields with branches' do
fill_in 'from', with: 'master'
fill_in 'to', with: 'feature'
click_button 'Compare'
end
step 'I see compared branches' do
expect(page).to have_content 'Commits (1)'
expect(page).to have_content 'Showing 1 changed file with 5 additions and 0 deletions'
end
step 'I see button to create a new merge request' do
expect(page).to have_link 'Create Merge Request'
end
step 'I should not see button to create a new merge request' do
expect(page).to_not have_link 'Create Merge Request'
end
step 'I should see button to the merge request' do
merge_request = MergeRequest.find_by(title: 'Feature')
expect(page).to have_link "View Open Merge Request", href: namespace_project_merge_request_path(@project.namespace, @project, merge_request)
end
step 'I see breadcrumb links' do step 'I see breadcrumb links' do
expect(page).to have_selector('ul.breadcrumb') expect(page).to have_selector('ul.breadcrumb')
expect(page).to have_selector('ul.breadcrumb a', count: 4) expect(page).to have_selector('ul.breadcrumb a', count: 4)
......
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