Skip to content

Commit 4a8503d

Browse files
authored
Merge pull request #824 from python-gitlab/feat/add-ref-artifacts
feat: get artifact by ref and job
2 parents ad1c0dd + cda1174 commit 4a8503d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

docs/gl_objects/builds.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ Get a single artifact file::
319319

320320
build_or_job.artifact('path/to/file')
321321

322+
Get a single artifact file by branch and job::
323+
324+
project.artifact('branch', 'path/to/file', 'job')
325+
322326
Mark a job artifact as kept when expiration is set::
323327

324328
build_or_job.keep_artifacts()

gitlab/v4/objects.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4263,6 +4263,38 @@ def transfer_project(self, to_namespace, **kwargs):
42634263
)
42644264

42654265

4266+
@cli.register_custom_action("Project", ("ref_name", "artifact_path", "job"))
4267+
@exc.on_http_error(exc.GitlabGetError)
4268+
def artifact(self, ref_name, artifact_path, job, streamed=False, action=None, chunk_size=1024, **kwargs):
4269+
"""Download a single artifact file from a specific tag or branch from within the job’s artifacts archive.
4270+
4271+
Args:
4272+
ref_name (str): Branch or tag name in repository. HEAD or SHA references are not supported.
4273+
artifact_path (str): Path to a file inside the artifacts archive.
4274+
job (str): The name of the job.
4275+
streamed (bool): If True the data will be processed by chunks of
4276+
`chunk_size` and each chunk is passed to `action` for
4277+
treatment
4278+
action (callable): Callable responsible of dealing with chunk of
4279+
data
4280+
chunk_size (int): Size of each chunk
4281+
**kwargs: Extra options to send to the server (e.g. sudo)
4282+
4283+
Raises:
4284+
GitlabAuthenticationError: If authentication is not correct
4285+
GitlabGetError: If the artifacts could not be retrieved
4286+
4287+
Returns:
4288+
str: The artifacts if `streamed` is False, None otherwise.
4289+
"""
4290+
4291+
path = "/projects/%s/jobs/artifacts/%s/raw/%s?job=%s" % (self.get_id(), ref_name, artifact_path, job)
4292+
result = self.manager.gitlab.http_get(
4293+
path, streamed=streamed, raw=True, **kwargs
4294+
)
4295+
return utils.response_content(result, streamed, action, chunk_size)
4296+
4297+
42664298
class ProjectManager(CRUDMixin, RESTManager):
42674299
_path = "/projects"
42684300
_obj_cls = Project

0 commit comments

Comments
 (0)