Skip to content

fix(cli): write binary data to stdout buffer #1202

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 4 commits into from
Oct 12, 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
6 changes: 6 additions & 0 deletions docs/cli-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions gitlab/v4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -440,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())
52 changes: 52 additions & 0 deletions tools/functional/cli/test_cli_artifacts.py
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions tools/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
17 changes: 16 additions & 1 deletion tools/functional/fixtures/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
version: '3'

networks:
gitlab-network:
name: gitlab-network

services:
gitlab:
image: '${GITLAB_IMAGE}:${GITLAB_TAG}'
Expand All @@ -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
Expand All @@ -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