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 --- Changes | 2 ++ lib/API/GitForge/GitLab.pm | 6 +----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Changes b/Changes index 5f6e9bf..a51c982 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for API::GitForge {{$NEXT}} + - API::GitForge::GitLab::_ensure_repo(): avoid an unnecessary API + call. 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 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 89c554cf3eb44ef1a002fbc077250e26fb2a0aa3 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 10 Mar 2020 09:02:42 -0700 Subject: pass single argument to methods which take a single argument Signed-off-by: Sean Whitton --- lib/API/GitForge/Role/GitForge.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/API/GitForge/Role/GitForge.pm b/lib/API/GitForge/Role/GitForge.pm index bacc293..78d3498 100644 --- a/lib/API/GitForge/Role/GitForge.pm +++ b/lib/API/GitForge/Role/GitForge.pm @@ -104,7 +104,7 @@ 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); @@ -124,7 +124,7 @@ sub clean_fork { # 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(@_); + $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 -- 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(-) 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 From 16b5e5d0592ff442716bc5702fde94d2c02a4971 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 10 Mar 2020 09:13:57 -0700 Subject: push the gitforge branch using Git::Wrapper again. Signed-off-by: Sean Whitton --- Changes | 4 ++++ lib/API/GitForge/Role/GitForge.pm | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Changes b/Changes index ee813bf..d5d3c2a 100644 --- a/Changes +++ b/Changes @@ -6,6 +6,10 @@ Revision history for API::GitForge - API::GitForge::Role::GitForge::clean_repo(): try to unprotect fork branches before deleting them, if GitForge API library supports doing this. + - API::GitForge::Role::GitForge::clean_repo(): push the gitforge + branch using Git::Wrapper again. + During early testing, this seemed to hang after pushing to + GitLab installations, but it seems to work well now. 0.002 2020-03-09 12:17:47-07:00 America/Phoenix - Implement API::GitForge::GitLab::_ensure_repo(). diff --git a/lib/API/GitForge/Role/GitForge.pm b/lib/API/GitForge/Role/GitForge.pm index c3459bf..3742553 100644 --- a/lib/API/GitForge/Role/GitForge.pm +++ b/lib/API/GitForge/Role/GitForge.pm @@ -120,10 +120,7 @@ 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"; - + $git->push($fork_uri, "master:gitforge"); $self->_clean_config_fork($_[0]); # assume that if we had to create the gitforge branch, we just -- cgit v1.2.3 From 797404ca55365a028b4957d7d04136ebe3e2fd24 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 10 Mar 2020 09:15:56 -0700 Subject: drop spurious camel case Signed-off-by: Sean Whitton --- lib/API/GitForge/Role/GitForge.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/API/GitForge/Role/GitForge.pm b/lib/API/GitForge/Role/GitForge.pm index 3742553..2f282ae 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. In this documentation, C should be replaced with the -domain at which your GitForge is hosted, e.g. C. +domain at which your git forge is hosted, e.g. C. =cut -- cgit v1.2.3 From bf07e6197761a81a13d1aeb292f7026ef812555b Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 10 Mar 2020 09:20:52 -0700 Subject: refer to the fork consistently with $fork_uri Signed-off-by: Sean Whitton --- lib/API/GitForge/Role/GitForge.pm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/API/GitForge/Role/GitForge.pm b/lib/API/GitForge/Role/GitForge.pm index 2f282ae..52760ff 100644 --- a/lib/API/GitForge/Role/GitForge.pm +++ b/lib/API/GitForge/Role/GitForge.pm @@ -109,9 +109,8 @@ sub clean_fork { 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"; -- cgit v1.2.3 From 98bb20d24254a99374b53ee910e6828db2238a06 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 10 Mar 2020 09:21:18 -0700 Subject: bump version Signed-off-by: Sean Whitton --- dist.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist.ini b/dist.ini index cd333e1..5ae0ca9 100644 --- a/dist.ini +++ b/dist.ini @@ -4,7 +4,7 @@ license = GPL_3 copyright_holder = Sean Whitton copyright_year = 2017, 2020 -version = 0.002 +version = 0.003 [PkgVersion] [PodWeaver] -- cgit v1.2.3 From ca1af5ff75521b8f2071642335858f464a7f221d Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 10 Mar 2020 09:21:35 -0700 Subject: v0.003 - 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. - API::GitForge::Role::GitForge::clean_repo(): push the gitforge branch using Git::Wrapper again. During early testing, this seemed to hang after pushing to GitLab installations, but it seems to work well now. Signed-off-by: Sean Whitton --- Changes | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changes b/Changes index d5d3c2a..cc506c8 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for API::GitForge {{$NEXT}} + +0.003 2020-03-10 09:21:28-07:00 America/Phoenix - API::GitForge::GitLab::_ensure_repo(): avoid an unnecessary API call. - API::GitForge::Role::GitForge::clean_repo(): try to unprotect -- cgit v1.2.3