Newer
Older
#!/usr/bin/env python3
import argparse
import sys
from gitlab import Gitlab, GitlabJobRetryError
from get_integration_sources import get_integration_sources
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from get_merge_requests import get_merge_requests
from retrigger_pipeline_jobs import retrigger_pipeline_jobs
def main():
"""
Retrigger a given job in all open MRs of projects in the given group with a target
branch that is configured for automatic integration into the given manifest project
and branch.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--gitlab-url",
help="""URL to the GitLab instance""",
dest="gitlab_url",
required=True,
)
parser.add_argument(
"--token",
help="""GitLab REST API private access token""",
dest="token",
required=True,
)
parser.add_argument(
"--manifest-project",
help="""name of the GitLab manifest project""",
dest="manifest_project",
required=True,
)
parser.add_argument(
"--manifest-branch",
help="""target integration branch""",
dest="manifest_branch",
required=True,
)
parser.add_argument(
"--group",
help="""group path or id to limit search scope to""",
dest="group",
required=True,
)
parser.add_argument(
"--job",
help="""job to retrigger""",
dest="job",
required=False,
default="check",
)
args, _ = parser.parse_known_args()
gitlab = Gitlab(args.gitlab_url, private_token=args.token)
group = gitlab.groups.get(args.group, retry_transient_errors=True)
integration_sources = get_integration_sources(
args.manifest_project, args.manifest_branch, group
)
failed = 0
for source in integration_sources:
project = gitlab.projects.get(source["project"], retry_transient_errors=True)
mrs = get_merge_requests(
project,
state="opened",
)
for mr in mrs:
# Get pipeline
if not mr.pipeline:
print("No pipeline in %s" % mr.web_url)
continue
pipeline = project.pipelines.get(
mr.pipeline.get("id"),
retry_transient_errors=True,
)
try:
jobs = retrigger_pipeline_jobs(
project,
pipeline,
args.job,
include_children=True,
)
if not jobs:
print(
"Could not find any jobs named '%s' with states %s in %s"
% (args.job, states, pipeline.web_url)
)
except GitlabJobRetryError as e:
print(
"ERROR: Could not retrigger job '%s' in %s: %s"
% (args.job, pipeline.web_url, e)
)
failed = failed + 1
continue
if failed > 0:
sys.exit(1)
if __name__ == "__main__":
main()