Coverage for src/idiap_devtools/scripts/gitlab/jobs.py: 0%
20 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-22 14:46 +0200
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-22 14:46 +0200
1# Copyright © 2022 Idiap Research Institute <contact@idiap.ch>
2#
3# SPDX-License-Identifier: BSD-3-Clause
5import click
7from ...click import PreserveIndentCommand, verbosity_option
8from ...logging import setup
10logger = setup(__name__.split(".", 1)[0])
13@click.command(
14 cls=PreserveIndentCommand,
15 epilog="""
16Examples:
18 1. List running jobs on any runners with tag "bob" (default):
20 .. code:: sh
22 devtool gitlab jobs -vv
25 2. List running jobs on a runner with tag "macos":
27 .. code:: sh
29 devtool gitlab jobs -vv macos
32 3. List running jobs on a runner with tag "macos" and "foo":
34 .. code:: sh
36 devtool gitlab jobs -vv macos foo
38""",
39)
40@click.argument("tags", nargs=-1)
41@click.option(
42 "-s",
43 "--status",
44 type=click.Choice(["running", "success", "failed", "canceled"]),
45 default="running",
46 show_default=True,
47 help='The status of jobs we are searching for - one of "running", '
48 '"success", "failed" or "canceled"',
49)
50@verbosity_option(logger=logger)
51def jobs(status, tags, **_) -> None:
52 """List jobs on a given runner identified by description."""
53 from ...gitlab import get_gitlab_instance
55 gl = get_gitlab_instance()
57 tags = tags or ["bob"]
59 # search for the runner(s) to affect
60 runners = gl.runners.list(tag_list=tags)
62 if not runners:
63 raise RuntimeError("Cannot find runner with tags = %s" % "|".join(tags))
65 for runner in runners:
66 jobs = runner.jobs.list(all=True, status=status)
67 click.echo(
68 "Runner %s (id=%d) -- %d running"
69 % (
70 runner.attributes["description"],
71 runner.attributes["id"],
72 len(jobs),
73 )
74 )
75 for k in jobs:
76 click.echo(
77 "** job %d: %s (%s), since %s, by %s [%s]"
78 % (
79 k.id,
80 k.attributes["project"]["path_with_namespace"],
81 k.attributes["name"],
82 k.attributes["started_at"],
83 k.attributes["user"]["username"],
84 k.attributes["web_url"],
85 )
86 )