summaryrefslogtreecommitdiff
path: root/lib/API
diff options
context:
space:
mode:
Diffstat (limited to 'lib/API')
-rw-r--r--lib/API/GitForge/GitLab.pm14
-rw-r--r--lib/API/GitForge/Role/GitForge.pm23
2 files changed, 18 insertions, 19 deletions
diff --git a/lib/API/GitForge/GitLab.pm b/lib/API/GitForge/GitLab.pm
index 3f38bf7..1e620d8 100644
--- a/lib/API/GitForge/GitLab.pm
+++ b/lib/API/GitForge/GitLab.pm
@@ -113,13 +113,9 @@ sub _clean_config_fork {
sub _ensure_repo {
my ($self, $target) = @_;
my ($ns, $repo) = _extract_project_id($target);
-
- # first we are required to get the namespace id
+ return if $self->{_api}->project($target);
my $namespace = $self->{_api}->namespace($ns)
or croak "invalid project namespace $ns";
-
- # now create the project unless it already exists
- return if $self->{_api}->project($target);
$self->{_api}
->create_project({ name => $repo, namespace_id => $namespace->{id} });
}
@@ -132,6 +128,14 @@ sub _nuke_fork {
$self->{_api}->delete_project("$user/$repo");
}
+sub _ensure_fork_branch_unprotected {
+ my ($self, $upstream, $branch) = @_;
+ my (undef, $repo) = _extract_project_id($upstream);
+ my $user = $self->{_api}->current_user->{username};
+ return unless $self->{_api}->protected_branch("$user/$repo", $branch);
+ $self->{_api}->unprotect_branch("$user/$repo", $branch);
+}
+
sub _extract_project_id {
my $project = shift;
$project =~ s#(?:\.git)?/?$##;
diff --git a/lib/API/GitForge/Role/GitForge.pm b/lib/API/GitForge/Role/GitForge.pm
index bacc293..52760ff 100644
--- a/lib/API/GitForge/Role/GitForge.pm
+++ b/lib/API/GitForge/Role/GitForge.pm
@@ -26,7 +26,7 @@ Operations which one might wish to perform against any git forge. See
L<API::GitForge>.
In this documentation, C<example.com> should be replaced with the
-domain at which your GitForge is hosted, e.g. C<salsa.debian.org>.
+domain at which your git forge is hosted, e.g. C<salsa.debian.org>.
=cut
@@ -104,14 +104,13 @@ fork suitable for adding as a git remote.
sub clean_fork {
my $self = shift;
- my $fork_uri = $self->_ensure_fork(@_);
+ my $fork_uri = $self->_ensure_fork($_[0]);
my $temp = tempdir CLEANUP => 1;
my $git = Git::Wrapper->new($temp);
$git->init;
- $git->remote(qw(add fork), $fork_uri);
my @fork_branches
- = map { m#refs/heads/#; $' } $git->ls_remote(qw(--heads fork));
+ = map { m#refs/heads/#; $' } $git->ls_remote("--heads", $fork_uri);
return $fork_uri if grep /\Agitforge\z/, @fork_branches;
open my $fh, ">", catfile $temp, "README.md";
@@ -120,19 +119,15 @@ sub clean_fork {
$git->add("README.md");
$git->commit({ message => "Temporary fork for pull request(s)" });
- # TODO why does Git::Wrapper hang after pushing the branch to
- # GitLab? for now, just use system() to do the push ourselves
- system "git", "-C", $git->dir, "push", $fork_uri, "master:gitforge";
-
- $self->_clean_config_fork(@_);
-
- # TODO use API to unprotect all branches in the fork. we still
- # want to use git-push(1) to delete the branches, rather than
- # using the API for that, because that's maximally compatible
+ $git->push($fork_uri, "master:gitforge");
+ $self->_clean_config_fork($_[0]);
# assume that if we had to create the gitforge branch, we just
# created the fork, so can go ahead and nuke all branches there.
- # may fail if some branches are protected; that's okay.
+ if ($self->can("_ensure_fork_branch_unprotected")) {
+ $self->_ensure_fork_branch_unprotected($_[0], $_) for @fork_branches;
+ }
+ # may fail if we couldn't unprotect; that's okay
eval { $git->push($fork_uri, "--delete", @fork_branches) };
return $fork_uri;