| 1 | package API::GitForge::Role::GitForge; | ||
| 2 | # ABSTRACT: role implementing generic git forge operations | ||
| 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::Role::GitForge::VERSION = '0.005'; | ||
| 19 | |||
| 20 | use 5.028; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use Role::Tiny; | ||
| 25 | |||
| 26 | use Carp; | ||
| 27 | use File::Temp qw(tempdir); | ||
| 28 | use Git::Wrapper; | ||
| 29 | use File::Spec::Functions qw(catfile); | ||
| 30 | |||
| 31 | |||
| 32 | sub new { | ||
| 33 | my ($class, %opts) = @_; | ||
| 34 | croak "need domain!" unless exists $opts{domain}; | ||
| 35 | |||
| 36 | my %attrs = (_domain => $opts{domain}); | ||
| 37 | $attrs{_access_token} = $opts{access_token} if exists $opts{access_token}; | ||
| 38 | my $self = bless \%attrs => $class; | ||
| 39 | |||
| 40 | $self->_make_api; | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub ensure_repo { shift->_create_repo(@_) } | ||
| 47 | |||
| 48 | |||
| 49 | sub clean_repo { | ||
| 50 | my ($self, $repo) = @_; | ||
| 51 | $self->_ensure_repo($repo); | ||
| 52 | $self->_clean_config_repo($repo); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub ensure_fork { shift->_ensure_fork(@_) } | ||
| 57 | |||
| 58 | |||
| 59 | sub clean_fork { | ||
| 60 | my $self = shift; | ||
| 61 | my $fork_uri = $self->_ensure_fork($_[0]); | ||
| 62 | |||
| 63 | my $temp = tempdir CLEANUP => 1; | ||
| 64 | my $git = Git::Wrapper->new($temp); | ||
| 65 | $git->init; | ||
| 66 | my @fork_branches | ||
| 67 | = map { m#refs/heads/#; $' } $git->ls_remote("--heads", $fork_uri); | ||
| 68 | return $fork_uri if grep /\Agitforge\z/, @fork_branches; | ||
| 69 | |||
| 70 | open my $fh, ">", catfile $temp, "README.md"; | ||
| 71 | say $fh "This repository exists only in order to submit pull request(s)."; | ||
| 72 | close $fh; | ||
| 73 | $git->add("README.md"); | ||
| 74 | $git->commit({ message => "Temporary fork for pull request(s)" }); | ||
| 75 | |||
| 76 | # We should be able to just say | ||
| 77 | # | ||
| 78 | # $git->push($fork_uri, "master:gitforge"); | ||
| 79 | # | ||
| 80 | # but that hangs indefinitely when pushing to (at least) Debian's | ||
| 81 | # GitLab instance. So just bypass Git::Wrapper and do the push | ||
| 82 | # ourselves for now | ||
| 83 | system qw(git -C), $git->dir, "push", $fork_uri, "master:gitforge" | ||
| 84 | or croak "failed to push the gitforge branch to $fork_uri"; | ||
| 85 | |||
| 86 | $self->_clean_config_fork($_[0]); | ||
| 87 | |||
| 88 | # assume that if we had to create the gitforge branch, we just | ||
| 89 | # created the fork, so can go ahead and nuke all branches there. | ||
| 90 | if ($self->can("_ensure_fork_branch_unprotected")) { | ||
| 91 | $self->_ensure_fork_branch_unprotected($_[0], $_) for @fork_branches; | ||
| 92 | } | ||
| 93 | # may fail if we couldn't unprotect; that's okay | ||
| 94 | eval { $git->push($fork_uri, "--delete", @fork_branches) }; | ||
| 95 | |||
| 96 | return $fork_uri; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub nuke_fork { shift->_nuke_fork(@_) } | ||
| 101 | |||
| 102 | |||
| 103 | sub clean_config_repo { shift->_clean_config_repo(@_) } | ||
| 104 | |||
| 105 | |||
| 106 | sub clean_config_fork { shift->_clean_config_fork(@_) } | ||
| 107 | |||
| 108 | requires | ||
| 109 | qw<_make_api _ensure_repo _clean_config_repo _clean_config_fork | ||
| 110 | _ensure_fork _nuke_fork>; | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | __END__ |