Skip to content

Commit b7a07fc

Browse files
committed
feat(api): add endpoint for latest ref artifacts
1 parent 4039c8c commit b7a07fc

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
GitLab API: https://docs.gitlab.com/ee/api/job_artifacts.html
3+
"""
4+
5+
import pytest
6+
import responses
7+
8+
from gitlab.v4.objects import Project
9+
10+
11+
ref_name = "master"
12+
job = "build"
13+
14+
15+
@pytest.fixture
16+
def resp_artifacts_by_ref_name(binary_content):
17+
url = f"http://localhost/api/v4/projects/1/jobs/artifacts/{ref_name}/download?job={job}"
18+
19+
with responses.RequestsMock() as rsps:
20+
rsps.add(
21+
method=responses.GET,
22+
url=url,
23+
body=binary_content,
24+
content_type="application/octet-stream",
25+
status=200,
26+
)
27+
yield rsps
28+
29+
30+
def test_download_artifacts_by_ref_name(gl, binary_content, resp_artifacts_by_ref_name):
31+
project = gl.projects.get(1, lazy=True)
32+
artifacts = project.artifacts(ref_name=ref_name, job=job)
33+
assert artifacts == binary_content

gitlab/v4/objects.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5087,6 +5087,40 @@ def transfer_project(self, to_namespace, **kwargs):
50875087
path, post_data={"namespace": to_namespace}, **kwargs
50885088
)
50895089

5090+
@cli.register_custom_action("Project", ("ref_name", "job"), ("job_token",))
5091+
@exc.on_http_error(exc.GitlabGetError)
5092+
def artifacts(
5093+
self, ref_name, job, streamed=False, action=None, chunk_size=1024, **kwargs
5094+
):
5095+
"""Get the job artifacts archive from a specific tag or branch.
5096+
5097+
Args:
5098+
ref_name (str): Branch or tag name in repository. HEAD or SHA references
5099+
are not supported.
5100+
artifact_path (str): Path to a file inside the artifacts archive.
5101+
job (str): The name of the job.
5102+
job_token (str): Job token for multi-project pipeline triggers.
5103+
streamed (bool): If True the data will be processed by chunks of
5104+
`chunk_size` and each chunk is passed to `action` for
5105+
treatment
5106+
action (callable): Callable responsible of dealing with chunk of
5107+
data
5108+
chunk_size (int): Size of each chunk
5109+
**kwargs: Extra options to send to the server (e.g. sudo)
5110+
5111+
Raises:
5112+
GitlabAuthenticationError: If authentication is not correct
5113+
GitlabGetError: If the artifacts could not be retrieved
5114+
5115+
Returns:
5116+
str: The artifacts if `streamed` is False, None otherwise.
5117+
"""
5118+
path = "/projects/%s/jobs/artifacts/%s/download" % (self.get_id(), ref_name)
5119+
result = self.manager.gitlab.http_get(
5120+
path, job=job, streamed=streamed, raw=True, **kwargs
5121+
)
5122+
return utils.response_content(result, streamed, action, chunk_size)
5123+
50905124
@cli.register_custom_action("Project", ("ref_name", "artifact_path", "job"))
50915125
@exc.on_http_error(exc.GitlabGetError)
50925126
def artifact(

0 commit comments

Comments
 (0)