blib/lib/MYDan/Code/Gitlab.pm untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 271 CC 27 SCAR 43.3
CC
27
Cov
0%
searchUserProjects
182.0
_initUser
42.0
searchUserGroups
20.0
CRAP 75.7
1 package MYDan::Code::Gitlab;
2 use strict;
3 use warnings;
4  
5 use GitLab::API::v4;
6  
7 $|++;
8  
9 sub new
10 {
11 my ( $class, %self ) = splice @_;
12 map{ die "$_ undef" unless $self{$_} }qw( apiurl token name );
13  
14 $self{api} = GitLab::API::v4->new(
15 url => $self{apiurl},
16 private_token => $self{token},
17 );
18  
19 delete $self{user};
20 $self{email} = +{};
21  
22 bless \%self, ref $class || $class;
23 }
24  
25 sub searchUser
26 {
27 my ( $this, $user ) = @_;
28 $this->searchUserGroups( $user );
29 $this->searchUserProjects( $user );
30 }
31  
32 sub searchUserProjects
33 {
34 my ( $this, $user ) = @_;
35 return unless $this->_initUser( $user );
36  
37 my $pp = $this->{api}->sudo( $this->{user}{username} )
38 ->paginator( 'projects', +{ membership => 1 } );
39  
40 my ( $i, %groupowner ) = ( 0 );
41 while( my $project = $pp->next() )
42 {
43 my ( $type, $typeid, @owner ) = map { $project->{namespace}{$_} }qw( kind id );
44  
45 my $m = $this->{api}->project_members( $project->{id} );
46 for my $mm ( @$m )
47 {
48 next if $mm->{state} ne 'active' || $mm->{access_level} != 50;
49  
50 unless( $this->{email}{$mm->{id}} )
51 {
52 my $user = $this->{api}->user( $mm->{id} );
53 $this->{email}{$mm->{id}} = $user->{email} || $mm->{username};
54 }
55  
56 push @owner, $mm->{id};
57 }
58  
59 my $bg = ( $type eq 'group' && $this->{groups}{$typeid} ) ? '@ByGroup' : '';
60  
61 if( ! @owner && $type eq 'group' && ! $bg )
62 {
63 if( defined $groupowner{$typeid} )
64 {
65 @owner = @{$groupowner{$typeid}};
66 }
67 else
68 {
69 my $m = $this->{api}->group_members( $typeid );
70 for my $mm ( @$m )
71 {
72 next if $mm->{state} ne 'active' || $mm->{access_level} != 50;
73 unless( $this->{email}{$mm->{id}} )
74 {
75 my $user = $this->{api}->user( $mm->{id} );
76 $this->{email}{$mm->{id}} = $user->{email} || $mm->{username};
77 }
78  
79 push @owner, $mm->{id};
80 $groupowner{$typeid} = \@owner;
81 }
82 }
83 $bg = '@GroupOwner';
84 }
85  
86 $bg = '@Self' if ! @owner && !$bg && $project->{web_url} =~ /^$this->{user}{web_url}/;
87  
88 if( ! @owner && !$bg && $project->{owner} && $project->{owner}{state} eq 'active' )
89 {
90 my $mm = $project->{owner};
91  
92 unless( $this->{email}{$mm->{id}} )
93 {
94 my $user = $this->{api}->user( $mm->{id} );
95 $this->{email}{$mm->{id}} = $user->{email} || $mm->{username};
96 }
97  
98 push @owner, $mm->{id};
99 $bg = '@ProjectOwner';
100 }
101  
102 $bg = 'NoFind' if ! @owner && !$bg;
103  
104 printf "[%i]: $this->{user}{email} $this->{name} project.$type $project->{web_url}\t%s $bg\n", ++$i,
105 join ' ', map{ "$this->{email}{$_}" }@owner;
106 }
107  
108 return $this;
109 }
110  
111 sub searchUserGroups
112 {
113 my ( $this, $user ) = @_;
114  
115 return unless $this->_initUser( $user );
116  
117 my $i = 0;
118 my $gp = $this->{api}->sudo( $this->{user}{username} )
119 ->paginator( 'groups', +{ min_access_level => 10 } );
120 while ( my $group = $gp->next() )
121 {
122 $this->{groups}{$group->{id}}++;
123 my @owner;
124 my $m = $this->{api}->group_members( $group->{id} );
125 for my $mm ( @$m )
126 {
127 next if $mm->{state} ne 'active' || $mm->{access_level} < 40;
128  
129 unless( $this->{email}{$mm->{id}} )
130 {
131 my $user = $this->{api}->user( $mm->{id} );
132 $this->{email}{$mm->{id}} = $user->{email} || $mm->{username};
133 }
134  
135 push @owner, $mm->{id};
136 }
137  
138 printf "[%i]: $this->{user}{email} $this->{name} group $group->{web_url}\t%s\n", ++$i,
139 join ' ', map{ "$this->{email}{$_}" }@owner;
140 }
141  
142 return $this;
143 }
144  
145  
146 sub getUsers
147 {
148 my ( $this, %email )= shift;
149  
150 my $i = 0;
151 my $pp = $this->{api}->paginator( 'users' );
152 while (my $user = $pp->next()) {
153 $email{$user->{email}} ++ if $user->{state} eq 'active';
154 printf "[%i]: $this->{name} $user->{email} $user->{state}\n", ++$i;
155 }
156 return %email;
157 }
158  
159 sub blockUser
160 {
161 my ( $this, $user ) = @_;
162 return unless $this->_initUser( $user );
163  
164 print "$this->{name} lock user.email=$this->{user}{email} user.id=$this->{user}{id}\n";
165 warn "block fail\n" unless $this->{api}->block_user( $this->{user}{id} );
166 }
167  
168 sub unblockUser
169 {
170 my ( $this, $user ) = @_;
171 return unless $this->_initUser( $user );
172  
173 print "$this->{name} unlock user.email=$this->{user}{email} user.id=$this->{user}{id}\n";
174 warn "unblock fail\n" unless $this->{api}->unblock_user( $this->{user}{id} );
175 }
176  
177 sub _initUser
178 {
179 my ( $this, $user ) = @_;
180  
181 return 1 if $this->{user};
182  
183 if( $user =~ /(.+)@/ )
184 {
185  
186 my $u = $this->{api}->users( +{ username => $1 } );
187 if( @$u )
188 {
189 $this->{user} = $u->[0];
190 return 1;
191 }
192  
193 my $userspage = $this->{api}->paginator( 'users' );
194 while( my $u = $userspage->next() ) {
195 $this->{email}{$u->{id}} = $u->{email} || $u->{username};
196 if( $u->{email} eq $user )
197 {
198 $this->{user} = $u;
199 return 1;
200 }
201 }
202 }
203 else
204 {
205 my $u = $this->{api}->users( +{ username => $user } );
206 return 0 unless @$u;
207 $this->{user} = $u->[0];
208 return 1;
209 }
210  
211 return 0;
212 }
213  
214 1;