API/GitForge/GitHub.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 120 CC 6 SCAR 18.9
CC
6
Cov
0%
_ensure_repo
20.0
_make_api
6.0
_ensure_fork
6.0
CRAP 6.6
1 package API::GitForge::GitHub;
2 # ABSTRACT: common git forge operations using the GitHub API
3 #
4 # Copyright (C) 2017, 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::GitHub::VERSION = '0.005';
19  
20 use 5.028;
21 use strict;
22 use warnings;
23  
24 use Role::Tiny::With;
25  
26 use Carp;
27 use Net::GitHub;
28  
29 with "API::GitForge::Role::GitForge";
30  
31 sub _make_api {
32 my $self = shift;
33 my %opts;
34 $opts{access_token} = $self->{_access_token}
35 if exists $self->{_access_token};
36 $self->{_api} = Net::GitHub->new(%opts);
37 }
38  
39 sub _ensure_fork {
40 my ($self, $upstream) = @_;
41 my ($org, $repo) = _extract_repo($upstream);
42  
43 my $repos = $self->{_api}->repos;
44 my $user = $self->{_api}->user->show->{login};
45 my @user_repos = $repos->list_user($user);
46 my $repo_exists = sub {
47 grep { $_->{name} eq $repo } @user_repos;
48 };
49 if (&$repo_exists) {
50 $self->_assert_fork_has_parent($upstream);
51 } else {
52 $repos->create_fork($org, $repo);
53 until (&$repo_exists) {
54 sleep 5;
55 @user_repos = $repos->list_user($user);
56 }
57 }
58 return "https://github.com/$user/$repo";
59 }
60  
61 sub _assert_fork_has_parent {
62 my ($self, $upstream) = @_;
63 my (undef, $repo) = _extract_repo($upstream);
64 my $user = $self->{_api}->user->show->{login};
65 my $fork = $self->{_api}->repos->get($user, $repo);
66  
67 $fork->{parent}{full_name} eq $upstream
68 or croak
69 "$user/$repo does not have parent $upstream; don't know what to do";
70 }
71  
72 sub _clean_config_repo {
73 my ($self, $target) = @_;
74 my ($org, $repo) = _extract_repo($target);
75 my $repos = $self->{_api}->repos;
76 $repos->set_default_user_repo($org, $repo);
77 $repos->update({
78 name => "$repo",
79 has_wiki => 0,
80 has_issues => 0,
81 has_downloads => 0,
82 has_pages => 0,
83 has_projects => 0,
84 });
85 }
86  
87 sub _clean_config_fork {
88 my ($self, $upstream) = @_;
89 my (undef, $repo) = _extract_repo($upstream);
90 my $user = $self->{_api}->user->show->{login};
91  
92 my $repos = $self->{_api}->repos;
93 $repos->set_default_user_repo($user, $repo);
94 $repos->update({
95 name => "$repo",
96 homepage => "",
97 description => "Temporary fork for pull request(s)",
98 default_branch => "gitforge",
99 });
100  
101 $self->_clean_config_repo("$user/$repo");
102 }
103  
104 sub _ensure_repo {
105 my ($self, $target) = @_;
106 my ($org, $repo) = _extract_repo($target);
107 my $repos = $self->{_api}->repos;
108 my $user = $self->{_api}->user->show->{login};
109 my %create_opts = (name => $repo);
110 my $list_method;
111 if ($org eq $user) {
112 $list_method = "list_user";
113 } else {
114 $list_method = "list_org";
115 $create_opts{org} = $org unless $org eq $user;
116 }
117 my @list_repos = $repos->$list_method($org);
118 my $repo_exists = sub {
119 grep { $_->{name} eq $repo } @list_repos;
120 };
121 unless (&$repo_exists) {
122 $repos->create(\%create_opts);
123 until (&$repo_exists) {
124 sleep 5;
125 @list_repos = $repos->$list_method($org);
126 }
127 }
128 return "https://github.com/$org/$repo";
129 }
130  
131 sub _nuke_fork {
132 my ($self, $upstream) = @_;
133 $self->_assert_fork_has_parent($upstream);
134 my (undef, $repo) = _extract_repo($upstream);
135 my $user = $self->{_api}->user->show->{login};
136 $self->{_api}->repos->delete($user, $repo);
137 }
138  
139 sub _extract_repo {
140 $_[0] =~ m#^([^/]+)/(.+)(?:\.git)?$#;
141 ($1, $2);
142 }
143  
144 1;
145  
146 __END__