xref: /aosp_15_r20/external/mesa3d/bin/ci/marge_queue.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1#!/usr/bin/env python3
2# Copyright © 2020 - 2023 Collabora Ltd.
3# Authors:
4#   David Heidelberg <[email protected]>
5#
6# SPDX-License-Identifier: MIT
7
8"""
9Monitors Marge-bot and return number of assigned MRs.
10"""
11
12import argparse
13import time
14import sys
15from datetime import datetime, timezone
16from dateutil import parser
17
18import gitlab
19from gitlab_common import read_token, pretty_duration
20
21REFRESH_WAIT = 30
22MARGE_BOT_USER_ID = 9716
23
24
25def parse_args() -> None:
26    """Parse args"""
27    parse = argparse.ArgumentParser(
28        description="Tool to show merge requests assigned to the marge-bot",
29    )
30    parse.add_argument(
31        "--wait", action="store_true", help="wait until CI is free",
32    )
33    parse.add_argument(
34        "--token",
35        metavar="token",
36        help="force GitLab token, otherwise it's read from ~/.config/gitlab-token",
37    )
38    return parse.parse_args()
39
40
41if __name__ == "__main__":
42    args = parse_args()
43    token = read_token(args.token)
44    gl = gitlab.Gitlab(url="https://gitlab.freedesktop.org", private_token=token)
45
46    project = gl.projects.get("mesa/mesa")
47
48    while True:
49        mrs = project.mergerequests.list(assignee_id=MARGE_BOT_USER_ID, scope="all", state="opened", get_all=True)
50
51        jobs_num = len(mrs)
52        for mr in mrs:
53            updated = parser.parse(mr.updated_at)
54            now = datetime.now(timezone.utc)
55            diff = (now - updated).total_seconds()
56            print(
57                f"⛭ \u001b]8;;{mr.web_url}\u001b\\{mr.title}\u001b]8;;\u001b\\ ({pretty_duration(diff)})"
58            )
59
60        print("Job waiting: " + str(jobs_num))
61
62        if jobs_num == 0:
63            sys.exit(0)
64        if not args.wait:
65            sys.exit(min(jobs_num, 127))
66
67        time.sleep(REFRESH_WAIT)
68