Skip to content

Commit

Permalink
-a/--artifacts for downloading artifacts.zip to $PWD
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Sep 18, 2020
1 parent 794de13 commit 72c8f28
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Changelog
==========

0.4.1 (unreleased)
0.5.0 (unreleased)
------------------

- Suppress tracebacks for keyboard interrupt or broken pipe errors.
- ``-a``/``--artifacts`` for downloading a job's artifacts.zip to the current
working directory.


0.4.0 (2020-09-16)
Expand Down
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ Help is available via ::

$ gitlab-trace --help
usage: gitlab-trace [-h] [--version] [-v] [--debug] [-g NAME] [-p ID]
[--job ID] [-b NAME] [PIPELINE-ID] [JOB-NAME] [NTH-JOB-OF-THAT-NAME]
[--job ID] [-b NAME] [-a]
[PIPELINE-ID] [JOB-NAME] [NTH-JOB-OF-THAT-NAME]

gitlab-trace: show the status/trace of a GitLab CI pipeline/job.

Expand All @@ -124,11 +125,13 @@ Help is available via ::
--debug print even more information, for debugging
-g NAME, --gitlab NAME
select configuration section in ~/.python-gitlab.cfg
-p ID, --project ID select GitLab project ('group/project' or the numeric ID)
-p ID, --project ID select GitLab project ('group/project' or the
numeric ID)
--job ID show the trace of GitLab CI job with this ID
-b NAME, --branch NAME, --ref NAME
show the last pipeline of this git branch (default:
the currently checked out branch)
-a, --artifacts download build artifacts


.. _python-gitlab: https://pypi.org/p/python-gitlab
Expand Down
21 changes: 20 additions & 1 deletion gitlab_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import gitlab


__version__ = '0.4.1.dev0'
__version__ = '0.5.0.dev0'
__author__ = "Marius Gedminas <marius@gedmin.as>"


Expand Down Expand Up @@ -88,6 +88,16 @@ def fmt_duration(duration: Optional[float]) -> str:
return " ".join(bits)


def fmt_size(size: Optional[float]) -> str:
if size is None:
return 'n/a'
for unit in 'B', 'KiB', 'MiB':
if size < 1024:
break
size /= 1024
return f'{size:.1f}'.rstrip('0').rstrip('.') + f' {unit}'


def _main() -> None:
colorama.init()

Expand Down Expand Up @@ -126,6 +136,10 @@ def _main() -> None:
" (default: the currently checked out branch)"
),
)
parser.add_argument(
"-a", "--artifacts", action="store_true",
help="download build artifacts",
)
parser.add_argument(
"pipeline", nargs="?", metavar="PIPELINE-ID",
help=(
Expand Down Expand Up @@ -207,6 +221,11 @@ def _main() -> None:
if args.debug:
info(json.dumps(job.attributes, indent=2))
sys.stdout.buffer.write(job.trace())
if args.artifacts:
filename = job.artifacts_file['filename']
info(f"Artifacts: {filename} ({fmt_size(job.artifacts_file['size'])})")
with open(filename, "xb") as f:
job.artifacts(streamed=True, action=f.write)
sys.exit(0)


Expand Down
37 changes: 37 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@ def __init__(self, id, name, status):
self.started_at = '2020-09-16T06:16:51.066Z'
self.finished_at = None
self.duration = 42
self.artifacts_file = {
'filename': 'artifacts.zip',
'size': 0,
}
self.attributes = {"json_attributes": "here"}

def artifacts(self, streamed=False, action=None):
pass

def trace(self):
return b'Hello, world!\n'

Expand Down Expand Up @@ -165,6 +172,19 @@ def test_fmt_duration(duration, expected):
assert gt.fmt_duration(duration) == expected


@pytest.mark.parametrize('size, expected', [
(0, '0 B'),
(1, '1 B'),
(1024, '1 KiB'),
(1124, '1.1 KiB'),
(1024**2, '1 MiB'),
(3.2 * 1024**2, '3.2 MiB'),
(None, 'n/a'),
])
def test_fmt_size(size, expected):
assert gt.fmt_size(size) == expected


def test_main_help(monkeypatch):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--help'])
with pytest.raises(SystemExit):
Expand Down Expand Up @@ -374,6 +394,23 @@ def test_main_job_debug(monkeypatch, capsys):
""")


def test_main_job_artefacts(monkeypatch, capsys, tmp_path):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3202', '-a'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Artifacts: artifacts.zip (0 B)
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")


def raise_keyboard_interrupt(*args, **kw):
raise KeyboardInterrupt()

Expand Down

0 comments on commit 72c8f28

Please sign in to comment.