Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/cli-examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ Download the artifacts zip archive of a job:

$ gitlab project-job artifacts --id 10 --project-id 1 > artifacts.zip

Download one file from a job's artifacts using its path within the archive:

.. code-block:: console

$ gitlab project-job artifact --id 10 --project-id 1 --path reports/result.xml > result.xml

Runners
-------

Expand Down
2 changes: 1 addition & 1 deletion gitlab/v4/objects/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def artifact(
**kwargs: Any,
) -> None: ...

@cli.register_custom_action(cls_names="ProjectJob")
@cli.register_custom_action(cls_names="ProjectJob", required=("path",))
@exc.on_http_error(exc.GitlabGetError)
def artifact(
self,
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from unittest import mock

import pytest
import responses

import gitlab.base
from gitlab import cli
from gitlab.exceptions import GitlabError
from gitlab.mixins import CreateMixin, UpdateMixin
from gitlab.types import RequiredOptional
from gitlab.v4 import cli as v4_cli
from gitlab.v4.objects import ProjectJobManager


@pytest.mark.parametrize(
Expand Down Expand Up @@ -119,6 +121,40 @@ def test_v4_parse_args():
assert args.resource_action == "list"


@pytest.mark.parametrize(
"path", ["result.bin", "reports/result.bin", "reports/a file.bin"]
)
@responses.activate
def test_project_job_artifact_cli_passes_required_path(
gl: gitlab.Gitlab, path: str
) -> None:
args = cli._get_parser().parse_args(
["project-job", "artifact", "--project-id", "1", "--id", "2", "--path", path]
)
payload = b"artifact\x00\xff"
url = f"http://localhost/api/v4/projects/1/jobs/2/artifacts/{path}"
responses.add(responses.GET, url, body=payload)

with mock.patch.object(ProjectJobManager, "_path", ProjectJobManager._path):
result = v4_cli.GitlabCLI(
gl,
args.gitlab_resource,
args.resource_action,
{"project_id": args.project_id, "id": args.id, "path": args.path},
).run()

assert result == payload
assert len(responses.calls) == 1


def test_project_job_artifact_cli_requires_path() -> None:
with pytest.raises(SystemExit) as error:
cli._get_parser().parse_args(
["project-job", "artifact", "--project-id", "1", "--id", "2"]
)
assert error.value.code == 2


def test_v4_parser():
parser = cli._get_parser()
subparsers = next(
Expand Down
Loading