ClickHouse/tests/ci/docs_check.py

103 lines
3.8 KiB
Python
Raw Normal View History

2021-10-29 09:58:25 +00:00
#!/usr/bin/env python3
import logging
import subprocess
import os
import sys
from github import Github
from s3_helper import S3Helper
from pr_info import PRInfo, get_event
2021-10-29 09:58:25 +00:00
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
2021-11-12 12:36:25 +00:00
from commit_status_helper import post_commit_status, get_commit
2021-11-19 15:42:56 +00:00
from clickhouse_helper import ClickHouseHelper, 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-10-29 09:58:25 +00:00
2021-11-12 12:36:25 +00:00
NAME = "Docs Check (actions)"
2021-10-29 09:58:25 +00:00
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
2021-11-19 14:47:04 +00:00
stopwatch = Stopwatch()
2021-10-29 09:58:25 +00:00
temp_path = os.path.join(os.getenv("TEMP_PATH"))
repo_path = os.path.join(os.getenv("REPO_COPY"))
pr_info = PRInfo(get_event(), need_changed_files=True)
2021-10-29 09:58:25 +00:00
gh = Github(get_best_robot_token())
2021-12-01 14:23:51 +00:00
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-10-29 09:58:25 +00:00
if not pr_info.has_changes_in_documentation():
logging.info ("No changes in documentation")
commit = get_commit(gh, pr_info.sha)
commit.create_status(context=NAME, description="No changes in docs", state="success")
sys.exit(0)
logging.info("Has changes in docs")
if not os.path.exists(temp_path):
os.makedirs(temp_path)
2021-11-12 12:13:13 +00:00
docker_image = get_image_with_version(temp_path, 'clickhouse/docs-check')
2021-10-29 09:58:25 +00:00
test_output = os.path.join(temp_path, 'docs_check_log')
if not os.path.exists(test_output):
os.makedirs(test_output)
2021-10-29 11:39:05 +00:00
cmd = f"docker run --cap-add=SYS_PTRACE --volume={repo_path}:/repo_path --volume={test_output}:/output_path {docker_image}"
2021-10-29 09:58:25 +00:00
run_log_path = os.path.join(test_output, 'runlog.log')
with open(run_log_path, 'w', encoding='utf-8') as log:
with subprocess.Popen(cmd, shell=True, stderr=log, stdout=log) as process:
retcode = process.wait()
2021-10-29 11:40:16 +00:00
if retcode == 0:
logging.info("Run successfully")
2021-10-29 15:01:29 +00:00
status = "success"
2021-10-29 17:40:28 +00:00
description = "Docs check passed"
2021-10-29 11:40:16 +00:00
else:
2021-10-29 17:40:28 +00:00
description = "Docs check failed (non zero exit code)"
2021-10-29 11:40:16 +00:00
status = "failure"
logging.info("Run failed")
2021-10-29 09:58:25 +00:00
subprocess.check_call(f"sudo chown -R ubuntu:ubuntu {temp_path}", shell=True)
files = os.listdir(test_output)
lines = []
additional_files = []
if not files:
logging.error("No output files after docs check")
description = "No output files after docs check"
status = "failure"
else:
for f in files:
path = os.path.join(test_output, f)
additional_files.append(path)
with open(path, 'r', encoding='utf-8') as check_file:
for line in check_file:
if "ERROR" in line:
lines.append((line.split(':')[-1], "FAIL"))
if lines:
status = "failure"
2021-10-29 17:40:28 +00:00
description = "Found errors in docs"
2021-10-29 13:57:47 +00:00
elif status != "failure":
2021-10-29 09:58:25 +00:00
lines.append(("No errors found", "OK"))
2021-10-29 13:57:47 +00:00
else:
lines.append(("Non zero exit code", "FAIL"))
2021-10-29 09:58:25 +00:00
s3_helper = S3Helper('https://s3.amazonaws.com')
2021-11-19 14:47:04 +00:00
ch_helper = ClickHouseHelper()
2021-10-29 09:58:25 +00:00
2021-11-12 11:39:00 +00:00
report_url = upload_results(s3_helper, pr_info.number, pr_info.sha, lines, additional_files, NAME)
2021-10-29 09:58:25 +00:00
print("::notice ::Report url: {report_url}")
2021-11-12 12:36:25 +00:00
post_commit_status(gh, pr_info.sha, NAME, description, status, report_url)
2021-11-19 14:47:04 +00:00
prepared_events = prepare_tests_results_for_clickhouse(pr_info, lines, status, stopwatch.duration_seconds, stopwatch.start_time_str, report_url, NAME)
ch_helper.insert_events_into(db="gh-data", table="checks", events=prepared_events)