ClickHouse/tests/ci/style_check.py

135 lines
5.6 KiB
Python
Raw Normal View History

2021-09-10 11:52:21 +00:00
#!/usr/bin/env python3
2021-09-15 09:02:38 +00:00
from github import Github
2021-09-10 11:52:21 +00:00
from report import create_test_html_report
2021-09-10 14:38:34 +00:00
import shutil
2021-09-10 11:52:21 +00:00
import logging
import subprocess
import os
import csv
2021-09-10 14:27:03 +00:00
from s3_helper import S3Helper
2021-09-15 09:23:02 +00:00
import time
2021-09-15 12:12:59 +00:00
import json
2021-09-15 12:59:39 +00:00
from pr_info import PRInfo
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
def process_logs(s3_client, additional_logs, s3_path_prefix):
additional_urls = []
for log_path in additional_logs:
if log_path:
additional_urls.append(
s3_client.upload_test_report_to_s3(
log_path,
s3_path_prefix + "/" + os.path.basename(log_path)))
return additional_urls
2021-09-10 11:52:21 +00:00
def process_result(result_folder):
test_results = []
additional_files = []
# Just upload all files from result_folder.
# If task provides processed results, then it's responsible for content of result_folder.
if os.path.exists(result_folder):
test_files = [f for f in os.listdir(result_folder) if os.path.isfile(os.path.join(result_folder, f))]
additional_files = [os.path.join(result_folder, f) for f in test_files]
status_path = os.path.join(result_folder, "check_status.tsv")
logging.info("Found test_results.tsv")
status = list(csv.reader(open(status_path, 'r'), delimiter='\t'))
if len(status) != 1 or len(status[0]) != 2:
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")
test_results = list(csv.reader(open(results_path, 'r'), delimiter='\t'))
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-15 16:32:17 +00:00
def upload_results(s3_client, pr_number, commit_sha, test_results, additional_files):
2021-09-10 14:27:03 +00:00
s3_path_prefix = f"{pr_number}/{commit_sha}/style_check"
additional_urls = process_logs(s3_client, additional_files, s3_path_prefix)
branch_url = "https://github.com/ClickHouse/ClickHouse/commits/master"
branch_name = "master"
if pr_number != 0:
branch_name = "PR #{}".format(pr_number)
branch_url = "https://github.com/ClickHouse/ClickHouse/pull/" + str(pr_number)
commit_url = f"https://github.com/ClickHouse/ClickHouse/commit/{commit_sha}"
2021-09-15 12:59:39 +00:00
task_url = f"https://github.com/ClickHouse/ClickHouse/actions/runs/{os.getenv('GITHUB_RUN_ID')}"
2021-09-10 14:27:03 +00:00
raw_log_url = additional_urls[0]
additional_urls.pop(0)
2021-09-15 10:56:37 +00:00
html_report = create_test_html_report(NAME, test_results, raw_log_url, task_url, branch_url, branch_name, commit_url, additional_urls)
2021-09-10 14:27:03 +00:00
with open('report.html', 'w') as f:
f.write(html_report)
url = s3_client.upload_test_report_to_s3('report.html', s3_path_prefix + ".html")
logging.info("Search result in url %s", url)
2021-09-15 09:02:38 +00:00
return url
2021-09-10 14:27:03 +00:00
2021-09-15 09:37:12 +00:00
def update_check_with_curl(check_id):
2021-09-15 10:02:30 +00:00
cmd_template = ("curl -v --request PATCH --url https://api.github.com/repos/ClickHouse/ClickHouse/check-runs/{} "
2021-09-15 09:37:12 +00:00
"--header 'authorization: Bearer {}' "
2021-09-15 10:14:19 +00:00
"--header 'Accept: application/vnd.github.v3+json' "
2021-09-15 09:37:12 +00:00
"--header 'content-type: application/json' "
2021-09-15 09:40:43 +00:00
"-d '{{\"name\" : \"hello-world-name\"}}'")
2021-09-15 09:37:12 +00:00
cmd = cmd_template.format(check_id, os.getenv("GITHUB_TOKEN"))
2021-09-15 09:42:48 +00:00
subprocess.check_call(cmd, shell=True)
2021-09-15 09:37:12 +00:00
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-09-17 08:20:56 +00:00
repo_path = os.path.join(os.getenv("GITHUB_WORKSPACE", os.path.abspath("../../")))
2021-09-10 14:38:34 +00:00
temp_path = os.path.join(os.getenv("RUNNER_TEMP", os.path.abspath("./temp")), 'style_check')
2021-09-15 12:59:39 +00:00
with open(os.getenv('GITHUB_EVENT_PATH'), 'r') as event_file:
event = json.load(event_file)
pr_info = PRInfo(event)
2021-09-15 18:52:37 +00:00
if not os.path.exists(temp_path):
os.makedirs(temp_path)
2021-09-10 14:27:03 +00:00
aws_secret_key_id = os.getenv("YANDEX_S3_ACCESS_KEY_ID", "")
aws_secret_key = os.getenv("YANDEX_S3_ACCESS_SECRET_KEY", "")
2021-09-15 18:26:48 +00:00
images_path = os.path.join(temp_path, 'changed_images.json')
2021-09-15 18:57:48 +00:00
docker_image = 'clickhouse/style-test'
2021-09-15 18:26:48 +00:00
if os.path.exists(images_path):
logging.info("Images file exists")
with open(images_path, 'r') as images_fd:
2021-09-15 18:54:20 +00:00
images = json.load(images_fd)
2021-09-15 18:26:48 +00:00
logging.info("Got images %s", images)
2021-09-15 18:57:48 +00:00
if 'clickhouse/style-test' in images:
docker_image += ':' + images['clickhouse/style-test']
2021-09-15 18:26:48 +00:00
logging.info("Got docker image %s", docker_image)
2021-09-30 13:20:08 +00:00
for i in range(10):
try:
subprocess.check_output(f"docker pull {docker_image}", shell=True)
break
except Exception as ex:
time.sleep(i * 3)
logging.info("Got execption pulling docker %s", ex)
else:
raise Exception(f"Cannot pull dockerhub for image {docker_image}")
2021-09-15 18:26:48 +00:00
2021-09-15 10:50:30 +00:00
if not aws_secret_key_id or not aws_secret_key:
logging.info("No secrets, will not upload anything to S3")
2021-09-10 14:27:03 +00:00
2021-09-15 10:50:30 +00:00
s3_helper = S3Helper('https://storage.yandexcloud.net', aws_access_key_id=aws_secret_key_id, aws_secret_access_key=aws_secret_key)
2021-09-10 11:52:21 +00:00
2021-09-17 08:20:56 +00:00
subprocess.check_output(f"docker run -u $(id -u ${{USER}}):$(id -g ${{USER}}) --cap-add=SYS_PTRACE --volume={repo_path}:/ClickHouse --volume={temp_path}:/test_output {docker_image}", shell=True)
2021-09-15 10:50:30 +00:00
state, description, test_results, additional_files = process_result(temp_path)
2021-09-15 16:32:17 +00:00
report_url = upload_results(s3_helper, pr_info.number, pr_info.sha, test_results, additional_files)
2021-09-16 10:39:36 +00:00
print("::notice ::Report url: {}".format(report_url))