Skip to content

Feat: Project job artifacts for latest successful pipeline #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/gl_objects/pipelines_and_jobs.rst
Original file line number Diff line number Diff line change
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
8 changes: 7 additions & 1 deletion gitlab/tests/objects/test_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ def test_create_commit(project, resp_create_commit):
data = {
"branch": "master",
"commit_message": "Commit message",
"actions": [{"action": "create", "file_path": "README", "content": "",}],
"actions": [
{
"action": "create",
"file_path": "README",
"content": "",
}
],
}
commit = project.commits.create(data)
assert commit.short_id == "ed899a2f"
Expand Down
31 changes: 31 additions & 0 deletions gitlab/tests/objects/test_job_artifacts.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 9 additions & 3 deletions gitlab/tests/objects/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ def resp_runner_delete():
status=200,
)
rsps.add(
method=responses.DELETE, url=pattern, status=204,
method=responses.DELETE,
url=pattern,
status=204,
)
yield rsps

Expand All @@ -177,7 +179,9 @@ def resp_runner_disable():
with responses.RequestsMock() as rsps:
pattern = re.compile(r".*?/(groups|projects)/1/runners/6")
rsps.add(
method=responses.DELETE, url=pattern, status=204,
method=responses.DELETE,
url=pattern,
status=204,
)
yield rsps

Expand All @@ -187,7 +191,9 @@ def resp_runner_verify():
with responses.RequestsMock() as rsps:
pattern = re.compile(r".*?/runners/verify")
rsps.add(
method=responses.POST, url=pattern, status=200,
method=responses.POST,
url=pattern,
status=200,
)
yield rsps

Expand Down
60 changes: 55 additions & 5 deletions gitlab/v4/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,14 @@ class ProjectDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager
_from_parent_attrs = {"project_id": "id"}
_obj_cls = ProjectDeployToken
_create_attrs = (
("name", "scopes",),
("expires_at", "username",),
(
"name",
"scopes",
),
(
"expires_at",
"username",
),
)


Expand All @@ -725,8 +731,14 @@ class GroupDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager):
_from_parent_attrs = {"group_id": "id"}
_obj_cls = GroupDeployToken
_create_attrs = (
("name", "scopes",),
("expires_at", "username",),
(
"name",
"scopes",
),
(
"expires_at",
"username",
),
)


Expand Down Expand Up @@ -4181,7 +4193,11 @@ class ProjectServiceManager(GetMixin, UpdateMixin, DeleteMixin, ListMixin, RESTM
),
),
"jira": (
("url", "username", "password",),
(
"url",
"username",
"password",
),
(
"api_url",
"active",
Expand Down Expand Up @@ -5071,6 +5087,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
7 changes: 6 additions & 1 deletion tools/python_test_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,12 @@

# Remove once https://gitlab.com/gitlab-org/gitlab/-/issues/211878 is fixed
deploy_token = deploy_token_group.deploytokens.create(
{"name": "foo", "username": "", "expires_at": "", "scopes": ["read_repository"],}
{
"name": "foo",
"username": "",
"expires_at": "",
"scopes": ["read_repository"],
}
)

assert len(deploy_token_group.deploytokens.list()) == 1
Expand Down