Skip to content

Commit 6cf8696

Browse files
committed
re run failed builds
1 parent 3b53929 commit 6cf8696

2 files changed

Lines changed: 74 additions & 14 deletions

File tree

scripts/trigger_auto_tests/main.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
AutoTestsInfo,
88
is_build_finished,
99
is_build_success,
10+
is_last_build_successful,
1011
is_shell_uses_package,
1112
trigger_auto_tests_build2,
1213
)
@@ -16,24 +17,59 @@
1617

1718

1819
def main(tc_user: str, tc_password: str):
19-
triggered_builds: dict[str, int] = {}
20-
builds_statuses: dict[str, bool] = {}
2120
errors = []
21+
triggered_builds = {}
2222
tc = TeamCity(TC_URL, auth=(tc_user, tc_password))
2323
tests_info = AutoTestsInfo.get_current(tc)
24+
if tests_info.re_run_builds:
25+
click.echo("Re run failed builds")
26+
else:
27+
click.echo("Run automated tests")
2428

2529
for shell_name in tests_info.supported_shells:
2630
try:
27-
if is_shell_uses_package(shell_name, tests_info):
28-
click.echo(f"{shell_name} Automation tests build triggering")
29-
build_id = trigger_auto_tests_build2(tc, shell_name, tests_info)
30-
triggered_builds[shell_name] = build_id
31-
else:
32-
click.echo(f"{shell_name} skipped tests")
31+
triggered_builds = _run_tests_for_shell(tc, shell_name, tests_info)
3332
except Exception as e:
3433
errors.append(e)
3534
click.echo(e, err=True)
3635

36+
builds_statuses, new_errors = _wait_build_finish(tc, triggered_builds)
37+
errors.extend(new_errors)
38+
39+
if errors:
40+
raise Exception("There were errors running automation tests.")
41+
return all(builds_statuses.values())
42+
43+
44+
def _run_tests_for_shell(
45+
tc: TeamCity, shell_name: str, tests_info: AutoTestsInfo
46+
) -> dict[str, int]:
47+
triggered_builds = {}
48+
if is_shell_uses_package(shell_name, tests_info):
49+
if tests_info.re_run_builds:
50+
if is_last_build_successful(tc, shell_name, tests_info):
51+
click.echo(
52+
f"{shell_name} last auto tests for this package and commit "
53+
f"id was successful, skip it"
54+
)
55+
else:
56+
click.echo(f"{shell_name} Re run automation tests")
57+
build_id = trigger_auto_tests_build2(tc, shell_name, tests_info)
58+
triggered_builds[shell_name] = build_id
59+
else:
60+
click.echo(f"{shell_name} Automation tests build triggering")
61+
build_id = trigger_auto_tests_build2(tc, shell_name, tests_info)
62+
triggered_builds[shell_name] = build_id
63+
else:
64+
click.echo(f"{shell_name} is not uses package with this version, skipped tests")
65+
return triggered_builds
66+
67+
68+
def _wait_build_finish(
69+
tc: TeamCity, triggered_builds: dict[str, int]
70+
) -> tuple[dict[str, bool], list[Exception]]:
71+
builds_statuses = {}
72+
errors = []
3773
while triggered_builds:
3874
time.sleep(BUILDS_CHECK_DELAY)
3975
for shell_name, build_id in triggered_builds.copy().items():
@@ -49,7 +85,4 @@ def main(tc_user: str, tc_password: str):
4985
except Exception as e:
5086
errors.append(e)
5187
click.echo(e, err=True)
52-
53-
if errors:
54-
raise Exception("There were errors running automation tests.")
55-
return all(builds_statuses.values())
88+
return builds_statuses, errors

scripts/trigger_auto_tests/utils/helpers.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from pathlib import Path, PosixPath
33

4-
from dohq_teamcity import Build, ModelProperty, Properties, TeamCity
4+
from dohq_teamcity import Build, BuildType, ModelProperty, Properties, TeamCity
55
from dohq_teamcity.rest import RESTClientObject
66
from github import Github, Repository, UnknownObjectException
77
from pip_download import PipDownloader
@@ -94,6 +94,26 @@ def trigger_auto_tests_build2(
9494
return build.id
9595

9696

97+
def is_last_build_successful(
98+
tc: TeamCity, shell_name: str, tests_info: "AutoTestsInfo"
99+
) -> bool:
100+
bt: BuildType = tc.projects.get_build_type(
101+
project_locator=f"id:{tests_info.automation_project_id}",
102+
bt_locator=f"name:{shell_name}",
103+
)
104+
for build in bt.get_builds():
105+
params = get_build_params(build)
106+
if (
107+
params["conf.triggered_by_project.url"] == tests_info.vcs_url
108+
and params["conf.triggered_by_project.commit_id"] == tests_info.commit_id
109+
):
110+
is_success = is_build_success(build)
111+
break
112+
else:
113+
is_success = False
114+
return is_success
115+
116+
97117
def is_build_finished(build: Build) -> bool:
98118
return build.state.lower() == "finished"
99119

@@ -108,6 +128,10 @@ def update_tc_csrf(tc: TeamCity):
108128
tc.rest_client = RESTClientObject(tc.configuration)
109129

110130

131+
def get_build_params(build: Build) -> dict:
132+
return {pr.name: pr.value for pr in build.get_build_actual_parameters()}
133+
134+
111135
class AutoTestsInfo(BaseModel):
112136
number: int
113137
params: dict
@@ -117,6 +141,7 @@ class AutoTestsInfo(BaseModel):
117141
name: str
118142
supported_shells: list[str]
119143
automation_project_id: str
144+
re_run_builds: bool
120145

121146
@classmethod
122147
def get_current(cls, tc: TeamCity) -> "AutoTestsInfo":
@@ -131,13 +156,14 @@ def get_current(cls, tc: TeamCity) -> "AutoTestsInfo":
131156
build = bt.get_builds(
132157
locator=f"number:{number},branch:(unspecified:any),running:true"
133158
)[0]
134-
params = {pr.name: pr.value for pr in build.get_build_actual_parameters()}
159+
params = get_build_params(build)
135160
vcs_url = params["vcsroot.url"]
136161
path = PosixPath(params["teamcity.build.checkoutDir"])
137162
supported_shells = list(
138163
filter(bool, map(str.strip, params["conf.shells"].split(";")))
139164
)
140165
automation_project_id = params["automation.project.id"]
166+
re_run_builds = params["re-run-builds"]
141167
return cls(
142168
number=int(number),
143169
params=params,
@@ -147,4 +173,5 @@ def get_current(cls, tc: TeamCity) -> "AutoTestsInfo":
147173
name=name,
148174
supported_shells=supported_shells,
149175
automation_project_id=automation_project_id,
176+
re_run_builds=re_run_builds,
150177
)

0 commit comments

Comments
 (0)