| 1 |
#!/usr/bin/env perl |
| 2 |
use strictures 2; |
| 3 |
|
| 4 |
use GitLab::API::v4::Config; |
| 5 |
use GitLab::API::v4::Constants qw( :all ); |
| 6 |
use GitLab::API::v4; |
| 7 |
use JSON::MaybeXS; |
| 8 |
use Log::Any qw( $log ); |
| 9 |
use Log::Any::Adapter::Screen; |
| 10 |
use Log::Any::Adapter; |
| 11 |
use Pod::Usage qw( pod2usage ); |
| 12 |
use Try::Tiny; |
| 13 |
|
| 14 |
if (!@ARGV) { |
| 15 |
print "USAGE: gitlab-api-v4 help\n"; |
| 16 |
exit 0; |
| 17 |
} |
| 18 |
|
| 19 |
my $config = GitLab::API::v4::Config->new(); |
| 20 |
|
| 21 |
$config->get_options( |
| 22 |
'a|all' => \my $all, |
| 23 |
'p|pretty' => \my $pretty, |
| 24 |
'c|canonical' => \my $canonical, |
| 25 |
|
| 26 |
'h|help' => \my $help, |
| 27 |
'v|verbose' => \my $verbose, |
| 28 |
'q|quiet' => \my $quiet, |
| 29 |
); |
| 30 |
|
| 31 |
if ($help or (@ARGV and $ARGV[0] eq 'help')) { |
| 32 |
pod2usage( -verbose => 2 ); |
| 33 |
exit 0; |
| 34 |
} |
| 35 |
|
| 36 |
if (@ARGV and $ARGV[0] eq 'configure') { |
| 37 |
$config->configure(); |
| 38 |
exit 0; |
| 39 |
} |
| 40 |
|
| 41 |
my $min_level = 'info'; |
| 42 |
$min_level = 'trace' if $verbose; |
| 43 |
$min_level = 'error' if $quiet; |
| 44 |
|
| 45 |
Log::Any::Adapter->set( |
| 46 |
'Screen', |
| 47 |
min_level => $min_level, |
| 48 |
stderr => 1, |
| 49 |
); |
| 50 |
|
| 51 |
my $method = shift @ARGV; |
| 52 |
die "ERROR: No API method specified.\n" if !$method; |
| 53 |
my $orig_method = $method; |
| 54 |
$method =~ s{-}{_}g; |
| 55 |
|
| 56 |
die "ERROR: Unknown API method \"$orig_method\".\n" |
| 57 |
if !GitLab::API::v4->can( $method ); |
| 58 |
|
| 59 |
my @args; |
| 60 |
while (@ARGV and $ARGV[0] !~ m{:}) { |
| 61 |
my $arg = shift @ARGV; |
| 62 |
next if $arg eq '--'; |
| 63 |
|
| 64 |
push @args, $arg; |
| 65 |
} |
| 66 |
|
| 67 |
my $aliases = { |
| 68 |
access_level => { |
| 69 |
guest => $GITLAB_ACCESS_LEVEL_GUEST, |
| 70 |
reporter => $GITLAB_ACCESS_LEVEL_REPORTER, |
| 71 |
developer => $GITLAB_ACCESS_LEVEL_DEVELOPER, |
| 72 |
master => $GITLAB_ACCESS_LEVEL_MASTER, |
| 73 |
owner => $GITLAB_ACCESS_LEVEL_OWNER, |
| 74 |
}, |
| 75 |
}; |
| 76 |
|
| 77 |
my $params = {}; |
| 78 |
while (@ARGV) { |
| 79 |
my $arg = shift @ARGV; |
| 80 |
next if $arg eq '--'; |
| 81 |
|
| 82 |
if ($arg =~ m{^([^:]+):(.*)$}s) { |
| 83 |
my ($key, $value) = ($1, $2); |
| 84 |
|
| 85 |
$key =~ s{-}{_}g; |
| 86 |
|
| 87 |
if ($aliases->{$key} and exists $aliases->{$key}->{$value}) { |
| 88 |
$value = $aliases->{$key}->{$value}; |
| 89 |
} |
| 90 |
|
| 91 |
$params->{$key} = $value; |
| 92 |
} |
| 93 |
else { |
| 94 |
die "ERROR: Invalid API parameter \"$arg\".\n"; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
# Make sure we don't leak tokens in the logs. |
| 99 |
my $debug_config = { %{ $config->args() } }; |
| 100 |
$debug_config->{private_token} = 'xxxx' if $debug_config->{private_token}; |
| 101 |
$debug_config->{access_token} = 'xxxx' if $debug_config->{access_token}; |
| 102 |
|
| 103 |
$log->debug('config: ' . encode_json($debug_config)); |
| 104 |
$log->debug("method: $method"); |
| 105 |
$log->debug('arguments: ' . encode_json(\@args)); |
| 106 |
$log->debug('params: ' . encode_json($params)); |
| 107 |
|
| 108 |
my $api = GitLab::API::v4->new( $config->args() ); |
| 109 |
|
| 110 |
if ($all) { |
| 111 |
unshift @args, $method; |
| 112 |
$method = 'paginator'; |
| 113 |
} |
| 114 |
|
| 115 |
my $data = $api->$method( |
| 116 |
@args, |
| 117 |
%$params ? $params : (), |
| 118 |
); |
| 119 |
|
| 120 |
$data = $data->all() if $all; |
| 121 |
|
| 122 |
binmode STDOUT, ':utf8'; |
| 123 |
my $json = JSON::MaybeXS->new(allow_nonref => 1); |
| 124 |
$json->pretty() if $pretty; |
| 125 |
$json->canonical() if $canonical; |
| 126 |
print $json->encode( $data ); |
| 127 |
|
| 128 |
__END__ |
| 129 |
|
| 130 |
=encoding utf8 |
| 131 |
|
| 132 |
=head1 NAME |
| 133 |
|
| 134 |
gitlab-api-v4 - Command line interface to the GitLab API v4. |
| 135 |
|
| 136 |
=head1 SYNOPSIS |
| 137 |
|
| 138 |
# Generally: |
| 139 |
gitlab-api-v4 [<options>] <method> [<arg> ...] [<param>:<value> ...] |
| 140 |
|
| 141 |
# List all groups: |
| 142 |
gitlab-api-v4 groups |
| 143 |
|
| 144 |
# List information about a project: |
| 145 |
gitlab-api-v4 project <project_id> |
| 146 |
|
| 147 |
# Create an admin user: |
| 148 |
gitlab-api-v4 create-user \ |
| 149 |
username:foo \ |
| 150 |
password:xxxxxxxx \ |
| 151 |
email:[email protected] \ |
| 152 |
"name:Foo Smith" \ |
| 153 |
admin:1 |
| 154 |
|
| 155 |
=head1 CONFIGURING |
| 156 |
|
| 157 |
You may configure this module with environment variables, command line options, |
| 158 |
and a configuration file. To setup the configuration file run: |
| 159 |
|
| 160 |
gitlab-api-v4 configure |
| 161 |
|
| 162 |
This will ask several interactive questions to help you configure this script. |
| 163 |
The information, which may include GitLab authentication tokens, is stored in |
| 164 |
C<~/.gitlab-api-v4.json>. |
| 165 |
|
| 166 |
Read more at L<GitLab::API::v4::Config>. |
| 167 |
|
| 168 |
=head1 OPTIONS |
| 169 |
|
| 170 |
=head2 url |
| 171 |
|
| 172 |
--url=<url> |
| 173 |
|
| 174 |
Sets L<GitLab::API::v4/url>. |
| 175 |
|
| 176 |
=head2 access-token |
| 177 |
|
| 178 |
--access-token=<token> |
| 179 |
|
| 180 |
Sets L<GitLab::API::v4/access_token>. |
| 181 |
|
| 182 |
=head2 private-token |
| 183 |
|
| 184 |
--private-token=<token> |
| 185 |
|
| 186 |
Sets L<GitLab::API::v4/private_token>. |
| 187 |
|
| 188 |
=head2 retries |
| 189 |
|
| 190 |
--retries=<count> |
| 191 |
|
| 192 |
Sets L<GitLab::API::v4/retries>. |
| 193 |
|
| 194 |
=head2 all |
| 195 |
|
| 196 |
--all |
| 197 |
-a |
| 198 |
|
| 199 |
Retrieves all results when the results would normally be paged. |
| 200 |
See L<GitLab::API::v4::Paginator/all> for details. |
| 201 |
|
| 202 |
=head2 pretty |
| 203 |
|
| 204 |
--pretty |
| 205 |
-p |
| 206 |
|
| 207 |
Enables the L<JSON::PP/pretty> feature. |
| 208 |
|
| 209 |
=head2 canonical |
| 210 |
|
| 211 |
--canonical |
| 212 |
-c |
| 213 |
|
| 214 |
Enables the L<JSON::PP/canonical> feature. |
| 215 |
|
| 216 |
=head1 API METHOD |
| 217 |
|
| 218 |
<method> |
| 219 |
|
| 220 |
The API method to call - one of the methods documented in |
| 221 |
L<GitLab::API::v4/API METHODS>. |
| 222 |
|
| 223 |
=head1 API ARGUMENTS |
| 224 |
|
| 225 |
<arg> ... |
| 226 |
|
| 227 |
Any arguments that the L</API METHOD> requires. |
| 228 |
|
| 229 |
=head1 API PARAMETERS |
| 230 |
|
| 231 |
<param>:<value> ... |
| 232 |
|
| 233 |
Any parameters that the L</API METHOD> accepts. |
| 234 |
|
| 235 |
=head2 access-level |
| 236 |
|
| 237 |
access-level:guest |
| 238 |
access-level:reporter |
| 239 |
access-level:developer |
| 240 |
access-level:master |
| 241 |
access-level:owner |
| 242 |
|
| 243 |
There are mappings setup for the various C<access-level> parameters |
| 244 |
so that you can, for example, specify C<access-level:guest> and it |
| 245 |
will be automatically converted to C<access-level:10>. |
| 246 |
|
| 247 |
=head1 SUPPORT |
| 248 |
|
| 249 |
See L<GitLab::API::v4/SUPPORT>. |
| 250 |
|
| 251 |
=head1 AUTHORS |
| 252 |
|
| 253 |
See L<GitLab::API::v4/AUTHORS>. |
| 254 |
|
| 255 |
=head1 COPYRIGHT AND LICENSE |
| 256 |
|
| 257 |
See L<GitLab::API::v4/COPYRIGHT AND LICENSE>. |
| 258 |
|
| 259 |
=cut |
| 260 |
|