File Coverage

blib/lib/App/BitBucketCli/Command/PullRequests.pm
Criterion Covered Total %
statement 18 72 25.0
branch 0 26 0.0
condition 0 25 0.0
subroutine 6 8 75.0
pod 2 2 100.0
total 26 133 19.5


line stmt bran cond sub pod time code
1             package App::BitBucketCli::Command::PullRequests;
2              
3             # Created on: 2018-06-07 08:23:20
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 1     1   4330 use Moo;
  1         2  
  1         5  
10 1     1   291 use warnings;
  1         2  
  1         21  
11 1     1   4 use Carp;
  1         8  
  1         66  
12 1     1   5 use Data::Dumper qw/Dumper/;
  1         2  
  1         35  
13 1     1   4 use English qw/ -no_match_vars /;
  1         2  
  1         6  
14 1     1   1034 use Path::Tiny;
  1         9345  
  1         681  
15              
16             extends 'App::BitBucketCli';
17              
18             our $VERSION = 0.009;
19              
20             sub options {
21 0     0 1   return [qw/
22             colors|c=s%
23             create|C
24             force|f!
25             author|a=s
26             emails|e=s
27             to_branch|to-branch|t=s
28             from_branch|from-branch|f=s
29             title|T=s
30             state|S=s
31             long|l
32             project|p=s
33             participant|P=s
34             regexp|R
35             remote|m=s
36             repository|r=s
37             sleep|s=i
38             /]
39             }
40              
41             sub pull_requests {
42 0     0 1   my ($self) = @_;
43              
44 0           my $author = $self->opt->author();
45 0           my $to_branch = $self->opt->to_branch();
46 0           my $from_branch = $self->opt->from_branch();
47 0           my $title = $self->opt->title();
48 0           my $emails = $self->opt->emails();
49 0           my $participant = $self->opt->participant();
50              
51 0 0         if ( $self->opt->create() ) {
52             #https://stash.ext.springdigital-devisland.com.au/projects/SD/repos/onlinestyleguide/pull-requests
53             # ?create
54             # &targetBranch=refs%2Fheads%2Fproject_shop_performance
55             # &sourceBranch=refs%2Fheads%2Fbugfix%2FALM-48995_hidden-scroll-bar3
56             # &targetRepoId=12
57 0 0         if ( ! $self->opt->{from_branch} ) {
58 0           my $dir = `git rev-parse --show-toplevel`;
59 0           chomp $dir;
60 0           my $head = path($dir, '.git', 'HEAD');
61              
62 0 0         if ( -s $head ) {
63 0           $head = $head->slurp;
64 0           chomp $head;
65 0           $head =~ s{^ref: refs/heads/}{};
66 0           $self->opt->{from_branch} = $head;
67             }
68             }
69              
70 0           my $url = $self->core->url;
71 0           $url =~ s{^(https?://)([^/]+?@)}{$1};
72 0           $url =~ s{/rest/.*}{};
73             $url .= '/projects/' . $self->opt->{project} . '/repos/'
74 0           . $self->opt->{repository} . '/pull-requests?create';
75              
76 0 0         $url .= '&sourceBranch=refs/heads/' . $self->opt->{from_branch} if $self->opt->{from_branch};
77 0 0         $url .= '&targetBranch=' . ( $self->opt->{to_branch} ? 'refs/heads/' . $self->opt->{to_branch} : '' );
78 0           print "$url\n";
79 0           return;
80             }
81              
82             my @pull_requests = sort {
83 0           lc $a->id cmp lc $b->id;
84             }
85 0   0       $self->core->pull_requests($self->opt->{project}, $self->opt->{repository}, $self->opt->{state} || 'OPEN');
86              
87 0           my @prs;
88             my %max;
89              
90 0           for my $pull_request (@pull_requests) {
91 0 0 0       next if $author && $pull_request->author->{user}{displayName} !~ /$author/;
92 0 0 0       next if $to_branch && $pull_request->toRef->{displayId} !~ /$to_branch/;
93 0 0 0       next if $from_branch && $pull_request->fromRef->{displayId} !~ /$from_branch/;
94 0 0 0       next if $title && $pull_request->title !~ /$title/;
95 0 0 0       next if $emails && ! grep { /$emails/ } @{ $pull_request->emails };
  0            
  0            
96 0 0 0       next if $participant && ! grep { $_->{user}{displayName} =~ /$participant/ } @{ $pull_request->participants };
  0            
  0            
97              
98 0   0       my $tasks = eval { $pull_request->{openTasks}->[0] } || 0;
99             push @prs, {
100             id => $pull_request->id,
101             title => $pull_request->title,
102             author => $pull_request->author->{user}{displayName},
103             from => $pull_request->fromRef->{displayId},
104             to => $pull_request->toRef->{displayId},
105 0           emails => $pull_request->emails,
106             tasks => $tasks,
107             };
108 0           chomp $prs[-1]{title};
109 0           for my $key (keys %{ $prs[-1] }) {
  0            
110 0 0 0       $max{$key} = length $prs[-1]{$key} if ! $max{$key} || $max{$key} < length $prs[-1]{$key};
111             }
112             }
113              
114 0           for my $pr (@prs) {
115 0           printf "%-$max{id}s ", $pr->{id};
116 0           printf "%-$max{author}s ", $pr->{author};
117 0           printf "%-$max{tasks}s ", $pr->{tasks};
118 0           print "$pr->{title}\n";
119 0 0         if ( $self->opt->long ) {
120 0           print ' ', ( join ', ', @{ $pr->{emails} } ), "\n";
  0            
121             }
122             }
123             }
124              
125             1;
126              
127             __END__
128              
129             =head1 NAME
130              
131             App::BitBucketCli::Command::PullRequests - Show the pull requests of a repository
132              
133             =head1 VERSION
134              
135             This documentation refers to App::BitBucketCli::Command::PullRequests version 0.009
136              
137             =head1 SYNOPSIS
138              
139             bb-cli pull-requests [options]
140              
141             OPTIONS:
142             -c --colors[=]str Change colours used specified as key=value
143             eg --colors disabled=grey22
144             current colour names aborted, disabled and notbuilt
145             -C --create Construct the url to create a pull request using --from-branch
146             (or the current branch) and --to-branch.
147             -f --force Force action
148             -l --long Show long form data if possible
149             -p --project[=]str
150             For commands that need a project name this is the name to use
151             -R --recipient[=]str
152             ??
153             -R --regexp[=]str ??
154             -m --remote[=]str ??
155             -r --repository[=]str
156             For commands that work on repositories this contains the repository
157             -s --sleep[=]seconds
158             ??
159             -t --test ??
160             -a --author[=]regex
161             Show only pull requests by this author
162             -e --emails[=]regex
163             Show only pull requests involving anyone with an email
164             matching matching this regex.
165             -P --participant[=]regex
166             Show only pull requests with participants matching this regex
167             -t --to-branch[=](regex|branch)
168             Show only pull requests to this branch
169             -f --from-branch[=](regex|branch)
170             Show only pull requests from this branchx
171             -T --title[=]regex
172             Show only pull requests matching this title
173             -S --state[=](OPEN|MERGED|DECLINED|ALL)
174             Show pull requests of this type (Default OPEN)
175              
176             CONFIGURATION:
177             -h --host[=]str Specify the Stash/Bitbucket Servier host name
178             -P --password[=]str
179             The password to connect to the server as
180             -u --username[=]str
181             The username to connect to the server as
182              
183             -v --verbose Show more detailed option
184             --version Prints the version information
185             --help Prints this help information
186             --man Prints the full documentation for bb-cli
187              
188             =head1 DESCRIPTION
189              
190             =head1 SUBROUTINES/METHODS
191              
192             =head2 C<options ()>
193              
194             Returns the command line options
195              
196             =head2 C<pull_requests ()>
197              
198             =head1 DIAGNOSTICS
199              
200             =head1 CONFIGURATION AND ENVIRONMENT
201              
202             =head1 DEPENDENCIES
203              
204             =head1 INCOMPATIBILITIES
205              
206             =head1 BUGS AND LIMITATIONS
207              
208             There are no known bugs in this module.
209              
210             Please report problems to Ivan Wills (ivan.wills@gmail.com).
211              
212             Patches are welcome.
213              
214             =head1 AUTHOR
215              
216             Ivan Wills - (ivan.wills@gmail.com)
217              
218             =head1 LICENSE AND COPYRIGHT
219              
220             Copyright (c) 2018 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
221             All rights reserved.
222              
223             This module is free software; you can redistribute it and/or modify it under
224             the same terms as Perl itself. See L<perlartistic>. This program is
225             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
226             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
227             PARTICULAR PURPOSE.
228              
229             =cut