ClickHouse/tests/ci/style_check.py

141 lines
4.5 KiB
Python
Raw Normal View History

2021-09-10 11:52:21 +00:00
#!/usr/bin/env python3
import logging
import subprocess
import os
import csv
2021-12-01 14:23:51 +00:00
import sys
from github import Github
2021-11-26 14:00:09 +00:00
2022-07-06 04:22:50 +00:00
from env_helper import (
RUNNER_TEMP,
GITHUB_WORKSPACE,
GITHUB_REPOSITORY,
2022-07-06 04:40:51 +00:00
GITHUB_SERVER_URL,
2022-07-06 04:22:50 +00:00
)
from s3_helper import S3Helper
2022-07-06 03:37:01 +00:00
from pr_info import PRInfo, SKIP_SIMPLE_CHECK_LABEL
from get_robot_token import get_best_robot_token
2021-11-12 11:39:00 +00:00
from upload_result_helper import upload_results
2021-11-12 12:13:13 +00:00
from docker_pull_helper import get_image_with_version
2022-07-06 04:22:50 +00:00
from commit_status_helper import post_commit_status, get_commit
2022-01-13 19:52:02 +00:00
from clickhouse_helper import (
ClickHouseHelper,
mark_flaky_tests,
prepare_tests_results_for_clickhouse,
)
2021-11-19 14:47:04 +00:00
from stopwatch import Stopwatch
2021-12-01 14:23:51 +00:00
from rerun_helper import RerunHelper
2021-09-10 14:27:03 +00:00
2021-09-15 12:59:39 +00:00
NAME = "Style Check (actions)"
2021-09-15 09:02:38 +00:00
2021-09-10 14:27:03 +00:00
2021-09-10 11:52:21 +00:00
def process_result(result_folder):
test_results = []
additional_files = []
# Just upload all files from result_folder.
2022-01-13 19:52:02 +00:00
# If task provides processed results, then it's responsible
# for content of result_folder.
2021-09-10 11:52:21 +00:00
if os.path.exists(result_folder):
2022-01-13 19:52:02 +00:00
test_files = [
f
for f in os.listdir(result_folder)
if os.path.isfile(os.path.join(result_folder, f))
]
2021-09-10 11:52:21 +00:00
additional_files = [os.path.join(result_folder, f) for f in test_files]
2021-12-12 12:09:44 +00:00
status = []
2021-09-10 11:52:21 +00:00
status_path = os.path.join(result_folder, "check_status.tsv")
2021-12-12 12:09:44 +00:00
if os.path.exists(status_path):
logging.info("Found test_results.tsv")
2022-01-13 19:52:02 +00:00
with open(status_path, "r", encoding="utf-8") as status_file:
status = list(csv.reader(status_file, delimiter="\t"))
2021-09-10 11:52:21 +00:00
if len(status) != 1 or len(status[0]) != 2:
logging.info("Files in result folder %s", os.listdir(result_folder))
2021-09-10 11:52:21 +00:00
return "error", "Invalid check_status.tsv", test_results, additional_files
state, description = status[0][0], status[0][1]
try:
results_path = os.path.join(result_folder, "test_results.tsv")
2022-01-13 19:52:02 +00:00
test_results = list(csv.reader(open(results_path, "r"), delimiter="\t"))
2021-09-10 11:52:21 +00:00
if len(test_results) == 0:
raise Exception("Empty results")
return state, description, test_results, additional_files
except Exception:
if state == "success":
state, description = "error", "Failed to read test_results.tsv"
return state, description, test_results, additional_files
2021-09-10 11:52:21 +00:00
if __name__ == "__main__":
2021-09-10 14:27:03 +00:00
logging.basicConfig(level=logging.INFO)
2021-11-19 14:47:04 +00:00
stopwatch = Stopwatch()
2021-11-26 14:00:09 +00:00
repo_path = GITHUB_WORKSPACE
2022-01-13 19:52:02 +00:00
temp_path = os.path.join(RUNNER_TEMP, "style_check")
2021-09-15 12:59:39 +00:00
2021-11-26 14:00:09 +00:00
pr_info = PRInfo()
2021-09-15 12:59:39 +00:00
2021-12-01 14:23:51 +00:00
gh = Github(get_best_robot_token())
rerun_helper = RerunHelper(gh, pr_info, NAME)
if rerun_helper.is_already_finished_by_status():
logging.info("Check is already finished according to github status, exiting")
sys.exit(0)
2021-09-15 18:52:37 +00:00
if not os.path.exists(temp_path):
os.makedirs(temp_path)
2022-01-13 19:52:02 +00:00
docker_image = get_image_with_version(temp_path, "clickhouse/style-test")
s3_helper = S3Helper("https://s3.amazonaws.com")
2021-09-10 11:52:21 +00:00
2022-02-03 13:06:21 +00:00
cmd = (
2022-01-13 19:52:02 +00:00
f"docker run -u $(id -u ${{USER}}):$(id -g ${{USER}}) --cap-add=SYS_PTRACE "
f"--volume={repo_path}:/ClickHouse --volume={temp_path}:/test_output "
2022-02-03 13:06:21 +00:00
f"{docker_image}"
)
logging.info("Is going to run the command: %s", cmd)
subprocess.check_call(
cmd,
2022-01-13 19:52:02 +00:00
shell=True,
)
2022-02-03 13:06:21 +00:00
2021-09-15 10:50:30 +00:00
state, description, test_results, additional_files = process_result(temp_path)
2021-11-19 14:47:04 +00:00
ch_helper = ClickHouseHelper()
mark_flaky_tests(ch_helper, NAME, test_results)
2022-01-13 19:52:02 +00:00
report_url = upload_results(
s3_helper, pr_info.number, pr_info.sha, test_results, additional_files, NAME
)
2021-09-16 10:39:36 +00:00
print("::notice ::Report url: {}".format(report_url))
2021-11-12 12:36:25 +00:00
post_commit_status(gh, pr_info.sha, NAME, description, state, report_url)
2021-11-19 14:47:04 +00:00
2022-01-13 19:52:02 +00:00
prepared_events = prepare_tests_results_for_clickhouse(
pr_info,
test_results,
state,
stopwatch.duration_seconds,
stopwatch.start_time_str,
report_url,
NAME,
)
2022-03-29 19:06:50 +00:00
ch_helper.insert_events_into(db="default", table="checks", events=prepared_events)
2022-03-29 12:41:47 +00:00
if state == "error":
2022-07-06 03:37:01 +00:00
if SKIP_SIMPLE_CHECK_LABEL not in pr_info.labels:
url = (
f"{GITHUB_SERVER_URL}/{GITHUB_REPOSITORY}/"
"blob/master/.github/PULL_REQUEST_TEMPLATE.md?plain=1"
)
commit = get_commit(gh, pr_info.sha)
commit.create_status(
2022-07-06 04:22:50 +00:00
context="Simple Check",
description=f"{NAME} failed",
state="failed",
2022-07-06 04:40:51 +00:00
target_url=url,
2022-07-06 03:37:01 +00:00
)
2022-03-29 12:41:47 +00:00
sys.exit(1)