Class: AppCommand::ScriptsBranchCleaner

Inherits:
Convoy::ActionCommand::Base
  • Object
show all
Defined in:
lib/routes/scripts_branch_cleaner.rb

Instance Method Summary collapse

Instance Method Details

#cleanObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/routes/scripts_branch_cleaner.rb', line 26

def clean

    App::Terminal::info('Running branch cleaner', "Gathering list of #{App::Terminal::format_highlight('local')} branches where last commit was #{App::Terminal::format_highlight('more than 1 month ago')}")

    sleep(1)

    branches_code, branches_db = get_historic_branches
    umc_branches = get_unmerged_commit_branches(branches_code, branches_db)
    fnl_branches_cd, fnl_branches_db = get_final_branches(branches_code, branches_db, umc_branches)

    puts "\n"

    atleast_one_error = false

    if umc_branches.any?
        App::Terminal::warning("The following branches have commits which have not been merged to #{App::Terminal::format_branch(App::Git::MASTER)}", umc_branches, false)
        atleast_one_error = true
    end

    unless fnl_branches_cd.any? || fnl_branches_db.any?
        App::Terminal::info('No branches to delete', ['The script found no branches which are eligible for deletion.', "All branches on your system have either a #{App::Terminal::format_highlight('commit within the last month')} or a #{App::Terminal::format_highlight('commit not in')} #{App::Terminal::format_branch(App::Git::MASTER)}."])
        exit
    end

    if atleast_one_error
        unless App::Terminal::prompt_yes_no('Continue by skipping these branches?', ["The above branch(es) have unmerged commits and will be #{App::Terminal::format_action('skipped')} in the removal process.", "You #{App::Terminal::format_invalid('cannot delete', true)} these branches using this script, this must be done manually #{App::Terminal::format_highlight('afterwards')}."])
            App::Terminal::abort
        end
    end

    @git.show_branches_draw_table(fnl_branches_cd, fnl_branches_db)

    unless App::Terminal::prompt_yes_no('Remove these branches?', ["By continuing, the above branches will be #{App::Terminal::format_action('removed permanently')} both #{App::Terminal::format_highlight('locally & remotely')} (except release branches)."], 'Are you absolutely sure you want to continue?', false)
        App::Terminal::abort
    end

    repo_cd = App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)
    repo_db = App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)

    branch_count = 0

    fnl_branches_cd.each do |branch|
        branch_name = branch[:"#{App::Git::REFNAME}"]
        unless branch_name == App::Git::MASTER
            commands = []
            commands << "git branch --unset-upstream #{branch_name}"
            commands << "git branch -D #{branch_name}"
            commands << "git push origin --delete #{branch_name}" unless release_branch(branch_name)
            App::Terminal::command(commands, repo_cd)
            branch_count = branch_count + 1
        end
    end

    fnl_branches_db.each do |branch|
        branch_name = branch[:"#{App::Git::REFNAME}"]
        unless branch_name == App::Git::MASTER
            commands = []
            commands << "git branch --unset-upstream #{branch_name}"
            commands << "git branch -D #{branch_name}"
            commands << "git push origin --delete #{branch_name}" unless release_branch(branch_name)
            App::Terminal::command(commands, repo_db)
            branch_count = branch_count + 1
        end
    end

    App::Terminal::success("#{App::Terminal::format_highlight(branch_count)} branches successfully #{App::Terminal::format_action('removed')}")
end

#executeObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/routes/scripts_branch_cleaner.rb', line 5

def execute

    @opts = command_options
    @args = arguments
    @git = App::Git.new

    opts_validate
    opts_routing

end

#opts_routingObject



20
21
22
23
24
# File 'lib/routes/scripts_branch_cleaner.rb', line 20

def opts_routing

    clean

end

#opts_validateObject



16
17
18
# File 'lib/routes/scripts_branch_cleaner.rb', line 16

def opts_validate

end