blib/lib/Dist/Zilla/Plugin/Author/GETTY/GiteaMeta.pm

filter: Statementstmt 96.4% 54 / 56 Branchbran 76.9% 20 / 26 Conditioncond 63.6% 7 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 84.5% 93 / 110 CC 18 SCAR 14.3
CC
18
Cov
87%
_slug
6.1
_git_output
6.1
_build_resources
4.0
CRAP 4.2
« .github/dzil-binary/require-gate.pl ↑ Summary .github/dzil-binary/win-dlls.pl »
1 package Dist::Zilla::Plugin::Author::GETTY::GiteaMeta;
2 # ABSTRACT: Add Gitea/Forgejo (incl. Codeberg) repository metadata to META.{json,yml}
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 with 'Dist::Zilla::Role::MetaProvider';
6  
7 3 our $VERSION = '0.328';
8  
9  
10  
11 3 has remote => (
12 is => 'ro',
13 isa => 'Str',
14 default => 'origin',
15 );
16  
17  
18 3 has host => (
19 is => 'ro',
20 isa => 'Str',
21 default => 'codeberg.org',
22 );
23  
24  
25 3 has repo => (
26 is => 'ro',
27 isa => 'Maybe[Str]',
28 );
29  
30  
31 3 has issues => (
32 is => 'ro',
33 isa => 'Bool',
34 default => 1,
35 );
36  
37  
38 3 has homepage => (
39 is => 'ro',
40 isa => 'Str',
41 default => 'repo',
42 );
43  
44 3 has _remote_info => (
45 is => 'ro',
46 isa => 'Maybe[HashRef]',
47 lazy => 1,
48 builder => '_build_remote_info',
49 );
50  
51 5 ▶sub _build_remote_info {
Subroutine _build_remote_info: called CC=3 SCAR=11.0
52 5 my ($self) = @_;
53 5 ▶ chomp(my $url = _git_output('config', '--get', 'remote.' . $self->remote . '.url') // '');
Condition100%2/2
_git_output("config", "--get", 'remote.' . $self->remote . '.url') // ""
l!l
32
MC/DC100%1/1
_git_output("config", "--get", 'remote.' . $self->remote . '.url') // ""
_git_output("config", "--get", 'remote.' . $self->remote . '.url')
Truth table100%2/2
A
A= _git_output("config", "--get", 'remote.' . $self->remote . '.url')
Aresult
11
00
54 5 ▶ return undef unless length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless length $url 2 3 5
55 3 return $self->_parse_remote_url($url);
56 }
57  
58 # Output of `git ARGS` in the cwd; undef when git cannot be started or exits
59 # non-zero. git runs without a shell, so the remote name reaches it as it is,
60 # also on Windows (cmd.exe knows no 2>/dev/null). Its STDERR goes to the null
61 # device. The same helper as in Dist::Zilla::PluginBundle::Author::GETTY.
62 5 ▶sub _git_output {
Subroutine _git_output: called CC=6 SCAR=18.1
63 5 my (@args) = @_;
64 5 ▶ open my $stderr, '>&', \*STDERR or die "cannot dup STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $stderr, ">&", \*STDERR or die "cannot dup STDERR: $!\n" 5 0 5
65 5 my $fh;
66 5 ▶ my $started = open(STDERR, '>', File::Spec->devnull) && open($fh, '-|', 'git', @args);
Condition66%2/3
open(STDERR, '>', 'File::Spec'->devnull) && open($fh, '-|', 'git', @args)
!ll&&!rl&&r
014
MC/DC50%1/2
open(STDERR, '>', 'File::Spec'->devnull) && open($fh, '-|', 'git', @args)
open(STDERR, '>', 'File::Spec'->devnull) open($fh, '-|', 'git', @args)
Truth table66%2/3
A && B
A= open(STDERR, '>', 'File::Spec'->devnull)B= open($fh, '-|', 'git', @args)
ABresult
0X0
100
111
67 5 ▶ open STDERR, '>&', $stderr or die "cannot restore STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $stderr or die "cannot restore STDERR: $!\n" 5 0 5
68 5 ▶ return undef unless $started;
Branch: 1 branch
BranchTrueFalseTotal
unless $started 1 4 5
69 4 my $out = do { local $/; <$fh> };
70 4 ▶ return close($fh) ? $out : undef;
Branch: 1 branch
BranchTrueFalseTotal
close $fh ? : 3 1 4
71 }
72  
73 # Parse a git remote URL into { host => ..., slug => 'owner/repo' }, or undef.
74 # Pure helper (no git), kept separate so the regex can be unit-tested.
75 10 ▶sub _parse_remote_url {
Subroutine _parse_remote_url: called CC=3 SCAR=11.0
76 10 my ($self, $url) = @_;
77 # git@host:owner/repo.git | https://host/owner/repo(.git) | ssh://git@host/owner/repo.git
78 10 my ($host, $slug) = $url =~ m{
79 (?: ^ \w+ :// (?: [^/\@]+ \@ )? | ^ [^/\@]+ \@ ) # scheme[//user@] or user@
80 ( [^/:]+ ) # host
81 [:/]+
82 ( .+? ) # owner/repo...
83 (?: \.git )? /? $
84 }x;
85 10 ▶ return undef unless $host && $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless $host and $slug 1 9 10
Condition66%2/3
$host and $slug
!ll&&!rl&&r
109
MC/DC50%1/2
$host and $slug
$host $slug
Truth table66%2/3
A and B
A= $hostB= $slug
ABresult
0X0
100
111
86 9 return { host => $host, slug => $slug };
87 }
88  
89 # Resolve the owner/repo slug for $self->host. An explicit repo= override is
90 # trusted; otherwise the git remote must point at the expected host. Returns
91 # undef (no usable target) if there is no remote or it points elsewhere.
92 1 ▶sub _slug {
Subroutine _slug: called CC=5 SCAR=18.1
93 1 my ($self) = @_;
94 1 ▶ return $self->repo if defined $self->repo && length $self->repo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->repo and length $self->repo 0 1 1
Condition33%1/3
defined $self->repo and length $self->repo
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->repo and length $self->repo
defined $self->repo length $self->repo
Truth table33%1/3
A and B
A= defined $self->repoB= length $self->repo
ABresult
0X0
100
111
95  
96 1 ▶ my $info = $self->_remote_info or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->_remote_info or return undef 1 0 1
97 1 ▶ return undef unless $info->{host} eq $self->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"host"} eq $self->host 0 1 1
98 1 return $info->{slug};
99 }
100  
101 1 ▶sub metadata {
Subroutine metadata: called CC=2 SCAR=7.9
Pod undocumented
102 1 my ($self) = @_;
103  
104 1 my $slug = $self->_slug;
105 1 ▶ unless (defined $slug) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $slug) 0 1 1
106 0 $self->log_debug('no meta target found -- skipping repository metadata');
107 0 return {};
108 }
109  
110 return {
111 1 resources => $self->_build_resources(
112 host => $self->host,
113 slug => $slug,
114 issues => $self->issues,
115 homepage => $self->homepage,
116 dist_name => $self->zilla->name,
117 ),
118 };
119 }
120  
121 # Pure assembly of the resources hashref. Kept free of zilla/git so it can be
122 # unit-tested as a class method.
123 5 ▶sub _build_resources {
Subroutine _build_resources: called CC=4 SCAR=13.9
124 5 my ($self, %a) = @_;
125 5 my $web = "https://$a{host}/$a{slug}";
126  
127 5 my %resources = (
128 repository => {
129 type => 'git',
130 url => "$web.git",
131 web => $web,
132 },
133 );
134  
135 5 ▶ $resources{bugtracker} = { web => "$web/issues" } if $a{issues};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"issues"} 4 1 5
136  
137 5 ▶ if ($a{homepage} eq 'repo') {
Branch: 2 branches
BranchTrueFalseTotal
if ($a{'homepage'} eq 'repo') { } 3 2 5
elsif ($a{'homepage'} eq 'metacpan') { } 1 1 2
138 3 $resources{homepage} = $web;
139 }
140 elsif ($a{homepage} eq 'metacpan') {
141 1 $resources{homepage} = "https://metacpan.org/release/$a{dist_name}";
142 }
143 # 'none' -> leave homepage unset
144  
145 5 return \%resources;
146 }
147  
148 3 __PACKAGE__->meta->make_immutable;
149 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 3 1;
151  
152 __END__
« .github/dzil-binary/require-gate.pl ↑ Summary .github/dzil-binary/win-dlls.pl »