bluefeet / GitLab-API-v4

A complete GitLab API v4 client.
https://metacpan.org/pod/GitLab::API::v4
Other
20 stars 23 forks source link

methods that download files aren't handled correctly by GitLab::API::v4::RESTClient #48

Open jwrightecs opened 3 years ago

jwrightecs commented 3 years ago

In the gitlab v4 api there are functions that download files, for example the artifact functions under jobs, this causes an error in GitLab::API::v4::RESTClient, and in gitlabl-api-v4

Actual error depends on perl version

$ perl  -Mdiagnostics -S gitlab-api-v4 job-artifacts repo/project 8003 
Redundant argument in sprintf at
    /home/gitlab/perl5/lib/perl5/GitLab/API/v4/RESTClient.pm line 66 (#1)
    (W redundant) You called a function with more arguments than other
    arguments you supplied indicated would be needed.  Currently only
    emitted when a printf-type format required fewer arguments than were
    supplied, but might be used in the future for e.g. "pack" in perlfunc.

Uncaught exception from user code:
    Redundant argument in sprintf at /home/gitlab/perl5/lib/perl5/GitLab/API/v4/RESTClient.pm line 66.
    GitLab::API::v4::RESTClient::croakf("Error decoding JSON (%s %s %s): ", "GET", "https://code.ecstuning.io/api/v4/projects/repo%2Fproject/jobs/8003"..., 200, "malformed JSON string, neither tag, array, object, number, st"...) called at /home/gitlab/perl5/lib/perl5/GitLab/API/v4/RESTClient.pm line 243
    GitLab::API::v4::RESTClient::catch {...} ("malformed JSON string, neither tag, array, object, number, st"...) called at /home/gitlab/perl5/lib/perl5/Try/Tiny.pm line 123
    Try::Tiny::try(CODE(0x55ceea834e08), Try::Tiny::Catch=REF(0x55cee96f1710)) called at /home/gitlab/perl5/lib/perl5/GitLab/API/v4/RESTClient.pm line 245
    GitLab::API::v4::RESTClient::request(GitLab::API::v4::RESTClient=HASH(0x55cee9d42de0), "GET", "projects/:project_id/jobs/:job_id/artifacts", ARRAY(0x55ceea79c2c0), HASH(0x55ceea7c6200)) called at /home/gitlab/perl5/lib/perl5/GitLab/API/v4.pm line 123
    GitLab::API::v4::_call_rest_client(GitLab::API::v4=HASH(0x55ceea813068), "GET", "projects/:project_id/jobs/:job_id/artifacts", ARRAY(0x55ceea79c2c0), HASH(0x55ceea7c6200)) called at /home/gitlab/perl5/lib/perl5/GitLab/API/v4.pm line 4062
    GitLab::API::v4::job_artifacts(GitLab::API::v4=HASH(0x55ceea813068), "repo/project", 8003) called at /home/gitlab/perl5/bin/gitlab-api-v4 line 115

(Older versions without the 'redundant' warning class)

$ perl -Mdiagnostics -S gitlab-api-v4 job-artifacts repo/project 8003
Uncaught exception from user code:
        malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "PK\x{3}\x{4}\x{14}\x{0}...") at /usr/local/share/perl5/GitLab/API/v4/RESTClient.pm  line 177.
 at /usr/local/share/perl5/GitLab/API/v4/RESTClient.pm line 177.
        GitLab::API::v4::RESTClient::request(GitLab::API::v4::RESTClient=HASH(0x1ef65a0), "GET", "projects/:project_id/jobs/:job_id/artifacts", ARRAY(0x2a3c448), HASH(0x23f9558)) called at /usr/loc
al/share/perl5/GitLab/API/v4.pm line 123
        GitLab::API::v4::_call_rest_client(GitLab::API::v4=HASH(0x23cd8e8), "GET", "projects/:project_id/jobs/:job_id/artifacts", ARRAY(0x2a3c448), HASH(0x23f9558)) called at /usr/local/share/perl5
/GitLab/API/v4.pm line 3872
        GitLab::API::v4::job_artifacts(GitLab::API::v4=HASH(0x23cd8e8), "repo/project", 8003) called at /usr/local/bin/gitlab-api-v4 line 117

Similarly, using the module directly:

$ reply
0> use GitLab::API::v4::Config; use GitLab::API::v4;
1> my $api = GitLab::API::v4->new(  GitLab::API::v4::Config->new->args );
$res[0] = bless( {
         '_access_token_closure' => sub { "DUMMY" },
         '_private_token_closure' => sub { "DUMMY" },
         'retries' => 0,
         'url' => 'https://code.gitlab.io/api/v4'
       }, 'GitLab::API::v4' )

2>  my $artifacts = $api->job_artifacts('repo/project', 8003);
Redundant argument in sprintf at /home/gitlab/perl5/lib/perl5/GitLab/API/v4/RESTClient.pm line 66.
3> 
sur5r commented 1 month ago

I tried adding $options->{decode}=0 after https://github.com/bluefeet/GitLab-API-v4/blob/4f31181771706c5c680f2b91aa66992898899c85/lib/GitLab/API/v4.pm#L4107 which prevents the error.

However, the artifact (a ZIP archive in my case) is printed to stdout (good) but with non-printable characters escaped as \uXXXX and double quotes around it (not so good).

It seems the handling of non-JSON replies needs to be rethought.