Skip to content

Commit 26f95f3

Browse files
authored
Merge pull request python-gitlab#1159 from python-gitlab/feat/project-artifacts
Feat: Project job artifacts for latest successful pipeline
2 parents 0f42e32 + c2806d8 commit 26f95f3

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

docs/gl_objects/pipelines_and_jobs.rst

+5
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ Get the artifacts of a job::
292292

293293
build_or_job.artifacts()
294294

295+
Get the artifacts of a job by its name from the latest successful pipeline of
296+
a branch or tag:
297+
298+
project.artifacts(ref_name='master', job='build')
299+
295300
.. warning::
296301

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

gitlab/v4/objects.py

+34
Original file line numberDiff line numberDiff line change
@@ -5122,6 +5122,40 @@ def transfer_project(self, to_namespace, **kwargs):
51225122
path, post_data={"namespace": to_namespace}, **kwargs
51235123
)
51245124

5125+
@cli.register_custom_action("Project", ("ref_name", "job"), ("job_token",))
5126+
@exc.on_http_error(exc.GitlabGetError)
5127+
def artifacts(
5128+
self, ref_name, job, streamed=False, action=None, chunk_size=1024, **kwargs
5129+
):
5130+
"""Get the job artifacts archive from a specific tag or branch.
5131+
5132+
Args:
5133+
ref_name (str): Branch or tag name in repository. HEAD or SHA references
5134+
are not supported.
5135+
artifact_path (str): Path to a file inside the artifacts archive.
5136+
job (str): The name of the job.
5137+
job_token (str): Job token for multi-project pipeline triggers.
5138+
streamed (bool): If True the data will be processed by chunks of
5139+
`chunk_size` and each chunk is passed to `action` for
5140+
treatment
5141+
action (callable): Callable responsible of dealing with chunk of
5142+
data
5143+
chunk_size (int): Size of each chunk
5144+
**kwargs: Extra options to send to the server (e.g. sudo)
5145+
5146+
Raises:
5147+
GitlabAuthenticationError: If authentication is not correct
5148+
GitlabGetError: If the artifacts could not be retrieved
5149+
5150+
Returns:
5151+
str: The artifacts if `streamed` is False, None otherwise.
5152+
"""
5153+
path = "/projects/%s/jobs/artifacts/%s/download" % (self.get_id(), ref_name)
5154+
result = self.manager.gitlab.http_get(
5155+
path, job=job, streamed=streamed, raw=True, **kwargs
5156+
)
5157+
return utils.response_content(result, streamed, action, chunk_size)
5158+
51255159
@cli.register_custom_action("Project", ("ref_name", "artifact_path", "job"))
51265160
@exc.on_http_error(exc.GitlabGetError)
51275161
def artifact(

0 commit comments

Comments
 (0)