API/GitForge/GitLab.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 113 CC 5 SCAR 13.0
CC
5
Cov
0%
_make_api
6.0
_ensure_fork
6.0
_ensure_repo
6.0
CRAP 3.7
1 package API::GitForge::GitLab;
2 # ABSTRACT: common git forge operations using the GitLab API
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::GitLab::VERSION = '0.003';
19  
20 use 5.028;
21 use strict;
22 use warnings;
23  
24 use Role::Tiny::With;
25  
26 use Carp;
27 use GitLab::API::v4;
28  
29 with "API::GitForge::Role::GitForge";
30  
31 sub _make_api {
32 my $self = shift;
33 my %opts = (url => "https://" . $self->{_domain} . "/api/v4");
34 $opts{private_token} = $self->{_access_token}
35 if exists $self->{_access_token};
36 $self->{_api} = GitLab::API::v4->new(%opts);
37 }
38  
39 sub _ensure_fork {
40 my ($self, $upstream) = @_;
41 my ($path, $repo) = _extract_project_id($upstream);
42  
43 my $user = $self->{_api}->current_user->{username};
44 my @user_repos;
45 my $update_user_repos = sub {
46 @user_repos
47 = @{ $self->{_api}->projects({ search => "$user/$repo" }) };
48 };
49 my $repo_exists = sub {
50 grep { $_->{path_with_namespace} eq "$user/$repo" } @user_repos;
51 };
52 &$update_user_repos;
53 if (&$repo_exists) {
54 $self->_assert_fork_has_parent($upstream);
55 } else {
56 $self->{_api}->fork_project("$path/$repo");
57 until (&$repo_exists) {
58 sleep 5;
59 &$update_user_repos;
60 }
61 }
62 return "https://" . $self->{_domain} . "/$user/$repo.git";
63 }
64  
65 sub _assert_fork_has_parent {
66 my ($self, $upstream) = @_;
67 my ($path, $repo) = _extract_project_id($upstream);
68 my $user = $self->{_api}->current_user->{username};
69 my $fork = $self->{_api}->project("$user/$repo");
70  
71 $fork->{forked_from_project}{path_with_namespace} eq $path . "/" . $repo
72 or croak
73 "$user/$repo does not have parent $upstream; don't know what to do";
74 }
75  
76 sub _clean_config_repo {
77 my ($self, $upstream) = @_;
78 my (undef, $repo) = _extract_project_id($upstream);
79 my $user = $self->{_api}->current_user->{username};
80  
81 $self->{_api}->edit_project(
82 "$user/$repo",
83 {
84 issues_access_level => "disabled",
85 merge_requests_access_level => "disabled",
86 });
87 }
88  
89 sub _clean_config_fork {
90 my ($self, $upstream) = @_;
91 my (undef, $repo) = _extract_project_id($upstream);
92 my $user = $self->{_api}->current_user->{username};
93  
94 $self->{_api}->edit_project(
95 "$user/$repo",
96 {
97 default_branch => "gitforge",
98 description => "Temporary fork for merge request(s)",
99 issues_access_level => "disabled",
100 # merge requests have to be enabled in the fork in order
101 # to submit merge requests to the upstream repo from which
102 # we forked, it seems
103 merge_requests_access_level => "enabled",
104 });
105 }
106  
107 sub _ensure_repo {
108 my ($self, $target) = @_;
109 my ($ns, $repo) = _extract_project_id($target);
110 return if $self->{_api}->project($target);
111 my $namespace = $self->{_api}->namespace($ns)
112 or croak "invalid project namespace $ns";
113 $self->{_api}
114 ->create_project({ name => $repo, namespace_id => $namespace->{id} });
115 }
116  
117 sub _nuke_fork {
118 my ($self, $upstream) = @_;
119 $self->_assert_fork_has_parent($upstream);
120 my (undef, $repo) = _extract_project_id($upstream);
121 my $user = $self->{_api}->current_user->{username};
122 $self->{_api}->delete_project("$user/$repo");
123 }
124  
125 sub _ensure_fork_branch_unprotected {
126 my ($self, $upstream, $branch) = @_;
127 my (undef, $repo) = _extract_project_id($upstream);
128 my $user = $self->{_api}->current_user->{username};
129 return unless $self->{_api}->protected_branch("$user/$repo", $branch);
130 $self->{_api}->unprotect_branch("$user/$repo", $branch);
131 }
132  
133 sub _extract_project_id {
134 my $project = shift;
135 $project =~ s#(?:\.git)?/?$##;
136 $project =~ m#/([^/]+)$#;
137 ($`, $1);
138 }
139  
140 1;
141  
142 __END__