diff options
author | Aran Clary Deltac <bluefeet@gmail.com> | 2021-04-14 08:23:45 -0700 |
---|---|---|
committer | Aran Clary Deltac <bluefeet@gmail.com> | 2021-04-14 08:23:45 -0700 |
commit | 4f111b85b9557037e8a910a70856f2a9faa2b062 (patch) | |
tree | 846f77bb93413c76476f48e32c5c5a2d5326fca9 | |
parent | 2cc51360db4f9442d4ec5c5ce342554ca23d0322 (diff) |
Add remote mirror methods. Fixes #46.
-rw-r--r-- | Changes | 4 | ||||
-rw-r--r-- | META.json | 1 | ||||
-rw-r--r-- | README.md | 37 | ||||
-rw-r--r-- | author/config.yml | 3 | ||||
-rw-r--r-- | author/sections/remote_mirrors.yml | 5 | ||||
-rwxr-xr-x | author/setup-test-container | 2 | ||||
-rw-r--r-- | cpanfile | 20 | ||||
-rw-r--r-- | lib/GitLab/API/v4.pm | 72 | ||||
-rw-r--r-- | lib/GitLab/API/v4/WWWClient.pm | 22 |
9 files changed, 148 insertions, 18 deletions
@@ -2,6 +2,10 @@ Revision history for Perl extension GitLab-API-v4. {{$NEXT}} + - Handle multiple cookies correctly when there is more than one. + - Live tests now run against GitLab CE 13.9.6 (was 11.10.4). + - Add the the remote_mirrors, create_remote_mirror, and + edit_remote_mirror methods. - Fixed incorrect license messaging in POD. 0.26 2021-01-30T07:10:57Z @@ -50,6 +50,7 @@ "HTTP::Tiny::Multipart" : "0.05", "IO::Prompter" : "0.004014", "JSON" : "2.59", + "List::Util" : "0", "Log::Any" : "1.703", "Log::Any::Adapter" : "1.703", "Log::Any::Adapter::Screen" : "0.13", @@ -4519,6 +4519,43 @@ See [https://docs.gitlab.com/ce/api/releases/links.html](https://docs.gitlab.com Sends a `DELETE` request to `projects/:project_id/releases/:tag_name/assets/links/:link_id` and returns the decoded response content. +## Remote Mirrors + +See [https://docs.gitlab.com/ce/api/remote\_mirrors.html](https://docs.gitlab.com/ce/api/remote_mirrors.html). + +- remote\_mirrors + + ```perl + my $mirrors = $api->remote_mirrors( + $project_id, + ); + ``` + + Sends a `GET` request to `projects/:project_id/remote_mirrors` and returns the decoded response content. + +- create\_remote\_mirror + + ```perl + my $mirror = $api->create_remote_mirror( + $project_id, + \%params, + ); + ``` + + Sends a `POST` request to `projects/:project_id/remote_mirrors` and returns the decoded response content. + +- edit\_remote\_mirror + + ```perl + my $mirror = $api->edit_remote_mirror( + $project_id, + $mirror_id, + \%params, + ); + ``` + + Sends a `PUT` request to `projects/:project_id/remote_mirrors/:mirror_id` and returns the decoded response content. + ## Repositories See [https://docs.gitlab.com/ce/api/repositories.html](https://docs.gitlab.com/ce/api/repositories.html). diff --git a/author/config.yml b/author/config.yml index 8cd6cbf..ece1689 100644 --- a/author/config.yml +++ b/author/config.yml @@ -150,6 +150,9 @@ sections: - release_links: head: Release Links doc_url: https://docs.gitlab.com/ce/api/releases/links.html +- remote_mirrors: + head: Remote Mirrors + doc_url: https://docs.gitlab.com/ce/api/remote_mirrors.html - repositories: head: Repositories doc_url: https://docs.gitlab.com/ce/api/repositories.html diff --git a/author/sections/remote_mirrors.yml b/author/sections/remote_mirrors.yml new file mode 100644 index 0000000..c1a9426 --- /dev/null +++ b/author/sections/remote_mirrors.yml @@ -0,0 +1,5 @@ +--- +- remote_mirrors: mirrors = GET projects/:project_id/remote_mirrors +- create_remote_mirror: mirror = POST projects/:project_id/remote_mirrors? +- edit_remote_mirror: mirror = PUT projects/:project_id/remote_mirrors/:mirror_id? + diff --git a/author/setup-test-container b/author/setup-test-container index fe2172a..8f6500f 100755 --- a/author/setup-test-container +++ b/author/setup-test-container @@ -7,7 +7,7 @@ sleep 5 docker run -d --rm \ -p 80:80 \ --name test-gitlab-api-v4 \ - gitlab/gitlab-ce:11.10.4-ce.0 + gitlab/gitlab-ce:13.9.6-ce.0 echo 1. Open you browser to: http://localhost \(it may take several minutes for it to respond\) echo 2. And set a password for the root user. @@ -8,7 +8,7 @@ requires 'Types::Standard' => '1.002001'; requires 'Types::Common::String' => '1.002001'; requires 'Types::Common::Numeric' => '1.002001'; requires 'Log::Any' => '1.703'; -requires 'Carp' => 0; +requires 'Carp'; requires 'JSON' => '2.59'; # Used by GitLab::API::v4::RESTClient. @@ -17,21 +17,23 @@ requires 'HTTP::Tiny::Multipart' => '0.05'; requires 'URI' => '1.62'; requires 'URI::Escape' => '1.72'; +# Used by GitLab::API::v4::WWWClient. +requires 'List::Util'; + # Used by GitLab::API::v4::Constants. requires 'Const::Fast' => '0.014'; -requires 'Exporter' => 0; +requires 'Exporter'; # Used by gitlab-api-v4 and/or GitLab::API::v4::Config. requires 'Try::Tiny' => '0.28'; -requires 'Getopt::Long' => 0; -requires 'Pod::Usage' => 0; +requires 'Getopt::Long'; +requires 'Pod::Usage'; requires 'Log::Any::Adapter' => '1.703'; requires 'Log::Any::Adapter::Screen' => '0.13'; requires 'Path::Tiny' => '0.079'; requires 'IO::Prompter' => '0.004014'; -on test => sub { - requires 'Test2::V0' => '0.000094'; - requires 'Log::Any::Adapter::TAP' => '0.003003'; - requires 'MIME::Base64' => '3.15'; -}; +test_requires 'Test2::V0' => '0.000094'; +test_requires 'Log::Any::Adapter::TAP' => '0.003003'; +test_requires 'MIME::Base64' => '3.15'; + diff --git a/lib/GitLab/API/v4.pm b/lib/GitLab/API/v4.pm index 6546c16..87ba571 100644 --- a/lib/GitLab/API/v4.pm +++ b/lib/GitLab/API/v4.pm @@ -8825,6 +8825,78 @@ sub delete_release_link { =back +=head2 Remote Mirrors + +See L<https://docs.gitlab.com/ce/api/remote_mirrors.html>. + +=over + +=item remote_mirrors + + my $mirrors = $api->remote_mirrors( + $project_id, + ); + +Sends a C<GET> request to C<projects/:project_id/remote_mirrors> and returns the decoded response content. + +=cut + +sub remote_mirrors { + my $self = shift; + croak 'remote_mirrors must be called with 1 arguments' if @_ != 1; + croak 'The #1 argument ($project_id) to remote_mirrors must be a scalar' if ref($_[0]) or (!defined $_[0]); + my $options = {}; + return $self->_call_rest_client( 'GET', 'projects/:project_id/remote_mirrors', [@_], $options ); +} + +=item create_remote_mirror + + my $mirror = $api->create_remote_mirror( + $project_id, + \%params, + ); + +Sends a C<POST> request to C<projects/:project_id/remote_mirrors> and returns the decoded response content. + +=cut + +sub create_remote_mirror { + my $self = shift; + croak 'create_remote_mirror must be called with 1 to 2 arguments' if @_ < 1 or @_ > 2; + croak 'The #1 argument ($project_id) to create_remote_mirror must be a scalar' if ref($_[0]) or (!defined $_[0]); + croak 'The last argument (\%params) to create_remote_mirror must be a hash ref' if defined($_[1]) and ref($_[1]) ne 'HASH'; + my $params = (@_ == 2) ? pop() : undef; + my $options = {}; + $options->{content} = $params if defined $params; + return $self->_call_rest_client( 'POST', 'projects/:project_id/remote_mirrors', [@_], $options ); +} + +=item edit_remote_mirror + + my $mirror = $api->edit_remote_mirror( + $project_id, + $mirror_id, + \%params, + ); + +Sends a C<PUT> request to C<projects/:project_id/remote_mirrors/:mirror_id> and returns the decoded response content. + +=cut + +sub edit_remote_mirror { + my $self = shift; + croak 'edit_remote_mirror must be called with 2 to 3 arguments' if @_ < 2 or @_ > 3; + croak 'The #1 argument ($project_id) to edit_remote_mirror must be a scalar' if ref($_[0]) or (!defined $_[0]); + croak 'The #2 argument ($mirror_id) to edit_remote_mirror must be a scalar' if ref($_[1]) or (!defined $_[1]); + croak 'The last argument (\%params) to edit_remote_mirror must be a hash ref' if defined($_[2]) and ref($_[2]) ne 'HASH'; + my $params = (@_ == 3) ? pop() : undef; + my $options = {}; + $options->{content} = $params if defined $params; + return $self->_call_rest_client( 'PUT', 'projects/:project_id/remote_mirrors/:mirror_id', [@_], $options ); +} + +=back + =head2 Repositories See L<https://docs.gitlab.com/ce/api/repositories.html>. diff --git a/lib/GitLab/API/v4/WWWClient.pm b/lib/GitLab/API/v4/WWWClient.pm index 5deb3c2..8856e1f 100644 --- a/lib/GitLab/API/v4/WWWClient.pm +++ b/lib/GitLab/API/v4/WWWClient.pm @@ -27,6 +27,7 @@ This class makes it possible to interact with the GitLab web site. use Carp qw( croak ); use HTTP::Tiny; +use List::Util qw( first ); use Types::Common::String qw( NonEmptySimpleStr ); use Moo; @@ -96,10 +97,13 @@ sub sign_in { m{name="authenticity_token" value="(.+?)"} )[0]; - my $first_session = ( - $load_res->{headers}->{'set-cookie'} =~ - m{_gitlab_session=(.+?);} - )[0]; + + my ($first_session) = do{ + my $set_cookie_headers = $load_res->{headers}->{ 'set-cookie' }; + $set_cookie_headers = [ $set_cookie_headers ] if !ref $set_cookie_headers; + my $value = first { $_ =~ m{^_gitlab_session=(.*)} } @$set_cookie_headers; + $value =~ s{^_gitlab_session=}{}r; + }; my $submit_res = $tiny->post_form( $sign_in_url, @@ -121,10 +125,12 @@ sub sign_in { _croak_res( 'post', $sign_in_url, $submit_res ); - my $second_session = ( - $submit_res->{headers}->{'set-cookie'} =~ - m{_gitlab_session=(.+?);} - )[0]; + my ($second_session) = do{ + my $set_cookie_headers = $submit_res->{headers}->{ 'set-cookie' }; + $set_cookie_headers = [ $set_cookie_headers ] if !ref $set_cookie_headers; + my $value = first { $_ =~ m{^_gitlab_session=(.*)} } @$set_cookie_headers; + $value =~ s{^_gitlab_session=}{}r; + }; my $home_res = $tiny->get( $base_url, |