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

1# Copyright © 2022 Idiap Research Institute <contact@idiap.ch> 

2# 

3# SPDX-License-Identifier: BSD-3-Clause 

4 

5import click 

6 

7from ...click import PreserveIndentCommand, verbosity_option 

8from ...logging import setup 

9 

10logger = setup(__name__.split(".", 1)[0]) 

11 

12 

13@click.command( 

14 cls=PreserveIndentCommand, 

15 epilog=""" 

16Examples: 

17 

18 1. List running jobs on any runners with tag "bob" (default): 

19 

20 .. code:: sh 

21 

22 devtool gitlab jobs -vv 

23 

24 

25 2. List running jobs on a runner with tag "macos": 

26 

27 .. code:: sh 

28 

29 devtool gitlab jobs -vv macos 

30 

31 

32 3. List running jobs on a runner with tag "macos" and "foo": 

33 

34 .. code:: sh 

35 

36 devtool gitlab jobs -vv macos foo 

37 

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 

54 

55 gl = get_gitlab_instance() 

56 

57 tags = tags or ["bob"] 

58 

59 # search for the runner(s) to affect 

60 runners = gl.runners.list(tag_list=tags) 

61 

62 if not runners: 

63 raise RuntimeError("Cannot find runner with tags = %s" % "|".join(tags)) 

64 

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 )