summaryrefslogtreecommitdiffstats
path: root/lib/Devscripts/Salsa/rename_branch.pm
blob: 835b98349949d94679abf34a328f1e111942b3e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package Devscripts::Salsa::rename_branch;

use strict;
use Devscripts::Output;
use Moo::Role;

with "Devscripts::Salsa::Repo";

our $prompt = 1;

sub rename_branch {
    my ($self, @reponames) = @_;
    my $res = 0;
    my @repos = $self->get_repo($prompt, @reponames);
    return @repos unless (ref $repos[0]);    # get_repo returns 1 when fails
    foreach (@repos) {
        my $id = $_->[0];
        ds_verbose "Configuring $_->[1]";
        my $project = $self->api->project($_->[0]);
        eval {
            $self->api->create_branch(
                $id,
                {
                    ref    => $self->config->source_branch,
                    branch => $self->config->dest_branch,
                });
            $self->api->delete_branch($id, $self->config->source_branch);
        };
        if ($@) {
            $res++;
            if ($self->config->no_fail) {
                ds_verbose $@;
                ds_warn
"Branch rename has failed for $_->[1]. Use --verbose to see errors\n";
                next;
            } else {
                ds_warn $@;
                return 1;
            }
        }
    }
    return $res;
}

1;