From 1030e0a7e13c4ec3fdc48b9010e9892833850db9 Mon Sep 17 00:00:00 2001 From: Nejc Habjan Date: Sun, 20 Sep 2020 23:17:36 +0200 Subject: [PATCH 1/4] chore(cli): remove python2 code --- gitlab/v4/cli.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py index 51416f142..fe5ab80ad 100644 --- a/gitlab/v4/cli.py +++ b/gitlab/v4/cli.py @@ -85,11 +85,7 @@ def do_project_export_download(self): try: project = self.gl.projects.get(int(self.args["project_id"]), lazy=True) data = project.exports.get().download() - if hasattr(sys.stdout, "buffer"): - # python3 - sys.stdout.buffer.write(data) - else: - sys.stdout.write(data) + sys.stdout.buffer.write(data) except Exception as e: cli.die("Impossible to download the export", e) From 0733ec6cad5c11b470ce6bad5dc559018ff73b3c Mon Sep 17 00:00:00 2001 From: Nejc Habjan Date: Thu, 1 Oct 2020 01:46:42 +0200 Subject: [PATCH 2/4] fix(cli): write binary data to stdout buffer --- gitlab/v4/cli.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py index fe5ab80ad..6172f9310 100644 --- a/gitlab/v4/cli.py +++ b/gitlab/v4/cli.py @@ -436,5 +436,7 @@ def run(gl, what, action, args, verbose, output, fields): printer.display(get_dict(data, fields), verbose=verbose, obj=data) elif isinstance(data, str): print(data) + elif isinstance(data, bytes): + sys.stdout.buffer.write(data) elif hasattr(data, "decode"): print(data.decode()) From 375b29d3ab393f7b3fa734c5320736cdcba5df8a Mon Sep 17 00:00:00 2001 From: Nejc Habjan Date: Thu, 1 Oct 2020 02:02:37 +0200 Subject: [PATCH 3/4] docs(cli): add example for job artifacts download --- docs/cli-usage.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/cli-usage.rst b/docs/cli-usage.rst index 10fd73afe..d66d73849 100644 --- a/docs/cli-usage.rst +++ b/docs/cli-usage.rst @@ -313,6 +313,12 @@ Define the status of a commit (as would be done from a CI tool for example): --target-url http://server/build/123 \ --description "Jenkins build succeeded" +Download the artifacts zip archive of a job: + +.. code-block:: console + + $ gitlab project-job artifacts --id 10 --project-id 1 > artifacts.zip + Use sudo to act as another user (admin only): .. code-block:: console From f4e79501f1be1394873042dd65beda49e869afb8 Mon Sep 17 00:00:00 2001 From: Nejc Habjan Date: Sat, 10 Oct 2020 17:24:57 +0200 Subject: [PATCH 4/4] test(cli): add test for job artifacts download --- tools/functional/cli/test_cli_artifacts.py | 52 ++++++++++++++++++++ tools/functional/conftest.py | 30 +++++++++++ tools/functional/fixtures/docker-compose.yml | 17 ++++++- 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 tools/functional/cli/test_cli_artifacts.py diff --git a/tools/functional/cli/test_cli_artifacts.py b/tools/functional/cli/test_cli_artifacts.py new file mode 100644 index 000000000..27d5d7473 --- /dev/null +++ b/tools/functional/cli/test_cli_artifacts.py @@ -0,0 +1,52 @@ +import subprocess +import sys +import textwrap +import time +from io import BytesIO +from zipfile import is_zipfile + +import pytest + + +content = textwrap.dedent( + """\ + test-artifact: + script: echo "test" > artifact.txt + artifacts: + untracked: true + """ +) +data = { + "file_path": ".gitlab-ci.yml", + "branch": "master", + "content": content, + "commit_message": "Initial commit", +} + + +@pytest.mark.skipif(sys.version_info < (3, 8), reason="I am the walrus") +def test_cli_artifacts(capsysbinary, gitlab_config, gitlab_runner, project): + project.files.create(data) + + while not (jobs := project.jobs.list(scope="success")): + time.sleep(0.5) + + job = project.jobs.get(jobs[0].id) + cmd = [ + "gitlab", + "--config-file", + gitlab_config, + "project-job", + "artifacts", + "--id", + str(job.id), + "--project-id", + str(project.id), + ] + + with capsysbinary.disabled(): + artifacts = subprocess.check_output(cmd) + assert isinstance(artifacts, bytes) + + artifacts_zip = BytesIO(artifacts) + assert is_zipfile(artifacts_zip) diff --git a/tools/functional/conftest.py b/tools/functional/conftest.py index 0cca3e35f..675dba960 100644 --- a/tools/functional/conftest.py +++ b/tools/functional/conftest.py @@ -132,6 +132,36 @@ def gl(gitlab_config): return instance +@pytest.fixture(scope="session") +def gitlab_runner(gl): + container = "gitlab-runner-test" + runner_name = "python-gitlab-runner" + token = "registration-token" + url = "http://gitlab" + + docker_exec = ["docker", "exec", container, "gitlab-runner"] + register = [ + "register", + "--run-untagged", + "--non-interactive", + "--registration-token", + token, + "--name", + runner_name, + "--url", + url, + "--clone-url", + url, + "--executor", + "shell", + ] + unregister = ["unregister", "--name", runner_name] + + yield check_output(docker_exec + register).decode() + + check_output(docker_exec + unregister).decode() + + @pytest.fixture(scope="module") def group(gl): """Group fixture for group API resource tests.""" diff --git a/tools/functional/fixtures/docker-compose.yml b/tools/functional/fixtures/docker-compose.yml index 687eeaa8b..a0794d6d6 100644 --- a/tools/functional/fixtures/docker-compose.yml +++ b/tools/functional/fixtures/docker-compose.yml @@ -1,4 +1,9 @@ version: '3' + +networks: + gitlab-network: + name: gitlab-network + services: gitlab: image: '${GITLAB_IMAGE}:${GITLAB_TAG}' @@ -9,7 +14,7 @@ services: GITLAB_OMNIBUS_CONFIG: | external_url 'http://gitlab.test' gitlab_rails['initial_root_password'] = '5iveL!fe' - gitlab_rails['initial_shared_runners_registration_token'] = 'sTPNtWLEuSrHzoHP8oCU' + gitlab_rails['initial_shared_runners_registration_token'] = 'registration-token' registry['enable'] = false nginx['redirect_http_to_https'] = false nginx['listen_port'] = 80 @@ -29,3 +34,13 @@ services: ports: - '8080:80' - '2222:22' + networks: + - gitlab-network + + gitlab-runner: + image: gitlab/gitlab-runner:latest + container_name: 'gitlab-runner-test' + depends_on: + - gitlab + networks: + - gitlab-network