Skip to content

Commit

Permalink
Make --verbose show job timestamps, add --debug
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Sep 16, 2020
1 parent 67729c1 commit fd51876
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
@@ -1,12 +1,14 @@
Changelog
==========

0.3.1 (unreleased)
0.4.0 (unreleased)
------------------

- 100% test coverage.
- Fixes for Python 3.6 compatibility (subprocess.run() doesn't
accept 'text' or 'capture_output' keyword arguments).
- ``--verbose`` shows job start/finish times and duration on stderr.
- ``--debug`` for seeing raw JSON data available from GitLab API.


0.3.0 (2020-09-09)
Expand Down
15 changes: 10 additions & 5 deletions README.rst
Expand Up @@ -105,25 +105,30 @@ Usage
Help is available via ::

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

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

positional arguments:
PIPELINE-ID select a GitLab CI pipeline by ID (default: the last pipeline of a git branch)
JOB_NAME select a GitLab CI pipeline job by name
NTH-JOB-OF-THAT-NAME select n-th GitLab CI pipeline job by this name (default: the last one)
PIPELINE-ID select a GitLab CI pipeline by ID (default: the
last pipeline of a git branch)
JOB-NAME select a GitLab CI pipeline job by name
NTH-JOB-OF-THAT-NAME select n-th GitLab CI pipeline job by this name
(default: the last one)

optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
-v, --verbose print more information
--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)
--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)
show the last pipeline of this git branch (default:
the currently checked out branch)


.. _python-gitlab: https://pypi.org/p/python-gitlab
Expand Down
29 changes: 28 additions & 1 deletion gitlab_trace.py
Expand Up @@ -4,6 +4,7 @@
"""

import argparse
import json
import subprocess
import sys
import urllib.parse
Expand All @@ -13,7 +14,7 @@
import gitlab


__version__ = '0.3.1.dev0'
__version__ = '0.4.0.dev0'
__author__ = "Marius Gedminas <marius@gedmin.as>"


Expand Down Expand Up @@ -72,6 +73,21 @@ def fmt_status(status: str) -> str:
return colors[status] + status + colorama.Style.RESET_ALL


def fmt_duration(duration: Optional[float]) -> str:
if duration is None:
return 'n/a'
m, s = divmod(round(duration), 60)
h, m = divmod(m, 60)
bits = []
if h:
bits.append(f"{h}h")
if m:
bits.append(f"{m}m")
if s or not bits:
bits.append(f"{s}s")
return " ".join(bits)


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

Expand All @@ -87,6 +103,10 @@ def main() -> None:
"-v", "--verbose", action="store_true",
help="print more information",
)
parser.add_argument(
"--debug", action="store_true",
help="print even more information, for debugging",
)
parser.add_argument(
"-g", "--gitlab", metavar="NAME",
help="select configuration section in ~/.python-gitlab.cfg",
Expand Down Expand Up @@ -179,6 +199,13 @@ def main() -> None:
sys.exit(0)

job = project.jobs.get(args.job)
if args.verbose:
info(f"Job created: {job.created_at}")
info(f"Job started: {job.started_at or 'not yet'}")
info(f"Job finished: {job.finished_at or 'not yet'}")
info(f"Job duration: {fmt_duration(job.duration)}")
if args.debug:
info(json.dumps(job.attributes, indent=2))
sys.stdout.buffer.write(job.trace())
sys.exit(0)

Expand Down
58 changes: 58 additions & 0 deletions tests.py
Expand Up @@ -78,6 +78,11 @@ def __init__(self, id, name, status):
self.id = str(id)
self.name = name
self.status = status
self.created_at = '2020-09-16T06:16:49.180Z'
self.started_at = '2020-09-16T06:16:51.066Z'
self.finished_at = None
self.duration = 42
self.attributes = {"json_attributes": "here"}

def trace(self):
return b'Hello, world!\n'
Expand Down Expand Up @@ -144,6 +149,22 @@ def test_fmt_status(status, expected):
assert gt.fmt_status(status) == expected


@pytest.mark.parametrize('duration, expected', [
(0, '0s'),
(1, '1s'),
(60, '1m'),
(61, '1m 1s'),
(3600, '1h'),
(3601, '1h 1s'),
(3660, '1h 1m'),
(3661, '1h 1m 1s'),
(958.767528, "15m 59s"),
(None, 'n/a'),
])
def test_fmt_duration(duration, expected):
assert gt.fmt_duration(duration) == expected


def test_main_help(monkeypatch):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--help'])
with pytest.raises(SystemExit):
Expand Down Expand Up @@ -314,3 +335,40 @@ def test_main_job_by_name_multiple_nth(monkeypatch, capsys):
assert stdout == textwrap.dedent("""\
Hello, world!
""")


def test_main_job_verbose(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3202', '-v'])
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
Job created: 2020-09-16T06:16:49.180Z
Job started: 2020-09-16T06:16:51.066Z
Job finished: not yet
Job duration: 42s
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")


def test_main_job_debug(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3202', '--debug'])
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
{
"json_attributes": "here"
}
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")

0 comments on commit fd51876

Please sign in to comment.