lib/App/TimeTracker/Command/Gitlab.pm

filter: Statementstmt 30.7% 32 / 104 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 60.0% 9 / 15 Podpod 0.0% 0 / 1 totaltotal 20.6% 41 / 199 CC 33 SCAR 54.9
CC
33
Cov
19%
__ANON__
506.0
_build_project_id
30.0
_build_gitlab_client
12.0
CRAP 242.8
↑ Summary
1 package App::TimeTracker::Command::Gitlab;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: App::TimeTracker Gitlab plugin
7 1 ▶use App::TimeTracker::Utils qw(error_message warning_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = "1.004";
10  
11 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON::XS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 has 'issue' => (
17 is => 'rw',
18 isa => 'Str',
19 documentation => 'gitlab issue',
20 predicate => 'has_issue'
21 );
22  
23 1 has 'gitlab_client' => (
24 is => 'rw',
25 isa => 'Maybe[HTTP::Tiny]',
26 lazy_build => 1,
27 traits => ['NoGetopt'],
28 );
29  
30 0 ▶sub _build_gitlab_client {
Subroutine _build_gitlab_client: not called CC=3 SCAR=24.8
31 0 my $self = shift;
32 0 my $config = $self->config->{gitlab};
33  
34 0 ▶ unless ( $config->{url} && $config->{token} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"url"} and $config->{"token"}) 0 0 0
Condition0%0/3
$config->{"url"} and $config->{"token"}
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"url"} and $config->{"token"}
$config->{"url"} $config->{"token"}
Truth table0%0/3
A and B
A= $config->{"url"}B= $config->{"token"}
ABresult
0X0
100
111
35 0 error_message(
36 "Please configure Gitlab in your TimeTracker config (needs url & token)"
37 );
38 0 return;
39 }
40  
41 return HTTP::Tiny->new(default_headers=>{
42 'PRIVATE-TOKEN'=> $self->config->{gitlab}{token},
43 0 });
44 }
45  
46 1 has 'project_id' => (
47 is=>'ro',
48 isa=>'Str',
49 documentation=>'The ID or namespace/name of this project',
50 lazy_build=>1,
51 );
52  
53 0 ▶sub _build_project_id {
Subroutine _build_project_id: not called CC=5 SCAR=34.0
54 0 my $self = shift;
55 0 ▶ return $self->config->{gitlab}{project_id} if $self->config->{gitlab}{project_id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"gitlab"}{"project_id"} 0 0 0
56 0 my $name = $self->config->{project};
57 0 ▶ my $namespace = $self->config->{gitlab}{namespace} || '' ;
Condition0%0/2
$self->config->{'gitlab'}{'namespace'} || ''
l!l
00
MC/DC0%0/1
$self->config->{'gitlab'}{'namespace'} || ''
$self->config->{'gitlab'}{'namespace'}
Truth table0%0/2
A
A= $self->config->{'gitlab'}{'namespace'}
Aresult
11
00
58 0 ▶ if ($name && $namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and $namespace) 0 0 0
Condition0%0/3
$name and $namespace
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $namespace
$name $namespace
Truth table0%0/3
A and B
A= $nameB= $namespace
ABresult
0X0
100
111
59 0 return join('%2F',$namespace, $name);
60 }
61 0 error_message("Please set either project_id, or project and namespace");
62 return
63 0 }
64  
65 0 ▶before [ 'cmd_start', 'cmd_continue', 'cmd_append' ] => sub {
Subroutine __ANON__: not called CC=22 SCAR=62.3
66 0 my $self = shift;
67 0 ▶ return unless $self->has_issue;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_issue 0 0 0
68  
69 0 my $issuename = 'issue#' . $self->issue;
70 0 $self->insert_tag($issuename);
71  
72 0 my $issue = $self->_call('GET','projects/'.$self->project_id.'/issues/'.$self->issue);
73  
74 0 my $name = $issue->{title};
75  
76 0 ▶ if ( defined $self->description ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->description) { } 0 0 0
77 0 $self->description( $self->description . ' ' . $name );
78 }
79 else {
80 0 $self->description($name);
81 }
82  
83 0 ▶ if ( $self->meta->does_role('App::TimeTracker::Command::Git') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->meta->does_role("App::TimeTracker::Command::Git")) 0 0 0
84 0 my $branch = $self->issue;
85 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
86 0 $branch = $self->safe_branch_name($self->issue.' '.$name);
87 }
88 0 $branch=~s/_/-/g;
89 0 ▶ $self->branch( lc($branch) ) unless $self->branch;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->branch 0 0 0
90 }
91  
92 # reopen
93 0 ▶ if ($self->config->{gitlab}{reopen} && $issue->{state} eq 'closed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"gitlab"}{"reopen"} and $issue->{"state"} eq "closed") 0 0 0
Condition0%0/3
$self->config->{"gitlab"}{"reopen"} and $issue->{"state"} eq "closed"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->config->{"gitlab"}{"reopen"} and $issue->{"state"} eq "closed"
$self->config->{"gitlab"}{"reopen"} $issue->{"state"} eq "closed"
Truth table0%0/3
A and B
A= $self->config->{"gitlab"}{"reopen"}B= $issue->{"state"} eq "closed"
ABresult
0X0
100
111
94 0 $self->_call('PUT','projects/'.$self->project_id.'/issues/'.$self->issue.'?state_event=reopen');
95 0 say "reopend closed issue";
96 }
97  
98 # set assignee
99 0 ▶ if ($self->config->{gitlab}{set_assignee}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"gitlab"}{"set_assignee"}) 0 0 0
100 0 my $assignee;
101 0 ▶ if ($issue->{assignees} && $issue->{assignees}[0] && $issue->{assignees}[0]{username}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($issue->{'assignees'} and $issue->{'assignees'}[0] and $issue->{'assignees'}[0]{'username'}) { } 0 0 0
elsif ($issue->{'assignee'} and $issue->{'assignee'}{'username'}) { } 0 0 0
Condition0%0/6
$issue->{"assignees"} and $issue->{"assignees"}[0] and $issue->{"assignees"}[0]{"username"}
!ll&&!rl&&r
000
$issue->{"assignees"} and $issue->{"assignees"}[0]
!ll&&!rl&&r
000
MC/DC0%0/3
$issue->{"assignees"} and $issue->{"assignees"}[0] and $issue->{"assignees"}[0]{"username"}
$issue->{"assignees"} $issue->{"assignees"}[0] $issue->{"assignees"}[0]{"username"}
Truth table0%0/4
A and B and C
A= $issue->{"assignees"}B= $issue->{"assignees"}[0]C= $issue->{"assignees"}[0]{"username"}
ABCresult
0XX0
10X0
1100
1111
102 0 $assignee = $issue->{assignees}[0]{username};
103 }
104 ▶ elsif ( $issue->{assignee} && $issue->{assignee}{username}) {
Condition0%0/3
$issue->{"assignee"} and $issue->{"assignee"}{"username"}
!ll&&!rl&&r
000
MC/DC0%0/2
$issue->{"assignee"} and $issue->{"assignee"}{"username"}
$issue->{"assignee"} $issue->{"assignee"}{"username"}
Truth table0%0/3
A and B
A= $issue->{"assignee"}B= $issue->{"assignee"}{"username"}
ABresult
0X0
100
111
105 0 $assignee = $issue->{assignee}{username};
106 }
107  
108 0 ▶ if (my $user = $self->_call('GET','user')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $user = $self->_call('GET', 'user')) { } 0 0 0
109 0 ▶ if ($assignee) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assignee) { } 0 0 0
110 0 ▶ if ($assignee ne $user->{username}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assignee ne $user->{"username"}) 0 0 0
111 0 warning_message("Assignee already set to ".$assignee);
112 }
113 }
114 else {
115 0 $self->_call('PUT','projects/'.$self->project_id.'/issues/'.$self->issue.'?assignee_id='.$user->{id});
116 0 say "Assignee set to you";
117 }
118 }
119 else {
120 0 error_message("Cannot get user-id, thus cannot assign issue");
121 }
122 }
123  
124 # un/set labels
125 0 ▶ if (my $on_start = $self->config->{gitlab}{labels_on_start}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_start = $self->config->{"gitlab"}{"labels_on_start"}) 0 0 0
126 0 my %l = map {$_ => 1} @{$issue->{labels}};
127 0 ▶ if (my $add = $on_start->{add}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $add = $on_start->{"add"}) 0 0 0
128 0 foreach my $new (@$add) {
129 0 $l{$new}=1;
130 }
131 }
132 0 ▶ if (my $remove = $on_start->{remove}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $remove = $on_start->{"remove"}) 0 0 0
133 0 foreach my $remove (@$remove) {
134 0 delete $l{$remove};
135 }
136 }
137 0 $self->_call('PUT','projects/'.$self->project_id.'/issues/'.$self->issue.'?labels='.uri_escape(join(',',keys %l)));
138 0 say "Labels are now: ".join(', ',sort keys %l);
139 }
140 1 };
141  
142 #after [ 'cmd_start', 'cmd_continue', 'cmd_append' ] => sub {
143 # my $self = shift;
144 # TODO: do we want to do something after stop?
145 #};
146  
147 0 ▶sub _get_user_id {
Subroutine _get_user_id: not called CC=3 SCAR=24.8
148 0 my $self = shift;
149 0 my $user = $self->_call('GET','user');
150 0 ▶ return $user->{id} if $user && $user->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $user and $user->{"id"} 0 0 0
Condition0%0/3
$user and $user->{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$user and $user->{"id"}
$user $user->{"id"}
Truth table0%0/3
A and B
A= $userB= $user->{"id"}
ABresult
0X0
100
111
151 0 return;
152 }
153  
154 0 ▶sub _call {
Subroutine _call: not called CC=2 SCAR=17.9
155 0 my ($self,$method, $endpoint, $args) = @_;
156  
157 0 my $url = $self->config->{gitlab}{url}.'/api/v4/'.$endpoint;
158 0 my $res = $self->gitlab_client->request($method,$url);
159 0 ▶ if ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"success"}) 0 0 0
160 0 my $data = decode_json($res->{content});
161 0 return $data;
162 }
163 0 error_message(join(" ",$res->{status}, $res->{reason}, "\n" . $res->{content}));
164 }
165  
166 0 ▶sub App::TimeTracker::Data::Task::gitlab_issue {
Subroutine gitlab_issue: not called CC=3 SCAR=24.8
Pod undocumented
167 0 my $self = shift;
168 0 foreach my $tag ( @{ $self->tags } ) {
169 0 ▶ next unless $tag =~ /^issue#(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /^issue#(\d+)/ 0 0 0
170 0 return $1;
171 }
172 }
173  
174 1 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 1 1;
176  
177 __END__
↑ Summary