Skip to content

Commit

Permalink
Merge pull request #1159 from python-gitlab/feat/project-artifacts
Browse files Browse the repository at this point in the history
Feat: Project job artifacts for latest successful pipeline
  • Loading branch information
max-wittig committed Aug 29, 2020
2 parents 0f42e32 + c2806d8 commit 26f95f3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/gl_objects/pipelines_and_jobs.rst
Expand Up @@ -292,6 +292,11 @@ Get the artifacts of a job::

build_or_job.artifacts()

Get the artifacts of a job by its name from the latest successful pipeline of
a branch or tag:

project.artifacts(ref_name='master', job='build')

.. warning::

Artifacts are entirely stored in memory in this example.
Expand Down
31 changes: 31 additions & 0 deletions gitlab/tests/objects/test_job_artifacts.py
@@ -0,0 +1,31 @@
"""
GitLab API: https://docs.gitlab.com/ee/api/job_artifacts.html
"""

import pytest
import responses


ref_name = "master"
job = "build"


@pytest.fixture
def resp_artifacts_by_ref_name(binary_content):
url = f"http://localhost/api/v4/projects/1/jobs/artifacts/{ref_name}/download?job={job}"

with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url=url,
body=binary_content,
content_type="application/octet-stream",
status=200,
)
yield rsps


def test_download_artifacts_by_ref_name(gl, binary_content, resp_artifacts_by_ref_name):
project = gl.projects.get(1, lazy=True)
artifacts = project.artifacts(ref_name=ref_name, job=job)
assert artifacts == binary_content
34 changes: 34 additions & 0 deletions gitlab/v4/objects.py
Expand Up @@ -5122,6 +5122,40 @@ def transfer_project(self, to_namespace, **kwargs):
path, post_data={"namespace": to_namespace}, **kwargs
)

@cli.register_custom_action("Project", ("ref_name", "job"), ("job_token",))
@exc.on_http_error(exc.GitlabGetError)
def artifacts(
self, ref_name, job, streamed=False, action=None, chunk_size=1024, **kwargs
):
"""Get the job artifacts archive from a specific tag or branch.
Args:
ref_name (str): Branch or tag name in repository. HEAD or SHA references
are not supported.
artifact_path (str): Path to a file inside the artifacts archive.
job (str): The name of the job.
job_token (str): Job token for multi-project pipeline triggers.
streamed (bool): If True the data will be processed by chunks of
`chunk_size` and each chunk is passed to `action` for
treatment
action (callable): Callable responsible of dealing with chunk of
data
chunk_size (int): Size of each chunk
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabGetError: If the artifacts could not be retrieved
Returns:
str: The artifacts if `streamed` is False, None otherwise.
"""
path = "/projects/%s/jobs/artifacts/%s/download" % (self.get_id(), ref_name)
result = self.manager.gitlab.http_get(
path, job=job, streamed=streamed, raw=True, **kwargs
)
return utils.response_content(result, streamed, action, chunk_size)

@cli.register_custom_action("Project", ("ref_name", "artifact_path", "job"))
@exc.on_http_error(exc.GitlabGetError)
def artifact(
Expand Down

0 comments on commit 26f95f3

Please sign in to comment.