blib/lib/Alien/Build/Plugin/Download/GitLab.pm

filter: Statementstmt 44.8% 39 / 87 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.9% 10 / 13 Podpod 100% 1 / 1 totaltotal 35.4% 50 / 141 CC 21 SCAR 48.0
CC
21
Cov
32%
__ANON__
240.0
init
56.0
__ANON__
2.0
CRAP 121.3
↑ Summary
1 package Alien::Build::Plugin::Download::GitLab;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::PP qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI::Escape qw( uri_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Alien::Build::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: Alien::Build plugin to download from GitLab
15 1 our $VERSION = '0.01'; # VERSION
16  
17  
18 1 has gitlab_host => 'https://gitlab.com';
19 1 has gitlab_user => undef;
20 1 has gitlab_project => undef;
21  
22  
23 1 has type => 'source'; # source or link
24  
25  
26 1 has format => 'tar.gz';
27  
28  
29 1 has version_from => 'tag_name'; # tag_name or name
30 1 has convert_version => undef;
31 1 has link_name => undef;
32  
33 sub init
34 0 ▶{
Subroutine init: not called CC=7 SCAR=40.3
Pod documented
35 0 my($self, $meta) = @_;
36  
37 0 ▶ croak("No gitlab_user provided") unless defined $self->gitlab_user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->gitlab_user 0 0 0
38 0 ▶ croak("No gitlab_project provided") unless defined $self->gitlab_project;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->gitlab_project 0 0 0
39 0 ▶ croak("Don't set set a start_url with the Download::GitLab plugin") if defined $meta->prop->{start_url};
Branch: 1 branch
BranchTrueFalseTotal
if defined $meta->prop->{"start_url"} 0 0 0
40  
41 0 $meta->add_requires('configure' => 'Alien::Build::Plugin::Download::GitLab' => 0 );
42  
43 0 my $url = URI->new($self->gitlab_host);
44 0 $url->path("/api/v4/projects/@{[ uri_escape(join '/', $self->gitlab_user, $self->gitlab_project) ]}/releases");
45 0 ▶ $meta->prop->{start_url} ||= "$url";
Condition0%0/3
$meta->prop->{"start_url"} ||= "$url"
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta->prop->{"start_url"} ||= "$url"
$meta->prop->{"start_url"} "$url"
Truth table0%0/3
A ||= B
A= $meta->prop->{"start_url"}B= "$url"
ABresult
1X1
011
000
46  
47 0 $meta->apply_plugin('Download');
48 0 $meta->apply_plugin('Extract', format => $self->format );
49  
50 # we assume that GitLab returns the releases in reverse
51 # chronological order.
52 $meta->register_hook(
53 0 ▶ prefer => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 my($build, $res) = @_;
55 0 return $res;
56 },
57 0 );
58  
59 0 ▶ croak "type must be one of source or link" if $self->type !~ /^(source|link)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type =~ /^(source|link)$/ 0 0 0
60 0 ▶ croak "version_from must be one of tag_name or name" if $self->version_from !~ /^(tag_name|name)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->version_from =~ /^(tag_name|name)$/ 0 0 0
61  
62 ## TODO insert tokens as header if possible
63 ## This may help with rate limiting (or if not then don't bother)
64 # curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/24/releases"
65  
66 $meta->around_hook(
67 0 ▶ fetch => sub {
Subroutine __ANON__: not called CC=15 SCAR=54.8
68 0 my $orig = shift;
69 0 my($build, $url, @the_rest) = @_;
70  
71 # only modify the response if we are using the GitLab API
72 # to get the release list
73 return $orig->($build, $url, @the_rest)
74 0 ▶ if defined $url && $url ne $meta->prop->{start_url};
Branch: 1 branch
BranchTrueFalseTotal
if defined $url and $url ne $meta->prop->{"start_url"} 0 0 0
Condition0%0/3
defined $url and $url ne $meta->prop->{"start_url"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $url and $url ne $meta->prop->{"start_url"}
defined $url $url ne $meta->prop->{"start_url"}
Truth table0%0/3
A and B
A= defined $urlB= $url ne $meta->prop->{"start_url"}
ABresult
0X0
100
111
75  
76 0 my $res = $orig->($build, $url, @the_rest);
77  
78 0 my $res2 = {
79 type => 'list',
80 list => [],
81 };
82  
83 0 ▶ $res2->{protocol} = $res->{protocol} if exists $res->{protocol};
Branch: 1 branch
BranchTrueFalseTotal
if exists $res->{"protocol"} 0 0 0
84  
85 0 my $rel;
86 0 ▶ if($res->{content})
Branch: 2 branches
BranchTrueFalseTotal
if ($res->{'content'}) { } 0 0 0
elsif ($res->{'path'}) { } 0 0 0
87 {
88 0 $rel = decode_json $res->{content};
89 }
90 elsif($res->{path})
91 {
92 0 $rel = decode_json path($res->{path})->slurp_raw;
93 }
94 else
95 {
96 0 croak("malformed response object: no content or path");
97 }
98  
99 0 foreach my $release (@$rel)
100 {
101 0 ▶ my $version = $self->version_from eq 'name' ? $release->{name} : $release->{tag_name};
Branch: 1 branch
BranchTrueFalseTotal
$self->version_from eq 'name' ? : 0 0 0
102 0 ▶ $version = $self->convert_version->($version) if $self->convert_version;
Branch: 1 branch
BranchTrueFalseTotal
if $self->convert_version 0 0 0
103  
104 0 ▶ if($self->type eq 'source')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq 'source') { } 0 0 0
105 {
106 0 foreach my $source (@{ $release->{assets}->{sources} })
107 {
108 0 ▶ next unless $source->{format} eq $self->format;
Branch: 1 branch
BranchTrueFalseTotal
unless $source->{"format"} eq $self->format 0 0 0
109 0 my $url = URI->new($source->{url});
110 0 my $filename = basename $url->path;
111 0 push @{ $res2->{list} }, {
112 filename => $filename,
113 url => $source->{url},
114 0 version => $version,
115 };
116 }
117 }
118 else # link
119 {
120 0 foreach my $link (@{ $release->{assets}->{links} })
121 {
122 0 my $url = URI->new($link->{url});
123 0 my $filename => basename $url->path;
124 0 ▶ if($self->link_name)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->link_name) 0 0 0
125 {
126 0 ▶ next unless $filename =~ $self->link_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ $self->link_name 0 0 0
127 }
128 0 push @{ $res2->{list} }, {
129 filename => $filename,
130 url => $link->{url},
131 0 version => $version,
132 };
133 }
134 }
135 }
136  
137 0 return $res2;
138  
139 },
140 0 );
141 }
142  
143 1 1;
144  
145 __END__
↑ Summary