From 36b065e634f65e5c4690ccd7eed0cc90986ec67b Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Fri, 28 Feb 2014 22:43:16 +0200
Subject: [PATCH] Feature: reopen closed merge request

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
---
 app/assets/stylesheets/sections/merge_requests.scss        | 5 +++--
 app/controllers/projects/merge_requests_controller.rb      | 4 ++--
 app/models/concerns/issuable.rb                            | 3 ++-
 app/models/issue.rb                                        | 4 ----
 app/models/merge_request.rb                                | 6 ++++--
 app/observers/merge_request_observer.rb                    | 2 ++
 app/views/projects/merge_requests/_show.html.haml          | 5 +++--
 app/views/projects/merge_requests/show/_mr_box.html.haml   | 2 +-
 app/views/projects/merge_requests/show/_mr_title.html.haml | 4 +++-
 9 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/app/assets/stylesheets/sections/merge_requests.scss b/app/assets/stylesheets/sections/merge_requests.scss
index 655dc9bf489..5341f411e54 100644
--- a/app/assets/stylesheets/sections/merge_requests.scss
+++ b/app/assets/stylesheets/sections/merge_requests.scss
@@ -91,6 +91,7 @@
 }
 
 // hide mr close link for inline diff comment form
-.diff-file .close-mr-link {
- display: none;
+.diff-file .close-mr-link,
+.diff-file .reopen-mr-link {
+  display: none;
 }
diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb
index 51509ecda35..38e567f3b13 100644
--- a/app/controllers/projects/merge_requests_controller.rb
+++ b/app/controllers/projects/merge_requests_controller.rb
@@ -135,7 +135,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
   def automerge
     return access_denied! unless allowed_to_merge?
 
-    if @merge_request.opened? && @merge_request.can_be_merged?
+    if @merge_request.open? && @merge_request.can_be_merged?
       @merge_request.should_remove_source_branch = params[:should_remove_source_branch]
       @merge_request.automerge!(current_user, params[:merge_commit_message])
       @status = true
@@ -230,7 +230,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
 
     @merge_request_diff = @merge_request.merge_request_diff
     @allowed_to_merge = allowed_to_merge?
-    @show_merge_controls = @merge_request.opened? && @commits.any? && @allowed_to_merge
+    @show_merge_controls = @merge_request.open? && @commits.any? && @allowed_to_merge
   end
 
   def allowed_to_merge?
diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb
index bf2c2157d38..75989888bfa 100644
--- a/app/models/concerns/issuable.rb
+++ b/app/models/concerns/issuable.rb
@@ -23,7 +23,8 @@ module Issuable
     scope :assigned, -> { where("assignee_id IS NOT NULL") }
     scope :unassigned, -> { where("assignee_id IS NULL") }
     scope :of_projects, ->(ids) { where(project_id: ids) }
-
+    scope :opened, -> { with_state(:opened, :reopened) }
+    scope :closed, -> { with_state(:closed) }
 
     delegate :name,
              :email,
diff --git a/app/models/issue.rb b/app/models/issue.rb
index 6580c5004af..a8dc6e5fd85 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -28,8 +28,6 @@ class Issue < ActiveRecord::Base
 
   scope :of_group, ->(group) { where(project_id: group.project_ids) }
   scope :of_user_team, ->(team) { where(project_id: team.project_ids, assignee_id: team.member_ids) }
-  scope :opened, -> { with_state(:opened, :reopened) }
-  scope :closed, -> { with_state(:closed) }
 
   attr_accessible :title, :assignee_id, :position, :description,
                   :milestone_id, :label_list, :author_id_of_changes,
@@ -50,9 +48,7 @@ class Issue < ActiveRecord::Base
     end
 
     state :opened
-
     state :reopened
-
     state :closed
   end
 
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index b596d7eb9cf..4774cbcf3aa 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -100,8 +100,6 @@ class MergeRequest < ActiveRecord::Base
 
   scope :of_group, ->(group) { where("source_project_id in (:group_project_ids) OR target_project_id in (:group_project_ids)", group_project_ids: group.project_ids) }
   scope :of_user_team, ->(team) { where("(source_project_id in (:team_project_ids) OR target_project_id in (:team_project_ids) AND assignee_id in (:team_member_ids))", team_project_ids: team.project_ids, team_member_ids: team.member_ids) }
-  scope :opened, -> { with_state(:opened) }
-  scope :closed, -> { with_state(:closed) }
   scope :merged, -> { with_state(:merged) }
   scope :by_branch, ->(branch_name) { where("(source_branch LIKE :branch) OR (target_branch LIKE :branch)", branch: branch_name) }
   scope :cared, ->(user) { where('assignee_id = :user OR author_id = :user', user: user.id) }
@@ -160,6 +158,10 @@ class MergeRequest < ActiveRecord::Base
     MergeRequests::AutoMergeService.new.execute(self, current_user, commit_message)
   end
 
+  def open?
+    opened? || reopened?
+  end
+
   def mr_and_commit_notes
     # Fetch comments only from last 100 commits
     commits_for_notes_limit = 100
diff --git a/app/observers/merge_request_observer.rb b/app/observers/merge_request_observer.rb
index ef31498e7d0..f2e2d16c943 100644
--- a/app/observers/merge_request_observer.rb
+++ b/app/observers/merge_request_observer.rb
@@ -22,6 +22,8 @@ class MergeRequestObserver < ActivityObserver
     create_event(merge_request, Event::REOPENED)
     create_note(merge_request)
     execute_hooks(merge_request)
+    merge_request.reload_code
+    merge_request.mark_as_unchecked
   end
 
   def after_update(merge_request)
diff --git a/app/views/projects/merge_requests/_show.html.haml b/app/views/projects/merge_requests/_show.html.haml
index 001e0cf4430..809b01918cf 100644
--- a/app/views/projects/merge_requests/_show.html.haml
+++ b/app/views/projects/merge_requests/_show.html.haml
@@ -2,7 +2,7 @@
   = render "projects/merge_requests/show/mr_title"
   = render "projects/merge_requests/show/how_to_merge"
   = render "projects/merge_requests/show/mr_box"
-  - if @merge_request.opened?
+  - if @merge_request.open?
     - if @merge_request.source_branch_exists? && @merge_request.target_branch_exists?
       = render "projects/merge_requests/show/mr_accept"
     - else
@@ -26,7 +26,8 @@
     - if can?(current_user, :modify_merge_request, @merge_request)
       - unless @merge_request.closed? || @merge_request.merged?
         = link_to 'Close', project_merge_request_path(@project, @merge_request, merge_request: {state_event: :close }), method: :put, class: "btn btn-grouped btn-close close-mr-link", title: "Close merge request"
-
+      - if @merge_request.closed?
+        = link_to 'Reopen', project_merge_request_path(@project, @merge_request, merge_request: {state_event: :reopen }), method: :put, class: "btn btn-grouped btn-reopen reopen-mr-link", title: "Close merge request"
 
   .notes.tab-content.voting_notes#notes{ class: (controller.action_name == 'show') ? "" : "hide" }
     = render "projects/notes/notes_with_form"
diff --git a/app/views/projects/merge_requests/show/_mr_box.html.haml b/app/views/projects/merge_requests/show/_mr_box.html.haml
index 3d0ce57c4ac..38db4363ade 100644
--- a/app/views/projects/merge_requests/show/_mr_box.html.haml
+++ b/app/views/projects/merge_requests/show/_mr_box.html.haml
@@ -36,7 +36,7 @@
       %i.icon-ok
       Merged by #{link_to_member(@project, @merge_request.merge_event.author)}
       #{time_ago_with_tooltip(@merge_request.merge_event.created_at)}.
-- if !@closes_issues.empty? && @merge_request.opened?
+- if !@closes_issues.empty? && @merge_request.open?
   .alert.alert-info.alert-info
     %span
       %i.icon-ok
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 58368ab18b6..7676fc137c7 100644
--- a/app/views/projects/merge_requests/show/_mr_title.html.haml
+++ b/app/views/projects/merge_requests/show/_mr_title.html.haml
@@ -3,7 +3,7 @@
 
   %span.pull-right
     - if can?(current_user, :modify_merge_request, @merge_request)
-      - if @merge_request.opened?
+      - if @merge_request.open?
         .btn-group.pull-left
           %a.btn.btn-grouped.dropdown-toggle{ data: {toggle: :dropdown} }
             %i.icon-download-alt
@@ -18,6 +18,8 @@
         = link_to edit_project_merge_request_path(@project, @merge_request), class: "btn btn-grouped", id:"edit_merge_request" do
           %i.icon-edit
           Edit
+      - if @merge_request.closed?
+        = link_to 'Reopen', project_merge_request_path(@project, @merge_request, merge_request: {state_event: :reopen }), method: :put, class: "btn btn-grouped btn-reopen reopen-mr-link", title: "Close merge request"
 
 .votes-holder.hidden-sm.hidden-xs
   #votes= render 'votes/votes_block', votable: @merge_request
-- 
GitLab