Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add button to delete all merged branches
It adds a button to the branches page that the user can use to delete all the branches that are already merged. This can be used to clean up all the branches that were forgotten to delete while merging MRs. Fixes #21076.
- Loading branch information
Showing
14 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require_relative 'base_service' | ||
|
||
class DeleteMergedBranchesService < BaseService | ||
def async_execute | ||
DeleteMergedBranchesWorker.perform_async(project.id, current_user.id) | ||
end | ||
|
||
def execute | ||
raise Gitlab::Access::AccessDeniedError unless can?(current_user, :push_code, project) | ||
|
||
branches = project.repository.branch_names | ||
branches = branches.select { |branch| project.repository.merged_to_root_ref?(branch) } | ||
|
||
branches.each do |branch| | ||
DeleteBranchService.new(project, current_user).execute(branch) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class DeleteMergedBranchesWorker | ||
include Sidekiq::Worker | ||
include DedicatedSidekiqQueue | ||
|
||
def perform(project_id, user_id) | ||
begin | ||
project = Project.find(project_id) | ||
rescue ActiveRecord::RecordNotFound | ||
return | ||
end | ||
|
||
user = User.find(user_id) | ||
|
||
begin | ||
DeleteMergedBranchesService.new(project, user).execute | ||
rescue Gitlab::Access::AccessDeniedError | ||
return | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: Add button to delete all merged branches | ||
merge_request: 6449 | ||
author: Toon Claes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'spec_helper' | ||
|
||
describe DeleteMergedBranchesService, services: true do | ||
subject(:service) { described_class.new(project, project.owner) } | ||
|
||
let(:project) { create(:project) } | ||
|
||
context '#execute' do | ||
context 'unprotected branches' do | ||
before do | ||
service.execute | ||
end | ||
|
||
it 'deletes a branch that was merged' do | ||
expect(project.repository.branch_names).not_to include('improve/awesome') | ||
end | ||
|
||
it 'keeps branch that is unmerged' do | ||
expect(project.repository.branch_names).to include('feature') | ||
end | ||
|
||
it 'keeps "master"' do | ||
expect(project.repository.branch_names).to include('master') | ||
end | ||
end | ||
|
||
context 'protected branches' do | ||
before do | ||
create(:protected_branch, name: 'improve/awesome', project: project) | ||
service.execute | ||
end | ||
|
||
it 'keeps protected branch' do | ||
expect(project.repository.branch_names).to include('improve/awesome') | ||
end | ||
end | ||
|
||
context 'user without rights' do | ||
let(:user) { create(:user) } | ||
|
||
it 'cannot execute' do | ||
expect { described_class.new(project, user).execute }.to raise_error(Gitlab::Access::AccessDeniedError) | ||
end | ||
end | ||
end | ||
|
||
context '#async_execute' do | ||
it 'calls DeleteMergedBranchesWorker async' do | ||
expect(DeleteMergedBranchesWorker).to receive(:perform_async) | ||
|
||
service.async_execute | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'spec_helper' | ||
|
||
describe DeleteMergedBranchesWorker do | ||
subject(:worker) { described_class.new } | ||
|
||
let(:project) { create(:project) } | ||
|
||
describe "#perform" do | ||
it "calls DeleteMergedBranchesService" do | ||
expect_any_instance_of(DeleteMergedBranchesService).to receive(:execute).and_return(true) | ||
|
||
worker.perform(project.id, project.owner.id) | ||
end | ||
|
||
it "returns false when project was not found" do | ||
expect(worker.perform('unknown', project.owner.id)).to be_falsy | ||
end | ||
end | ||
end |