Add docs check to documentation

This commit is contained in:
alesapin 2021-10-29 12:58:25 +03:00
parent 0669b7248b
commit a2866de25c
5 changed files with 197 additions and 7 deletions

View File

@ -21,7 +21,6 @@ jobs:
python3 run_check.py python3 run_check.py
DockerHubPush: DockerHubPush:
needs: CheckLabels needs: CheckLabels
if: ${{ !contains(github.event.pull_request.labels.*.name, 'pr-documentation') && !contains(github.event.pull_request.labels.*.name, 'pr-doc-fix') }}
runs-on: [self-hosted, style-checker] runs-on: [self-hosted, style-checker]
steps: steps:
- name: Check out repository code - name: Check out repository code
@ -57,8 +56,33 @@ jobs:
run: | run: |
docker kill $(docker ps -q) ||: docker kill $(docker ps -q) ||:
sudo rm -fr $TEMP_PATH sudo rm -fr $TEMP_PATH
DocsCheck:
needs: DockerHubPush
runs-on: [self-hosted, style-checker]
steps:
- name: Download changed images
uses: actions/download-artifact@v2
with:
name: changed_images
path: ${{ runner.temp }}/docs_check
- name: Check out repository code
uses: actions/checkout@v2
- name: Style Check
env:
TEMP_PATH: ${{runner.temp}}/docs_check
REPO_COPY: ${{runner.temp}}/docs_check/ClickHouse
run: |
cp -r $GITHUB_WORKSPACE $TEMP_PATH
cd $REPO_COPY/tests/ci
python3 docs_check.py
- name: Cleanup
if: always()
run: |
docker kill $(docker ps -q) ||:
sudo rm -fr $TEMP_PATH
BuilderDebDebug: BuilderDebDebug:
needs: DockerHubPush needs: DockerHubPush
if: ${{ !contains(github.event.pull_request.labels.*.name, 'pr-documentation') && !contains(github.event.pull_request.labels.*.name, 'pr-doc-fix') }}
runs-on: [self-hosted, builder] runs-on: [self-hosted, builder]
steps: steps:
- name: Download changed images - name: Download changed images
@ -179,6 +203,7 @@ jobs:
sudo rm -fr $TEMP_PATH sudo rm -fr $TEMP_PATH
FastTest: FastTest:
needs: DockerHubPush needs: DockerHubPush
if: ${{ !contains(github.event.pull_request.labels.*.name, 'pr-documentation') && !contains(github.event.pull_request.labels.*.name, 'pr-doc-fix') }}
runs-on: [self-hosted, builder] runs-on: [self-hosted, builder]
steps: steps:
- name: Check out repository code - name: Check out repository code

View File

@ -1,4 +1,4 @@
# docker build -t yandex/clickhouse-docs-build . # docker build -t clickhouse/docs-build .
FROM ubuntu:20.04 FROM ubuntu:20.04
ENV LANG=C.UTF-8 ENV LANG=C.UTF-8
@ -44,7 +44,4 @@ certifi==2020.4.5.2 chardet==3.0.4 click==7.1.2 closure==20191111 cssmin==0.2.0
idna==2.10 Jinja2>=2.11.3 jinja2-highlight==0.6.1 jsmin==3.0.0 livereload==2.6.2 Markdown==3.3.2 MarkupSafe==2.0.1 \ idna==2.10 Jinja2>=2.11.3 jinja2-highlight==0.6.1 jsmin==3.0.0 livereload==2.6.2 Markdown==3.3.2 MarkupSafe==2.0.1 \
mkdocs==1.2.3 mkdocs-htmlproofer-plugin==0.0.3 mkdocs-macros-plugin==0.4.20 nltk==3.5 nose==1.3.7 protobuf==3.14.0 \ mkdocs==1.2.3 mkdocs-htmlproofer-plugin==0.0.3 mkdocs-macros-plugin==0.4.20 nltk==3.5 nose==1.3.7 protobuf==3.14.0 \
numpy==1.21.2 pymdown-extensions==9.0 python-slugify==4.0.1 PyYAML==5.4.1 repackage==0.7.3 requests==2.25.1 \ numpy==1.21.2 pymdown-extensions==9.0 python-slugify==4.0.1 PyYAML==5.4.1 repackage==0.7.3 requests==2.25.1 \
singledispatch==3.4.0.3 six==1.15.0 soupsieve==2.0.1 termcolor==1.1.0 tornado==6.1 Unidecode==1.1.1 urllib3>=1.26.5 singledispatch==3.4.0.3 six==1.15.0 soupsieve==2.0.1 termcolor==1.1.0 tornado==6.1 Unidecode==1.1.1 urllib3>=1.26.5
COPY * /

View File

@ -169,6 +169,11 @@
}, },
"docker/docs/builder": { "docker/docs/builder": {
"name": "clickhouse/docs-builder", "name": "clickhouse/docs-builder",
"dependent": [] "dependent": ["docker/docs/check"]
},
"docker/docs/check": {
"name": "clickhouse/docs-check",
"dependent": []
} }
} }

148
tests/ci/docs_check.py Normal file
View File

@ -0,0 +1,148 @@
#!/usr/bin/env python3
import logging
import subprocess
import os
import time
import json
import sys
from github import Github
from report import create_test_html_report
from s3_helper import S3Helper
from pr_info import PRInfo
from get_robot_token import get_best_robot_token
NAME = "Docs Check (actions)"
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
def upload_results(s3_client, pr_number, commit_sha, test_results, additional_files):
s3_path_prefix = f"{pr_number}/{commit_sha}/docs_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 = f"PR #{pr_number}"
branch_url = f"https://github.com/ClickHouse/ClickHouse/pull/{pr_number}"
commit_url = f"https://github.com/ClickHouse/ClickHouse/commit/{commit_sha}"
task_url = f"https://github.com/ClickHouse/ClickHouse/actions/runs/{os.getenv('GITHUB_RUN_ID')}"
raw_log_url = additional_urls[0]
additional_urls.pop(0)
html_report = create_test_html_report(NAME, test_results, raw_log_url, task_url, branch_url, branch_name, commit_url, additional_urls)
with open('report.html', 'w', encoding='utf-8') 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)
return url
def get_commit(gh, commit_sha):
repo = gh.get_repo(os.getenv("GITHUB_REPOSITORY", "ClickHouse/ClickHouse"))
commit = repo.get_commit(commit_sha)
return commit
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
temp_path = os.path.join(os.getenv("TEMP_PATH"))
repo_path = os.path.join(os.getenv("REPO_COPY"))
with open(os.getenv('GITHUB_EVENT_PATH'), 'r', encoding='utf-8') as event_file:
event = json.load(event_file)
pr_info = PRInfo(event, need_changed_files=True)
gh = Github(get_best_robot_token())
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)
images_path = os.path.join(temp_path, 'changed_images.json')
docker_image = 'clickhouse/docs-check'
if os.path.exists(images_path):
logging.info("Images file exists")
with open(images_path, 'r', encoding='utf-8') as images_fd:
images = json.load(images_fd)
logging.info("Got images %s", images)
if 'clickhouse/docs-check' in images:
docker_image += ':' + images['clickhouse/docs-check']
logging.info("Got docker image %s", docker_image)
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}")
test_output = os.path.join(temp_path, 'docs_check_log')
if not os.path.exists(test_output):
os.makedirs(test_output)
cmd = "docker run --cap-add=SYS_PTRACE --volume={repo_path}:/ClickHouse --volume={test_output}:/test_output {docker_image}"
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()
if retcode == 0:
logging.info("Run successfully")
status = "Success"
description = "Run Ok"
else:
description = "Run failed (non zero exit code)"
status = "failure"
logging.info("Run failed")
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"
description = "Found errors during docs check"
else:
lines.append(("No errors found", "OK"))
s3_helper = S3Helper('https://s3.amazonaws.com')
report_url = upload_results(s3_helper, pr_info.number, pr_info.sha, lines, additional_files)
print("::notice ::Report url: {report_url}")
commit = get_commit(gh, pr_info.sha)
commit.create_status(context=NAME, description=description, state=status, target_url=report_url)

View File

@ -2,8 +2,11 @@
import urllib import urllib
import requests import requests
from unidiff import PatchSet from unidiff import PatchSet
import os
DIFF_IN_DOCUMENTATION_EXT = [".html", ".md", ".yml", ".txt", ".css", ".js", ".xml", ".ico", ".conf", ".svg", ".png", ".jpg", ".py", ".sh"]
class PRInfo: class PRInfo:
def __init__(self, github_event, need_orgs=False, need_changed_files=False): def __init__(self, github_event, need_orgs=False, need_changed_files=False):
self.number = github_event['number'] self.number = github_event['number']
@ -37,6 +40,18 @@ class PRInfo:
'user_orgs': self.user_orgs, 'user_orgs': self.user_orgs,
} }
def has_changes_in_documentation(self):
# If the list wasn't built yet the best we can do is to
# assume that there were changes.
if self.changed_files is None or not self.changed_files:
return True
for f in self.changed_files:
_, ext = os.path.splitext(f)
if ext in DIFF_IN_DOCUMENTATION_EXT or 'Dockerfile' in f:
return True
return False
class FakePRInfo: class FakePRInfo:
def __init__(self): def __init__(self):