2021-10-29 09:58:25 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-04-19 21:35:39 +00:00
|
|
|
import argparse
|
2021-10-29 09:58:25 +00:00
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-09-22 11:16:46 +00:00
|
|
|
from pathlib import Path
|
2023-01-03 14:23:19 +00:00
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
from docker_images_helper import get_docker_image, pull_image
|
|
|
|
from env_helper import TEMP_PATH, REPO_COPY
|
2023-01-03 14:23:19 +00:00
|
|
|
from pr_info import PRInfo
|
2024-01-04 15:35:09 +00:00
|
|
|
from report import JobReport, TestResults, TestResult
|
2023-01-03 14:23:19 +00:00
|
|
|
from stopwatch import Stopwatch
|
2021-12-03 08:33:16 +00:00
|
|
|
from tee_popen import TeePopen
|
2021-10-29 09:58:25 +00:00
|
|
|
|
|
|
|
|
2022-07-21 11:10:22 +00:00
|
|
|
NAME = "Docs Check"
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2023-01-03 14:23:19 +00:00
|
|
|
|
2023-09-22 11:16:46 +00:00
|
|
|
def parse_args() -> argparse.Namespace:
|
2022-04-19 21:35:39 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
description="Script to check the docs integrity",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--docs-branch",
|
|
|
|
default="",
|
|
|
|
help="a branch to get from docs repository",
|
|
|
|
)
|
2022-04-29 12:34:24 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--force",
|
|
|
|
action="store_true",
|
|
|
|
help="check the docs even if there no changes",
|
|
|
|
)
|
2023-09-22 11:16:46 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parse_args()
|
2022-04-19 21:35:39 +00:00
|
|
|
|
2021-10-29 09:58:25 +00:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
2021-11-19 14:47:04 +00:00
|
|
|
stopwatch = Stopwatch()
|
|
|
|
|
2023-09-22 11:16:46 +00:00
|
|
|
temp_path = Path(TEMP_PATH)
|
|
|
|
temp_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
repo_path = Path(REPO_COPY)
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2021-11-26 14:00:09 +00:00
|
|
|
pr_info = PRInfo(need_changed_files=True)
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2022-04-29 12:34:24 +00:00
|
|
|
if not pr_info.has_changes_in_documentation() and not args.force:
|
2021-11-26 14:00:09 +00:00
|
|
|
logging.info("No changes in documentation")
|
2024-01-04 15:35:09 +00:00
|
|
|
JobReport(
|
|
|
|
description="No changes in docs",
|
|
|
|
test_results=[],
|
|
|
|
status="success",
|
|
|
|
start_time=stopwatch.start_time_str,
|
|
|
|
duration=stopwatch.duration_seconds,
|
|
|
|
additional_files=[],
|
|
|
|
).dump()
|
2021-10-29 09:58:25 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2022-04-29 12:34:24 +00:00
|
|
|
if pr_info.has_changes_in_documentation():
|
|
|
|
logging.info("Has changes in docs")
|
|
|
|
elif args.force:
|
|
|
|
logging.info("Check the docs because of force flag")
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
docker_image = pull_image(get_docker_image("clickhouse/docs-builder"))
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2023-09-22 11:16:46 +00:00
|
|
|
test_output = temp_path / "docs_check_log"
|
|
|
|
test_output.mkdir(parents=True, exist_ok=True)
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2022-04-19 21:35:39 +00:00
|
|
|
cmd = (
|
|
|
|
f"docker run --cap-add=SYS_PTRACE -e GIT_DOCS_BRANCH={args.docs_branch} "
|
|
|
|
f"--volume={repo_path}:/ClickHouse --volume={test_output}:/output_path "
|
|
|
|
f"{docker_image}"
|
|
|
|
)
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2023-09-22 11:16:46 +00:00
|
|
|
run_log_path = test_output / "run.log"
|
2022-01-14 08:15:41 +00:00
|
|
|
logging.info("Running command: '%s'", cmd)
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2021-12-03 08:33:16 +00:00
|
|
|
with TeePopen(cmd, run_log_path) as process:
|
|
|
|
retcode = process.wait()
|
|
|
|
if retcode == 0:
|
|
|
|
logging.info("Run successfully")
|
|
|
|
status = "success"
|
|
|
|
description = "Docs check passed"
|
|
|
|
else:
|
|
|
|
description = "Docs check failed (non zero exit code)"
|
|
|
|
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)
|
2023-01-03 14:23:19 +00:00
|
|
|
test_results = [] # type: TestResults
|
2021-10-29 09:58:25 +00:00
|
|
|
additional_files = []
|
2023-09-22 11:16:46 +00:00
|
|
|
if not any(test_output.iterdir()):
|
2021-10-29 09:58:25 +00:00
|
|
|
logging.error("No output files after docs check")
|
|
|
|
description = "No output files after docs check"
|
|
|
|
status = "failure"
|
|
|
|
else:
|
2023-09-22 11:16:46 +00:00
|
|
|
for p in test_output.iterdir():
|
|
|
|
additional_files.append(p)
|
|
|
|
with open(p, "r", encoding="utf-8") as check_file:
|
2021-10-29 09:58:25 +00:00
|
|
|
for line in check_file:
|
|
|
|
if "ERROR" in line:
|
2023-01-03 14:23:19 +00:00
|
|
|
test_results.append(TestResult(line.split(":")[-1], "FAIL"))
|
|
|
|
if test_results:
|
2021-10-29 09:58:25 +00:00
|
|
|
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":
|
2023-01-03 14:23:19 +00:00
|
|
|
test_results.append(TestResult("No errors found", "OK"))
|
2021-10-29 13:57:47 +00:00
|
|
|
else:
|
2023-01-03 14:23:19 +00:00
|
|
|
test_results.append(TestResult("Non zero exit code", "FAIL"))
|
2021-10-29 09:58:25 +00:00
|
|
|
|
2024-01-04 15:35:09 +00:00
|
|
|
JobReport(
|
|
|
|
description=description,
|
|
|
|
test_results=test_results,
|
|
|
|
status=status,
|
|
|
|
start_time=stopwatch.start_time_str,
|
|
|
|
duration=stopwatch.duration_seconds,
|
|
|
|
additional_files=additional_files,
|
|
|
|
).dump()
|
2022-03-29 12:41:47 +00:00
|
|
|
|
2023-01-03 14:23:19 +00:00
|
|
|
if status == "failure":
|
2022-03-29 12:41:47 +00:00
|
|
|
sys.exit(1)
|
2023-01-03 14:23:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|