From 0ae4bf932c0808123d9e8149f5c7fedd10f6c2b7 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Mon, 9 Mar 2020 13:24:37 -0700 Subject: don't look up namespace if project already exists Signed-off-by: Sean Whitton --- lib/API/GitForge/GitLab.pm | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib/API/GitForge/GitLab.pm') diff --git a/lib/API/GitForge/GitLab.pm b/lib/API/GitForge/GitLab.pm index 3f38bf7..2091cfc 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} }); } -- cgit v1.2.3 From 68c29d68184b93fda6fa8ca2741114859693aa0e Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 10 Mar 2020 09:05:43 -0700 Subject: try to unprotect fork branches before deleting them Signed-off-by: Sean Whitton --- Changes | 3 +++ lib/API/GitForge/GitLab.pm | 8 ++++++++ lib/API/GitForge/Role/GitForge.pm | 9 ++++----- 3 files changed, 15 insertions(+), 5 deletions(-) (limited to 'lib/API/GitForge/GitLab.pm') diff --git a/Changes b/Changes index a51c982..ee813bf 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,9 @@ Revision history for API::GitForge {{$NEXT}} - API::GitForge::GitLab::_ensure_repo(): avoid an unnecessary API call. + - API::GitForge::Role::GitForge::clean_repo(): try to unprotect + fork branches before deleting them, if GitForge API library + supports doing this. 0.002 2020-03-09 12:17:47-07:00 America/Phoenix - Implement API::GitForge::GitLab::_ensure_repo(). diff --git a/lib/API/GitForge/GitLab.pm b/lib/API/GitForge/GitLab.pm index 2091cfc..1e620d8 100644 --- a/lib/API/GitForge/GitLab.pm +++ b/lib/API/GitForge/GitLab.pm @@ -128,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 78d3498..c3459bf 100644 --- a/lib/API/GitForge/Role/GitForge.pm +++ b/lib/API/GitForge/Role/GitForge.pm @@ -126,13 +126,12 @@ sub clean_fork { $self->_clean_config_fork($_[0]); - # 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 - # 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; -- cgit v1.2.3