From 38e708f0cea2f6707a26854b9d077182c063dd51 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rub=C3=A9n=20D=C3=A1vila?= <rdavila84@gmail.com>
Date: Mon, 8 Feb 2016 17:06:04 -0500
Subject: [PATCH] Add link to fork if user can't revert MR.

---
 app/controllers/concerns/creates_commit.rb          |  2 +-
 app/helpers/merge_requests_helper.rb                |  7 +++++++
 app/models/repository.rb                            |  2 ++
 app/views/projects/merge_requests/_show.html.haml   |  2 ++
 .../merge_requests/show/_mr_title.html.haml         | 13 +++++++++++--
 5 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/app/controllers/concerns/creates_commit.rb b/app/controllers/concerns/creates_commit.rb
index f8d6621999d..1818947eb70 100644
--- a/app/controllers/concerns/creates_commit.rb
+++ b/app/controllers/concerns/creates_commit.rb
@@ -108,7 +108,7 @@ module CreatesCommit
       # Merge request from fork to this project
       @mr_source_project = @tree_edit_project
       @mr_target_project = @project
-      @mr_target_branch = @ref
+      @mr_target_branch  ||= @ref
     end
   end
 end
diff --git a/app/helpers/merge_requests_helper.rb b/app/helpers/merge_requests_helper.rb
index 1dd07a2a220..b4858b999d5 100644
--- a/app/helpers/merge_requests_helper.rb
+++ b/app/helpers/merge_requests_helper.rb
@@ -92,4 +92,11 @@ module MergeRequestsHelper
       ["#{source_path}:#{source_branch}", "#{target_path}:#{target_branch}"]
     end
   end
+
+  def can_update_merge_request?
+    project ||= @project
+
+    can?(current_user, :update_merge_request, project) ||
+      (current_user && current_user.already_forked?(project))
+  end
 end
diff --git a/app/models/repository.rb b/app/models/repository.rb
index a77e3012e70..27e3da24559 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -628,6 +628,8 @@ class Repository
     args          = [commit.id, source_sha]
     args          << { mainline: 1 } if commit.is_a_merge_commit?
 
+    # Temporary branch exists and contains the revert commit
+    return true if create_mr && find_branch(target_branch)
     return false unless diff_exists?(source_sha, commit.id)
 
     revert_index = rugged.revert_commit(*args)
diff --git a/app/views/projects/merge_requests/_show.html.haml b/app/views/projects/merge_requests/_show.html.haml
index da67645bc2b..24a33954821 100644
--- a/app/views/projects/merge_requests/_show.html.haml
+++ b/app/views/projects/merge_requests/_show.html.haml
@@ -85,6 +85,8 @@
         = spinner
 
 = render 'shared/issuable/sidebar', issuable: @merge_request
+- if @merge_request.merge_commit_sha
+  = render "projects/commit/revert", commit_id: @merge_request.merge_commit_sha
 
 :javascript
   var merge_request;
diff --git a/app/views/projects/merge_requests/show/_mr_title.html.haml b/app/views/projects/merge_requests/show/_mr_title.html.haml
index 87cd50abdd9..a6aac25ca8c 100644
--- a/app/views/projects/merge_requests/show/_mr_title.html.haml
+++ b/app/views/projects/merge_requests/show/_mr_title.html.haml
@@ -18,7 +18,16 @@
           Edit
       - if @merge_request.closed?
         = link_to 'Reopen', merge_request_path(@merge_request, merge_request: {state_event: :reopen }), method: :put, class: 'btn btn-nr btn-grouped btn-reopen reopen-mr-link', title: "Reopen merge request"
-      - if @merge_request.merged? && @merge_request.merge_commit_sha.present?
-        = render "projects/commit/revert", commit_id: @merge_request.merge_commit_sha
+
+    - if @merge_request.merged? && @merge_request.merge_commit_sha.present?
+      - if can_update_merge_request?
         = link_to '#modal-revert-commit', { 'data-target' => '#modal-revert-commit', 'data-toggle' => 'modal', class: 'btn btn-grouped btn-close'} do
           Revert
+      - else
+        - continue_params = { to:         namespace_project_merge_request_path(@project.namespace, @project, @merge_request),
+                              notice:     edit_in_new_fork_notice,
+                              notice_now: edit_in_new_fork_notice_now }
+        - fork_path = namespace_project_forks_path(@project.namespace, @project, namespace_key:  current_user.namespace.id,
+                                                                                 continue:       continue_params)
+        = link_to fork_path, { class: 'btn btn-grouped btn-close', method: :post } do
+          Revert
-- 
GitLab