| 1 | package Dist::Zilla::Plugin::DBIO::CodebergMeta; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Add Codeberg (or a Forgejo instance via host=) repository metadata to META.{json,yml} | ||||||||||||||||||||||||||||
| 3 | 8 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use IPC::Open3 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Symbol (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | with 'Dist::Zilla::Role::MetaProvider'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 8 | has remote => ( | |||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||
| 13 | isa => 'Str', | ||||||||||||||||||||||||||||
| 14 | default => 'origin', | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 8 | has host => ( | |||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | isa => 'Str', | ||||||||||||||||||||||||||||
| 21 | default => 'codeberg.org', | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | has repo => ( | |||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||
| 27 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 8 | has issues => ( | |||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||
| 33 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 34 | default => 1, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 8 | has homepage => ( | |||||||||||||||||||||||||||
| 39 | is => 'ro', | ||||||||||||||||||||||||||||
| 40 | isa => 'Str', | ||||||||||||||||||||||||||||
| 41 | default => 'repo', | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 8 | has _remote_url => ( | |||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 47 | lazy => 1, | ||||||||||||||||||||||||||||
| 48 | builder => '_build_remote_url', | ||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 8 | has _remote_info => ( | |||||||||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||||||||||
| 53 | isa => 'Maybe[HashRef]', | ||||||||||||||||||||||||||||
| 54 | lazy => 1, | ||||||||||||||||||||||||||||
| 55 | builder => '_build_remote_info', | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # The URL of the configured remote, asked of git at the dist root (list form, | ||||||||||||||||||||||||||||
| 59 | # no shell), or undef when there is none. | ||||||||||||||||||||||||||||
| 60 | 6 | ▶ | sub _build_remote_url { | ||||||||||||||||||||||||||
|
Subroutine
_build_remote_url: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 61 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 62 | 6 | my $remote = $self->remote; | |||||||||||||||||||||||||||
| 63 | 6 | my $err = Symbol::gensym(); | |||||||||||||||||||||||||||
| 64 | 6 | my ($in, $out); | |||||||||||||||||||||||||||
| 65 | 6 | ▶ | my $pid = eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 6 | IPC::Open3::open3($in, $out, $err, 'git', '-C', $self->zilla->root->stringify, | |||||||||||||||||||||||||||
| 67 | 'config', '--get', "remote.$remote.url"); | ||||||||||||||||||||||||||||
| 68 | } or return undef; | ||||||||||||||||||||||||||||
| 69 | 6 | close $in; | |||||||||||||||||||||||||||
| 70 | 6 | my $url = do { local $/; <$out> }; | |||||||||||||||||||||||||||
| 71 | 6 | waitpid $pid, 0; | |||||||||||||||||||||||||||
| 72 | 6 | ▶ | return undef unless defined $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 6 | $url =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 74 | 6 | ▶ | return length $url ? $url : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | sub _build_remote_info { | ||||||||||||||||||||||||||
|
Subroutine
_build_remote_info: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 78 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 79 | 6 | my $url = $self->_remote_url; | |||||||||||||||||||||||||||
| 80 | 6 | ▶ | return undef unless defined $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 6 | return $self->_parse_remote_url($url); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Parse a git remote URL into { host => ..., slug => 'owner/repo' }, or undef. | ||||||||||||||||||||||||||||
| 85 | # Pure helper (no git), kept separate so the regex can be unit-tested. | ||||||||||||||||||||||||||||
| 86 | 13 | ▶ | sub _parse_remote_url { | ||||||||||||||||||||||||||
|
Subroutine
_parse_remote_url: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 87 | 13 | my ($self, $url) = @_; | |||||||||||||||||||||||||||
| 88 | # git@host:owner/repo.git | https://host/owner/repo(.git) | ssh://git@host/owner/repo.git | ||||||||||||||||||||||||||||
| 89 | 13 | my ($host, $slug) = $url =~ m{ | |||||||||||||||||||||||||||
| 90 | (?: ^ \w+ :// (?: [^/\@]+ \@ )? | ^ [^/\@]+ \@ ) # scheme[//user@] or user@ | ||||||||||||||||||||||||||||
| 91 | ( [^/:]+ ) # host | ||||||||||||||||||||||||||||
| 92 | [:/]+ | ||||||||||||||||||||||||||||
| 93 | ( .+? ) # owner/repo... | ||||||||||||||||||||||||||||
| 94 | (?: \.git )? /? $ | ||||||||||||||||||||||||||||
| 95 | }x; | ||||||||||||||||||||||||||||
| 96 | 13 | ▶ | return undef unless $host && $slug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$host and $slug
MC/DC50%1/2
$host and $slug
$host
$slug
Truth table66%2/3
A and B
A= $hostB= $slug
| |||||||||||||||||||||||||||||
| 97 | 11 | return { host => $host, slug => $slug }; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Resolve the owner/repo slug for $self->host. An explicit repo= override is | ||||||||||||||||||||||||||||
| 101 | # trusted; otherwise the git remote must point at the expected host. Returns | ||||||||||||||||||||||||||||
| 102 | # the slug, or (undef, $reason) when there is no usable target: no remote, a | ||||||||||||||||||||||||||||
| 103 | # URL that is not understood, or a remote on another host. | ||||||||||||||||||||||||||||
| 104 | 6 | ▶ | sub _resolve { | ||||||||||||||||||||||||||
|
Subroutine
_resolve: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 105 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 106 | 6 | ▶ | return $self->repo if defined $self->repo && length $self->repo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->repo and length $self->repo
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
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 6 | my ($remote, $host) = ($self->remote, $self->host); | |||||||||||||||||||||||||||
| 109 | 6 | my $url = $self->_remote_url; | |||||||||||||||||||||||||||
| 110 | 6 | ▶ | return (undef, "git remote '$remote' has no URL in " . $self->zilla->root) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | unless defined $url; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | (my $shown = $url) =~ s{^(\w+://)[^/\@]*\@}{$1}; # no credentials in the log | |||||||||||||||||||||||||||
| 114 | 6 | ▶ | my $info = $self->_remote_info | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | or return (undef, "git remote '$remote' URL '$shown' is not understood"); | ||||||||||||||||||||||||||||
| 116 | return (undef, "git remote '$remote' points at $info->{host}, not at $host") | ||||||||||||||||||||||||||||
| 117 | 5 | ▶ | unless $info->{host} eq $host; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 3 | return $info->{slug}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub _slug { | ||||||||||||||||||||||||||
|
Subroutine
_slug: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 122 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 123 | 0 | my ($slug) = $self->_resolve; | |||||||||||||||||||||||||||
| 124 | 0 | return $slug; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 6 | ▶ | sub metadata { | ||||||||||||||||||||||||||
|
Subroutine
metadata: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 6 | my ($slug, $why) = $self->_resolve; | |||||||||||||||||||||||||||
| 131 | 6 | ▶ | unless (defined $slug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 3 | $self->log("$why -- skipping repository metadata (META gets no repository, bugtracker or homepage)"); | |||||||||||||||||||||||||||
| 133 | 3 | return {}; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | return { | ||||||||||||||||||||||||||||
| 137 | 3 | resources => $self->_build_resources( | |||||||||||||||||||||||||||
| 138 | host => $self->host, | ||||||||||||||||||||||||||||
| 139 | slug => $slug, | ||||||||||||||||||||||||||||
| 140 | issues => $self->issues, | ||||||||||||||||||||||||||||
| 141 | homepage => $self->homepage, | ||||||||||||||||||||||||||||
| 142 | dist_name => $self->zilla->name, | ||||||||||||||||||||||||||||
| 143 | ), | ||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # Pure assembly of the resources hashref. Kept free of zilla/git so it can be | ||||||||||||||||||||||||||||
| 148 | # unit-tested as a class method. | ||||||||||||||||||||||||||||
| 149 | 7 | ▶ | sub _build_resources { | ||||||||||||||||||||||||||
|
Subroutine
_build_resources: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 150 | 7 | my ($self, %a) = @_; | |||||||||||||||||||||||||||
| 151 | 7 | my $web = "https://$a{host}/$a{slug}"; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 7 | my %resources = ( | |||||||||||||||||||||||||||
| 154 | repository => { | ||||||||||||||||||||||||||||
| 155 | type => 'git', | ||||||||||||||||||||||||||||
| 156 | url => "$web.git", | ||||||||||||||||||||||||||||
| 157 | web => $web, | ||||||||||||||||||||||||||||
| 158 | }, | ||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 7 | ▶ | $resources{bugtracker} = { web => "$web/issues" } if $a{issues}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 7 | ▶ | if ($a{homepage} eq 'repo') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 164 | 5 | $resources{homepage} = $web; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | elsif ($a{homepage} eq 'metacpan') { | ||||||||||||||||||||||||||||
| 167 | 1 | $resources{homepage} = "https://metacpan.org/release/$a{dist_name}"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | # 'none' -> leave homepage unset | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 7 | return \%resources; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 8 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 175 | 8 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 176 | 8 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||