From f8114954292e3551b61406438d41cd05a6f9ae31 Mon Sep 17 00:00:00 2001
From: Jacob Carlborg <jacob.carlborg@tretti.se>
Date: Wed, 27 Nov 2013 09:17:29 +0100
Subject: [PATCH] Add button for removing source branch for merged MR's.

---
 .../javascripts/merge_requests.js.coffee      | 12 +++++++++++
 .../stylesheets/sections/merge_requests.scss  |  2 +-
 .../projects/merge_requests_controller.rb     | 20 ++++++++++++++-----
 .../projects/merge_requests/_show.html.haml   |  1 +
 .../show/_remove_source_branch.html.haml      | 18 +++++++++++++++++
 5 files changed, 47 insertions(+), 6 deletions(-)
 create mode 100644 app/views/projects/merge_requests/show/_remove_source_branch.html.haml

diff --git a/app/assets/javascripts/merge_requests.js.coffee b/app/assets/javascripts/merge_requests.js.coffee
index 2eef7df1c6..6c930c91ad 100644
--- a/app/assets/javascripts/merge_requests.js.coffee
+++ b/app/assets/javascripts/merge_requests.js.coffee
@@ -57,6 +57,18 @@ class MergeRequest
       $('.automerge_widget.can_be_merged').hide()
       $('.merge-in-progress').show()
 
+    this.$('.remove_source_branch').on 'click', ->
+      $('.remove_source_branch_widget').hide()
+      $('.remove_source_branch_in_progress').show()
+
+    this.$(".remove_source_branch").on "ajax:success", (e, data, status, xhr) ->
+      location.reload()
+
+    this.$(".remove_source_branch").on "ajax:error", (e, data, status, xhr) =>
+      this.$('.remove_source_branch_widget').hide()
+      this.$('.remove_source_branch_in_progress').hide()
+      this.$('.remove_source_branch_widget.failed').show()
+
   activateTab: (action) ->
     this.$('.nav-tabs li').removeClass 'active'
     this.$('.tab-content').hide()
diff --git a/app/assets/stylesheets/sections/merge_requests.scss b/app/assets/stylesheets/sections/merge_requests.scss
index 0f0f8c858d..dc46b20670 100644
--- a/app/assets/stylesheets/sections/merge_requests.scss
+++ b/app/assets/stylesheets/sections/merge_requests.scss
@@ -53,7 +53,7 @@
   }
 }
 
-.merge-in-progress {
+.merge-in-progress, .remove_source_branch_in_progress {
   @extend .padded;
   @extend .append-bottom-10;
 }
diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb
index 6d39673194..7d71f7678f 100644
--- a/app/controllers/projects/merge_requests_controller.rb
+++ b/app/controllers/projects/merge_requests_controller.rb
@@ -199,6 +199,8 @@ class Projects::MergeRequestsController < Projects::ApplicationController
     @commits = @merge_request.commits
 
     @allowed_to_merge = allowed_to_merge?
+    @allowed_to_remove_source_branch = allowed_to_remove_source_branch?
+    @source_branch = @merge_request.source_project.repository.find_branch(@merge_request.source_branch).try(:name)
     @show_merge_controls = @merge_request.opened? && @commits.any? && @allowed_to_merge
 
     @target_type = :merge_request
@@ -206,6 +208,19 @@ class Projects::MergeRequestsController < Projects::ApplicationController
   end
 
   def allowed_to_merge?
+    allowed_to_push_code?(project)
+  end
+
+  def invalid_mr
+    # Render special view for MR with removed source or target branch
+    render 'invalid'
+  end
+
+  def allowed_to_remove_source_branch?
+    allowed_to_push_code?(@merge_request.source_project)
+  end
+
+  def allowed_to_push_code? (project)
     action = if project.protected_branch?(@merge_request.target_branch)
                :push_code_to_protected_branches
              else
@@ -214,9 +229,4 @@ class Projects::MergeRequestsController < Projects::ApplicationController
 
     can?(current_user, action, @project)
   end
-
-  def invalid_mr
-    # Render special view for MR with removed source or target branch
-    render 'invalid'
-  end
 end
diff --git a/app/views/projects/merge_requests/_show.html.haml b/app/views/projects/merge_requests/_show.html.haml
index 0b9e4df3fd..c612838d18 100644
--- a/app/views/projects/merge_requests/_show.html.haml
+++ b/app/views/projects/merge_requests/_show.html.haml
@@ -7,6 +7,7 @@
       = render "projects/merge_requests/show/mr_accept"
     - else
       = render "projects/merge_requests/show/no_accept"
+  = render "projects/merge_requests/show/remove_source_branch"
   - if @merge_request.source_project.gitlab_ci?
     = render "projects/merge_requests/show/mr_ci"
   = render "projects/merge_requests/show/commits"
diff --git a/app/views/projects/merge_requests/show/_remove_source_branch.html.haml b/app/views/projects/merge_requests/show/_remove_source_branch.html.haml
new file mode 100644
index 0000000000..161cc93593
--- /dev/null
+++ b/app/views/projects/merge_requests/show/_remove_source_branch.html.haml
@@ -0,0 +1,18 @@
+- if @source_branch.blank?
+  .alert.alert-info Source branch has been removed
+
+- elsif @allowed_to_remove_source_branch && @merge_request.merged?
+  .remove_source_branch_widget
+    .alert.alert-info
+      = link_to project_branch_path(@merge_request.source_project, @source_branch), remote: true, method: :delete, class: "btn remove_source_branch" do
+        %i.icon-remove
+        Remove Source Branch
+
+  .remove_source_branch_widget.failed.hide
+    .alert.alert-error Failed to remove source branch '#{@merge_request.source_branch}'
+
+  .remove_source_branch_in_progress.hide
+    %span.cgray
+      %i.icon-refresh.icon-spin
+      &nbsp;
+      Removing source branch '#{@merge_request.source_branch}'. Please wait. Page will be automatically reloaded. &nbsp;
\ No newline at end of file
-- 
2.30.9