App-TimeTracker-Command-Gitlab
view release on metacpan or search on metacpan
lib/App/TimeTracker/Command/Gitlab.pm view on Meta::CPAN
unless ( $config->{url} && $config->{token} ) {
error_message(
"Please configure Gitlab in your TimeTracker config (needs url & token)"
);
return;
}
return HTTP::Tiny->new(default_headers=>{
'PRIVATE-TOKEN'=> $self->config->{gitlab}{token},
});
}
has 'project_id' => (
is=>'ro',
isa=>'Str',
documentation=>'The ID or namespace/name of this project',
lazy_build=>1,
);
sub _build_project_id {
my $self = shift;
return $self->config->{gitlab}{project_id} if $self->config->{gitlab}{project_id};
my $name = $self->config->{project};
my $namespace = $self->config->{gitlab}{namespace} || '' ;
if ($name && $namespace) {
return join('%2F',$namespace, $name);
}
error_message("Please set either project_id, or project and namespace");
return
}
before [ 'cmd_start', 'cmd_continue', 'cmd_append' ] => sub {
my $self = shift;
return unless $self->has_issue;
my $issuename = 'issue#' . $self->issue;
$self->insert_tag($issuename);
my $issue = $self->_call('GET','projects/'.$self->project_id.'/issues/'.$self->issue);
my $name = $issue->{title};
if ( defined $self->description ) {
$self->description( $self->description . ' ' . $name );
}
else {
$self->description($name);
}
if ( $self->meta->does_role('App::TimeTracker::Command::Git') ) {
my $branch = $self->issue;
if ($name) {
$branch = $self->safe_branch_name($self->issue.' '.$name);
}
$branch=~s/_/-/g;
$self->branch( lc($branch) ) unless $self->branch;
}
# reopen
if ($self->config->{gitlab}{reopen} && $issue->{state} eq 'closed') {
$self->_call('PUT','projects/'.$self->project_id.'/issues/'.$self->issue.'?state_event=reopen');
say "reopend closed issue";
}
# set assignee
if ($self->config->{gitlab}{set_assignee}) {
my $assignee;
if ($issue->{assignees} && $issue->{assignees}[0] && $issue->{assignees}[0]{username}) {
$assignee = $issue->{assignees}[0]{username};
}
elsif ( $issue->{assignee} && $issue->{assignee}{username}) {
$assignee = $issue->{assignee}{username};
}
if (my $user = $self->_call('GET','user')) {
if ($assignee) {
if ($assignee ne $user->{username}) {
warning_message("Assignee already set to ".$assignee);
}
}
else {
$self->_call('PUT','projects/'.$self->project_id.'/issues/'.$self->issue.'?assignee_id='.$user->{id});
say "Assignee set to you";
}
}
else {
error_message("Cannot get user-id, thus cannot assign issue");
}
}
# un/set labels
if (my $on_start = $self->config->{gitlab}{labels_on_start}) {
my %l = map {$_ => 1} @{$issue->{labels}};
if (my $add = $on_start->{add}) {
foreach my $new (@$add) {
$l{$new}=1;
}
}
if (my $remove = $on_start->{remove}) {
foreach my $remove (@$remove) {
delete $l{$remove};
}
}
$self->_call('PUT','projects/'.$self->project_id.'/issues/'.$self->issue.'?labels='.uri_escape(join(',',keys %l)));
say "Labels are now: ".join(', ',sort keys %l);
}
};
#after [ 'cmd_start', 'cmd_continue', 'cmd_append' ] => sub {
# my $self = shift;
# TODO: do we want to do something after stop?
#};
sub _get_user_id {
my $self = shift;
my $user = $self->_call('GET','user');
return $user->{id} if $user && $user->{id};
return;
}
sub _call {
my ($self,$method, $endpoint, $args) = @_;
my $url = $self->config->{gitlab}{url}.'/api/v4/'.$endpoint;
my $res = $self->gitlab_client->request($method,$url);
if ($res->{success}) {
my $data = decode_json($res->{content});
return $data;
}
error_message(join(" ",$res->{status}, $res->{reason}, "\n" . $res->{content}));
}
sub App::TimeTracker::Data::Task::gitlab_issue {
my $self = shift;
foreach my $tag ( @{ $self->tags } ) {
next unless $tag =~ /^issue#(\d+)/;
return $1;
}
}
no Moose::Role;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
App::TimeTracker::Command::Gitlab - App::TimeTracker Gitlab plugin
=head1 VERSION
version 1.004
=head1 DESCRIPTION
Connect tracker with L<Gitlab|https://about.gitlab.com/>.
Using the Gitlab plugin, tracker can fetch the name of an issue and use
it as the task's description; generate a nicely named C<git> branch
(if you're also using the C<Git> plugin).
( run in 0.510 second using v1.01-cache-2.11-cpan-524268b4103 )