summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Wittig <max.wittig@siemens.com>2020-08-29 12:08:34 +0200
committerGitHub <noreply@github.com>2020-08-29 12:08:34 +0200
commit26f95f30a5219243f33d505747c65f798ac6a486 (patch)
tree158c7003b5774799ece2880779c8649c3b0f89dc
parent0f42e32cb756766735c7e277f099030f6b3d8fc7 (diff)
parentc2806d8c0454a83dfdafd1bdbf7e10bb28d205e0 (diff)
downloadgitlab-26f95f30a5219243f33d505747c65f798ac6a486.tar.gz
Merge pull request #1159 from python-gitlab/feat/project-artifacts
Feat: Project job artifacts for latest successful pipeline
-rw-r--r--docs/gl_objects/pipelines_and_jobs.rst5
-rw-r--r--gitlab/tests/objects/test_job_artifacts.py31
-rw-r--r--gitlab/v4/objects.py34
3 files changed, 70 insertions, 0 deletions
diff --git a/docs/gl_objects/pipelines_and_jobs.rst b/docs/gl_objects/pipelines_and_jobs.rst
index 7faf657..eb9e23a 100644
--- a/docs/gl_objects/pipelines_and_jobs.rst
+++ b/docs/gl_objects/pipelines_and_jobs.rst
@@ -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.
diff --git a/gitlab/tests/objects/test_job_artifacts.py b/gitlab/tests/objects/test_job_artifacts.py
new file mode 100644
index 0000000..c441b4b
--- /dev/null
+++ b/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
diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py
index 84fb5c3..8d24b52 100644
--- a/gitlab/v4/objects.py
+++ b/gitlab/v4/objects.py
@@ -5122,6 +5122,40 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
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(