Skip to content

Commit 4f111b8

Browse files
committed
Add remote mirror methods. Fixes #46.
1 parent 2cc5136 commit 4f111b8

File tree

9 files changed

+148
-18
lines changed

9 files changed

+148
-18
lines changed

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Revision history for Perl extension GitLab-API-v4.
22

33
{{$NEXT}}
44

5+
- Handle multiple cookies correctly when there is more than one.
6+
- Live tests now run against GitLab CE 13.9.6 (was 11.10.4).
7+
- Add the the remote_mirrors, create_remote_mirror, and
8+
edit_remote_mirror methods.
59
- Fixed incorrect license messaging in POD.
610

711
0.26 2021-01-30T07:10:57Z

META.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"HTTP::Tiny::Multipart" : "0.05",
5151
"IO::Prompter" : "0.004014",
5252
"JSON" : "2.59",
53+
"List::Util" : "0",
5354
"Log::Any" : "1.703",
5455
"Log::Any::Adapter" : "1.703",
5556
"Log::Any::Adapter::Screen" : "0.13",

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4519,6 +4519,43 @@ See [https://docs.gitlab.com/ce/api/releases/links.html](https://docs.gitlab.com
45194519
45204520
Sends a `DELETE` request to `projects/:project_id/releases/:tag_name/assets/links/:link_id` and returns the decoded response content.
45214521
4522+
## Remote Mirrors
4523+
4524+
See [https://docs.gitlab.com/ce/api/remote\_mirrors.html](https://docs.gitlab.com/ce/api/remote_mirrors.html).
4525+
4526+
- remote\_mirrors
4527+
4528+
```perl
4529+
my $mirrors = $api->remote_mirrors(
4530+
$project_id,
4531+
);
4532+
```
4533+
4534+
Sends a `GET` request to `projects/:project_id/remote_mirrors` and returns the decoded response content.
4535+
4536+
- create\_remote\_mirror
4537+
4538+
```perl
4539+
my $mirror = $api->create_remote_mirror(
4540+
$project_id,
4541+
\%params,
4542+
);
4543+
```
4544+
4545+
Sends a `POST` request to `projects/:project_id/remote_mirrors` and returns the decoded response content.
4546+
4547+
- edit\_remote\_mirror
4548+
4549+
```perl
4550+
my $mirror = $api->edit_remote_mirror(
4551+
$project_id,
4552+
$mirror_id,
4553+
\%params,
4554+
);
4555+
```
4556+
4557+
Sends a `PUT` request to `projects/:project_id/remote_mirrors/:mirror_id` and returns the decoded response content.
4558+
45224559
## Repositories
45234560
45244561
See [https://docs.gitlab.com/ce/api/repositories.html](https://docs.gitlab.com/ce/api/repositories.html).

author/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ sections:
150150
- release_links:
151151
head: Release Links
152152
doc_url: https://docs.gitlab.com/ce/api/releases/links.html
153+
- remote_mirrors:
154+
head: Remote Mirrors
155+
doc_url: https://docs.gitlab.com/ce/api/remote_mirrors.html
153156
- repositories:
154157
head: Repositories
155158
doc_url: https://docs.gitlab.com/ce/api/repositories.html

author/sections/remote_mirrors.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- remote_mirrors: mirrors = GET projects/:project_id/remote_mirrors
3+
- create_remote_mirror: mirror = POST projects/:project_id/remote_mirrors?
4+
- edit_remote_mirror: mirror = PUT projects/:project_id/remote_mirrors/:mirror_id?
5+

author/setup-test-container

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sleep 5
77
docker run -d --rm \
88
-p 80:80 \
99
--name test-gitlab-api-v4 \
10-
gitlab/gitlab-ce:11.10.4-ce.0
10+
gitlab/gitlab-ce:13.9.6-ce.0
1111

1212
echo 1. Open you browser to: http://localhost \(it may take several minutes for it to respond\)
1313
echo 2. And set a password for the root user.

cpanfile

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires 'Types::Standard' => '1.002001';
88
requires 'Types::Common::String' => '1.002001';
99
requires 'Types::Common::Numeric' => '1.002001';
1010
requires 'Log::Any' => '1.703';
11-
requires 'Carp' => 0;
11+
requires 'Carp';
1212
requires 'JSON' => '2.59';
1313

1414
# Used by GitLab::API::v4::RESTClient.
@@ -17,21 +17,23 @@ requires 'HTTP::Tiny::Multipart' => '0.05';
1717
requires 'URI' => '1.62';
1818
requires 'URI::Escape' => '1.72';
1919

20+
# Used by GitLab::API::v4::WWWClient.
21+
requires 'List::Util';
22+
2023
# Used by GitLab::API::v4::Constants.
2124
requires 'Const::Fast' => '0.014';
22-
requires 'Exporter' => 0;
25+
requires 'Exporter';
2326

2427
# Used by gitlab-api-v4 and/or GitLab::API::v4::Config.
2528
requires 'Try::Tiny' => '0.28';
26-
requires 'Getopt::Long' => 0;
27-
requires 'Pod::Usage' => 0;
29+
requires 'Getopt::Long';
30+
requires 'Pod::Usage';
2831
requires 'Log::Any::Adapter' => '1.703';
2932
requires 'Log::Any::Adapter::Screen' => '0.13';
3033
requires 'Path::Tiny' => '0.079';
3134
requires 'IO::Prompter' => '0.004014';
3235

33-
on test => sub {
34-
requires 'Test2::V0' => '0.000094';
35-
requires 'Log::Any::Adapter::TAP' => '0.003003';
36-
requires 'MIME::Base64' => '3.15';
37-
};
36+
test_requires 'Test2::V0' => '0.000094';
37+
test_requires 'Log::Any::Adapter::TAP' => '0.003003';
38+
test_requires 'MIME::Base64' => '3.15';
39+

lib/GitLab/API/v4.pm

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8825,6 +8825,78 @@ sub delete_release_link {
88258825

88268826
=back
88278827
8828+
=head2 Remote Mirrors
8829+
8830+
See L<https://docs.gitlab.com/ce/api/remote_mirrors.html>.
8831+
8832+
=over
8833+
8834+
=item remote_mirrors
8835+
8836+
my $mirrors = $api->remote_mirrors(
8837+
$project_id,
8838+
);
8839+
8840+
Sends a C<GET> request to C<projects/:project_id/remote_mirrors> and returns the decoded response content.
8841+
8842+
=cut
8843+
8844+
sub remote_mirrors {
8845+
my $self = shift;
8846+
croak 'remote_mirrors must be called with 1 arguments' if @_ != 1;
8847+
croak 'The #1 argument ($project_id) to remote_mirrors must be a scalar' if ref($_[0]) or (!defined $_[0]);
8848+
my $options = {};
8849+
return $self->_call_rest_client( 'GET', 'projects/:project_id/remote_mirrors', [@_], $options );
8850+
}
8851+
8852+
=item create_remote_mirror
8853+
8854+
my $mirror = $api->create_remote_mirror(
8855+
$project_id,
8856+
\%params,
8857+
);
8858+
8859+
Sends a C<POST> request to C<projects/:project_id/remote_mirrors> and returns the decoded response content.
8860+
8861+
=cut
8862+
8863+
sub create_remote_mirror {
8864+
my $self = shift;
8865+
croak 'create_remote_mirror must be called with 1 to 2 arguments' if @_ < 1 or @_ > 2;
8866+
croak 'The #1 argument ($project_id) to create_remote_mirror must be a scalar' if ref($_[0]) or (!defined $_[0]);
8867+
croak 'The last argument (\%params) to create_remote_mirror must be a hash ref' if defined($_[1]) and ref($_[1]) ne 'HASH';
8868+
my $params = (@_ == 2) ? pop() : undef;
8869+
my $options = {};
8870+
$options->{content} = $params if defined $params;
8871+
return $self->_call_rest_client( 'POST', 'projects/:project_id/remote_mirrors', [@_], $options );
8872+
}
8873+
8874+
=item edit_remote_mirror
8875+
8876+
my $mirror = $api->edit_remote_mirror(
8877+
$project_id,
8878+
$mirror_id,
8879+
\%params,
8880+
);
8881+
8882+
Sends a C<PUT> request to C<projects/:project_id/remote_mirrors/:mirror_id> and returns the decoded response content.
8883+
8884+
=cut
8885+
8886+
sub edit_remote_mirror {
8887+
my $self = shift;
8888+
croak 'edit_remote_mirror must be called with 2 to 3 arguments' if @_ < 2 or @_ > 3;
8889+
croak 'The #1 argument ($project_id) to edit_remote_mirror must be a scalar' if ref($_[0]) or (!defined $_[0]);
8890+
croak 'The #2 argument ($mirror_id) to edit_remote_mirror must be a scalar' if ref($_[1]) or (!defined $_[1]);
8891+
croak 'The last argument (\%params) to edit_remote_mirror must be a hash ref' if defined($_[2]) and ref($_[2]) ne 'HASH';
8892+
my $params = (@_ == 3) ? pop() : undef;
8893+
my $options = {};
8894+
$options->{content} = $params if defined $params;
8895+
return $self->_call_rest_client( 'PUT', 'projects/:project_id/remote_mirrors/:mirror_id', [@_], $options );
8896+
}
8897+
8898+
=back
8899+
88288900
=head2 Repositories
88298901
88308902
See L<https://docs.gitlab.com/ce/api/repositories.html>.

lib/GitLab/API/v4/WWWClient.pm

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This class makes it possible to interact with the GitLab web site.
2727

2828
use Carp qw( croak );
2929
use HTTP::Tiny;
30+
use List::Util qw( first );
3031
use Types::Common::String qw( NonEmptySimpleStr );
3132

3233
use Moo;
@@ -96,10 +97,13 @@ sub sign_in {
9697
m{name="authenticity_token" value="(.+?)"}
9798
)[0];
9899

99-
my $first_session = (
100-
$load_res->{headers}->{'set-cookie'} =~
101-
m{_gitlab_session=(.+?);}
102-
)[0];
100+
101+
my ($first_session) = do{
102+
my $set_cookie_headers = $load_res->{headers}->{ 'set-cookie' };
103+
$set_cookie_headers = [ $set_cookie_headers ] if !ref $set_cookie_headers;
104+
my $value = first { $_ =~ m{^_gitlab_session=(.*)} } @$set_cookie_headers;
105+
$value =~ s{^_gitlab_session=}{}r;
106+
};
103107

104108
my $submit_res = $tiny->post_form(
105109
$sign_in_url,
@@ -121,10 +125,12 @@ sub sign_in {
121125

122126
_croak_res( 'post', $sign_in_url, $submit_res );
123127

124-
my $second_session = (
125-
$submit_res->{headers}->{'set-cookie'} =~
126-
m{_gitlab_session=(.+?);}
127-
)[0];
128+
my ($second_session) = do{
129+
my $set_cookie_headers = $submit_res->{headers}->{ 'set-cookie' };
130+
$set_cookie_headers = [ $set_cookie_headers ] if !ref $set_cookie_headers;
131+
my $value = first { $_ =~ m{^_gitlab_session=(.*)} } @$set_cookie_headers;
132+
$value =~ s{^_gitlab_session=}{}r;
133+
};
128134

129135
my $home_res = $tiny->get(
130136
$base_url,

0 commit comments

Comments
 (0)