85313cc1deb8f5ebe5c63601480a7496ee4c9a33
[platform/upstream/mesa.git] / .gitlab-ci / bin / gitlab_common.py
1 #!/usr/bin/env python3
2 # Copyright © 2020 - 2022 Collabora Ltd.
3 # Authors:
4 #   Tomeu Vizoso <tomeu.vizoso@collabora.com>
5 #   David Heidelberg <david.heidelberg@collabora.com>
6 #
7 # SPDX-License-Identifier: MIT
8 '''Shared functions between the scripts.'''
9
10 import os
11 import time
12 from typing import Optional
13
14
15 def get_gitlab_project(glab, name: str):
16     """Finds a specified gitlab project for given user"""
17     glab.auth()
18     username = glab.user.username
19     return glab.projects.get(f"{username}/mesa")
20
21
22 def read_token(token_arg: Optional[str]) -> str:
23     """pick token from args or file"""
24     if token_arg:
25         return token_arg
26     return (
27         open(os.path.expanduser("~/.config/gitlab-token"), encoding="utf-8")
28         .readline()
29         .rstrip()
30     )
31
32
33 def wait_for_pipeline(project, sha: str):
34     """await until pipeline appears in Gitlab"""
35     print("⏲ for the pipeline to appear..", end="")
36     while True:
37         pipelines = project.pipelines.list(sha=sha)
38         if pipelines:
39             print("", flush=True)
40             return pipelines[0]
41         print("", end=".", flush=True)
42         time.sleep(1)