#!/usr/bin/perl

# PODNAME: tanker_branch

=encoding UTF-8
=cut

=head1 SYNOPSIS

tanker_branch --project=project_id [ --list | --create=new_branch | --delete=branch_to_delete ]

 Obligatory options:

      --project=project_id  # project id in the tanker
      --token=12345         # project secret token

 One of those options should be specified:

      --list                # the list of all branches in project will be printed
      --create=<new_branch> # new branch with the name 'new_branch' will be created
                            # parameter --ref defines the branch 'new_branch' will be copied from
      --delete=<branch_to_delete>  # branch with the name 'branch_to_delete' will be deleted

 Optional options:

      --help                    # show this message and exit
      --use_test_tanker         # work with tanker-test.yandex-team.ru
      --ref=<sha>               # this parameter can be used only with --create
                                # it specifies the branch that the new branch will be equal to
                                # if it is not specified the new branch will be created as a copy from master

Script for branch management in Tanker project. Can list, create and delete branches.

=cut

# common modules
use strict;
use warnings FATAL => 'all';
use 5.008;
use Carp;
use Getopt::Long;
use Term::ANSIColor qw(:constants);
use Data::Dumper;
use Pod::Usage;

use Yandex::Tanker;

# subs
sub get_correct_opts {

    my $opts;

    GetOptions(
        'help' => \$opts->{help},
        'project=s' => \$opts->{project},
        'token=s' => \$opts->{token},
        'use_test_tanker' => \$opts->{use_test_tanker},
        'list' => \$opts->{list},
        'create=s' => \$opts->{create},
        'ref=s' => \$opts->{ref},
        'delete=s' => \$opts->{delete},
    );

    if ($opts->{help}) {
        pod2usage({-message => ''});
        exit;
    } elsif (not defined $opts->{project}) {
        Yandex::Tanker::_print_error_and_exit("'--project' is missing");
    } elsif (not $opts->{list} and not defined $opts->{create} and not defined $opts->{delete}) {
        Yandex::Tanker::_print_error_and_exit("One of those parameters should be specified: --list, --create, --delete");
    }

    return $opts;
}

# main
sub main {
    my $opts = get_correct_opts();

    my $tanker_url;
    if ($opts->{use_test_tanker}) {
        $tanker_url = $Yandex::Tanker::TEST_URL;
    } else {
        $tanker_url = $Yandex::Tanker::PROD_URL;
    }

    my $tanker = Yandex::Tanker->new({
        url     => $tanker_url,
        project => $opts->{project},
        token   => $opts->{token},
    });

    if (defined $opts->{list}) {
        foreach ($tanker->get_branches()) {
            print $_ . "\n";
        }
    } elsif (defined $opts->{create}) {
        $opts->{ref} = 'master' unless defined $opts->{ref};
        $tanker->create_branch(
            name => $opts->{create},
            ref => $opts->{ref},
        );
    } elsif (defined $opts->{delete}) {
        $tanker->delete_branch(branch => $opts->{delete});
    }
}

main();
__END__
