API/GitForge.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 83 CC 2 SCAR 9.4
CC
2
Cov
0%
new_from_domain
6.0
forge_access_token
2.0
remote_forge_info
2.0
CRAP 2.6
1 package API::GitForge;
2 # ABSTRACT: generic interface to APIs of sites like GitHub, GitLab etc.
3 #
4 # Copyright (C) 2020 Sean Whitton <spwhitton@spwhitton.name>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or (at
9 # your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 $API::GitForge::VERSION = '0.005';
19  
20 use 5.028;
21 use strict;
22 use warnings;
23  
24 use Carp;
25 use Exporter "import";
26 use File::Spec::Functions qw(catfile);
27 use Git::Wrapper;
28 use Cwd;
29 use API::GitForge::GitHub;
30 use API::GitForge::GitLab;
31  
32 our @EXPORT_OK = qw(new_from_domain forge_access_token remote_forge_info);
33  
34 our %known_forges = (
35 "github.com" => "API::GitForge::GitHub",
36 "salsa.debian.org" => "API::GitForge::GitLab",
37 );
38  
39  
40 sub new_from_domain {
41 my %opts = @_;
42 croak "unknown domain" unless exists $known_forges{ $opts{domain} };
43 $known_forges{ $opts{domain} }->new(%opts);
44 }
45  
46  
47 sub forge_access_token {
48 my $domain = shift;
49 my $root = $ENV{XDG_CONFIG_HOME} || catfile $ENV{HOME}, ".config";
50 my $file = catfile $root, "gitforge", "access_tokens", $domain;
51 -e $file and -r _ or croak "$file does not exist or is not readable";
52 open my $fh, "<", $file or die "failed to open $file for reading: $!";
53 chomp(my $key = <$fh>);
54 $key;
55 }
56  
57  
58 sub remote_forge_info {
59 my $remote = shift;
60 my $git = Git::Wrapper->new(getcwd);
61 my ($uri) = $git->remote("get-url", $remote);
62 $uri =~ m#^https?://([^:/@]+)/#
63 or $uri =~ m#^(?:\w+\@)?([^:/@]+):#
64 or croak "couldn't determine git forge info from $remote remote";
65 ($1, $');
66 }
67  
68 1;
69  
70 __END__