ClickHouse/tests/ci/style_check.py

137 lines
5.5 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-10 14:27:03 +00:00
2021-09-15 09:03:47 +00:00
NAME = "Style-Check"
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-10 14:27:03 +00:00
def upload_results(s3_client, pr_number, commit_sha, state, description, test_results, additional_files):
s3_path_prefix = f"{pr_number}/{commit_sha}/style_check"
additional_urls = process_logs(s3_client, additional_files, s3_path_prefix)
# Add link to help. Anchors in the docs must be adjusted accordingly.
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}"
task_url = f"https://github.com/ClickHouse/ClickHouse/actions/runs/{run_id}"
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-10 11:52:21 +00:00
def get_pr_url_from_ref(ref):
try:
return ref.split("/")[2]
except:
return "master"
2021-09-15 10:47:25 +00:00
def get_parent_commit(gh, commit_sha):
2021-09-15 09:02:38 +00:00
repo = gh.get_repo(os.getenv("GITHUB_REPOSITORY", "ClickHouse/ClickHouse"))
commit = repo.get_commit(commit_sha)
2021-09-15 09:14:36 +00:00
parent = commit.parents[1]
2021-09-15 10:47:25 +00:00
return parent
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"))
print("CMD {}", cmd)
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-10 11:52:21 +00:00
repo_path = 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-10 11:52:21 +00:00
run_id = os.getenv("GITHUB_RUN_ID", 0)
commit_sha = os.getenv("GITHUB_SHA", 0)
ref = os.getenv("GITHUB_REF", "")
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 09:02:38 +00:00
gh = Github(os.getenv("GITHUB_TOKEN"))
2021-09-15 09:49:50 +00:00
2021-09-15 10:50:30 +00:00
docker_image_version = os.getenv("DOCKER_IMAGE_VERSION", "latest")
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-15 10:50:30 +00:00
if os.path.exists(temp_path):
shutil.rmtree(temp_path)
2021-09-10 14:38:34 +00:00
2021-09-15 10:50:30 +00:00
if not os.path.exists(temp_path):
os.makedirs(temp_path)
2021-09-10 11:52:21 +00:00
2021-09-15 12:12:59 +00:00
with open(os.getenv('GITHUB_EVENT_PATH'), 'r') as event_file:
print("Dumping event file")
print(json.load(event_file))
2021-09-15 10:56:37 +00:00
parent = get_parent_commit(gh, commit_sha)
2021-09-15 10:50:30 +00:00
subprocess.check_output(f"docker run --cap-add=SYS_PTRACE --volume={repo_path}:/ClickHouse --volume={temp_path}:/test_output clickhouse/style-test:{docker_image_version}", shell=True)
state, description, test_results, additional_files = process_result(temp_path)
2021-09-15 10:56:37 +00:00
report_url = upload_results(s3_helper, get_pr_url_from_ref(ref), parent.sha, state, description, test_results, additional_files)
parent.create_status(context=NAME, description=description, state=state, target_url=report_url)