Class: BrightpearlCommand::ScriptsBranchCleaner

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

Instance Method Summary collapse

Instance Method Details

#cleanObject



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
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/routes/scripts_branch_cleaner.rb', line 34

def clean

    local_remote = @opts[:scanRemote] ? 'remote' : 'local'

    Brightpearl::Terminal::info('Running branch cleaner', "Gathering list of #{Brightpearl::Terminal::format_action(local_remote)} branches which are safe to delete.")

    branches_code, branches_db = get_historic_branches
    umc_branches = get_unsafe_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?
        Brightpearl::Terminal::warning("The following branches have commits which #{Brightpearl::Terminal::format_invalid('have not been merged to', true)} #{Brightpearl::Terminal::format_branch(Brightpearl::Git::MASTER)} \xe2\x80\x94 #{Brightpearl::Terminal::format_directory(@git.get_repo_shorthand(Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE)))}", umc_branches, false)
        atleast_one_error = true
    end

    if atleast_one_error
        unless Brightpearl::Terminal::prompt_yes_no('Skip these branches?', ["The above branch(es) have unmerged commits and will be #{Brightpearl::Terminal::format_action('skipped')} in the removal process."])
            Brightpearl::Terminal::abort(nil, nil, true, false)
        end
    end

    unless fnl_branches_cd.any? && fnl_branches_db.any?
        Brightpearl::Terminal::info('Nothing to delete', nil, false)
        exit
    end

    @git.show_branches_draw_table(fnl_branches_cd, fnl_branches_db)

    unless Brightpearl::Terminal::prompt_yes_no('Remove these branches?', ["By continuing, the above branches will be #{Brightpearl::Terminal::format_action('removed permanently')}"], 'Are you absolutely sure you want to continue?', false)
        Brightpearl::Terminal::abort(nil, nil, true, false)
    end

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

    branch_count = 0

    fnl_branches_cd.each do |branch|
        branch_name = branch[:"#{Brightpearl::Git::REFNAME}"]
        commands = []
        if @opts[:scanLocal]
            commands << "git branch -D #{branch_name}"
        else
            commands << "git branch --unset-upstream #{branch_name}"
            commands << "git push origin --delete  #{branch_name}"
        end
        Brightpearl::Terminal::command(commands, repo_cd)
        branch_count = branch_count + 1
    end

    fnl_branches_db.each do |branch|
        branch_name = branch[:"#{Brightpearl::Git::REFNAME}"]
        commands = []
        if @opts[:scanLocal]
            commands << "git branch -D #{branch_name}"
        else
            commands << "git branch --unset-upstream #{branch_name}"
            commands << "git push origin --delete  #{branch_name}"
        end
        Brightpearl::Terminal::command(commands, repo_db)
        branch_count = branch_count + 1
    end

    Brightpearl::Terminal::success("#{Brightpearl::Terminal::format_highlight(branch_count)} branches successfully #{Brightpearl::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 = Brightpearl::Git.new

    opts_validate
    opts_routing

end

#opts_routingObject



28
29
30
31
32
# File 'lib/routes/scripts_branch_cleaner.rb', line 28

def opts_routing

    clean

end

#opts_validateObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/routes/scripts_branch_cleaner.rb', line 16

def opts_validate

    if @opts[:scanLocal] && @opts[:scanRemote]
        Brightpearl::Terminal::error("Cannot scan #{Brightpearl::Terminal::format_action('local')} & #{Brightpearl::Terminal::format_action('remote')} simultaneously", "Must use either the #{Brightpearl::Terminal::format_flag('l')} or the #{Brightpearl::Terminal::format_flag('r')} \xe2\x80\x94 cannot use both.", true)
    end

    if @opts[:scanLocal] == false && @opts[:scanRemote] == false
        @opts[:scanLocal] = true
    end

end