Skip to content

Commit 061422b

Browse files
committed
fix(cli): expose the required job artifact path
Signed-off-by: Umut Alisoglu <umutalisoglu@gmail.com>
1 parent 51eb17e commit 061422b

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎docs/cli-examples.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ Download the artifacts zip archive of a job:
322322
323323
$ gitlab project-job artifacts --id 10 --project-id 1 > artifacts.zip
324324
325+
Download one file from a job's artifacts using its path within the archive:
326+
327+
.. code-block:: console
328+
329+
$ gitlab project-job artifact --id 10 --project-id 1 --path reports/result.xml > result.xml
330+
325331
Runners
326332
-------
327333

‎gitlab/v4/objects/jobs.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def artifact(
224224
**kwargs: Any,
225225
) -> None: ...
226226

227-
@cli.register_custom_action(cls_names="ProjectJob")
227+
@cli.register_custom_action(cls_names="ProjectJob", required=("path",))
228228
@exc.on_http_error(exc.GitlabGetError)
229229
def artifact(
230230
self,

‎tests/unit/test_cli.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
from unittest import mock
77

88
import pytest
9+
import responses
910

1011
import gitlab.base
1112
from gitlab import cli
1213
from gitlab.exceptions import GitlabError
1314
from gitlab.mixins import CreateMixin, UpdateMixin
1415
from gitlab.types import RequiredOptional
1516
from gitlab.v4 import cli as v4_cli
17+
from gitlab.v4.objects import ProjectJobManager
1618

1719

1820
@pytest.mark.parametrize(
@@ -119,6 +121,40 @@ def test_v4_parse_args():
119121
assert args.resource_action == "list"
120122

121123

124+
@pytest.mark.parametrize(
125+
"path", ["result.bin", "reports/result.bin", "reports/a file.bin"]
126+
)
127+
@responses.activate
128+
def test_project_job_artifact_cli_passes_required_path(
129+
gl: gitlab.Gitlab, path: str
130+
) -> None:
131+
args = cli._get_parser().parse_args(
132+
["project-job", "artifact", "--project-id", "1", "--id", "2", "--path", path]
133+
)
134+
payload = b"artifact\x00\xff"
135+
url = f"http://localhost/api/v4/projects/1/jobs/2/artifacts/{path}"
136+
responses.add(responses.GET, url, body=payload)
137+
138+
with mock.patch.object(ProjectJobManager, "_path", ProjectJobManager._path):
139+
result = v4_cli.GitlabCLI(
140+
gl,
141+
args.gitlab_resource,
142+
args.resource_action,
143+
{"project_id": args.project_id, "id": args.id, "path": args.path},
144+
).run()
145+
146+
assert result == payload
147+
assert len(responses.calls) == 1
148+
149+
150+
def test_project_job_artifact_cli_requires_path() -> None:
151+
with pytest.raises(SystemExit) as error:
152+
cli._get_parser().parse_args(
153+
["project-job", "artifact", "--project-id", "1", "--id", "2"]
154+
)
155+
assert error.value.code == 2
156+
157+
122158
def test_v4_parser():
123159
parser = cli._get_parser()
124160
subparsers = next(

0 commit comments

Comments
 (0)