2021-09-10 11:52:21 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-07-19 13:17:05 +00:00
|
|
|
import argparse
|
2022-11-14 19:02:33 +00:00
|
|
|
import atexit
|
2022-07-19 11:31:43 +00:00
|
|
|
import csv
|
2021-09-10 11:52:21 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2022-07-19 11:31:43 +00:00
|
|
|
import subprocess
|
2021-12-01 14:23:51 +00:00
|
|
|
import sys
|
2023-01-03 14:23:19 +00:00
|
|
|
from pathlib import Path
|
2022-11-14 19:02:33 +00:00
|
|
|
from typing import List, Tuple
|
2021-12-01 14:23:51 +00:00
|
|
|
|
2023-12-18 12:21:13 +00:00
|
|
|
from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
|
2023-04-18 14:58:17 +00:00
|
|
|
from commit_status_helper import (
|
|
|
|
RerunHelper,
|
|
|
|
get_commit,
|
|
|
|
post_commit_status,
|
|
|
|
update_mergeable_check,
|
|
|
|
)
|
2023-12-18 12:21:13 +00:00
|
|
|
from docker_images_helper import get_docker_image, pull_image
|
2023-12-18 08:07:22 +00:00
|
|
|
from env_helper import REPO_COPY, TEMP_PATH
|
2022-07-19 11:31:43 +00:00
|
|
|
from get_robot_token import get_best_robot_token
|
2023-12-18 08:07:22 +00:00
|
|
|
from git_helper import GIT_PREFIX, git_runner
|
2023-12-18 12:21:13 +00:00
|
|
|
from github_helper import GitHub
|
2022-07-19 11:31:43 +00:00
|
|
|
from pr_info import PRInfo
|
2023-01-03 14:23:19 +00:00
|
|
|
from report import TestResults, read_test_results
|
2022-07-19 11:31:43 +00:00
|
|
|
from s3_helper import S3Helper
|
2022-07-19 13:17:05 +00:00
|
|
|
from ssh import SSHKey
|
2022-07-19 11:31:43 +00:00
|
|
|
from stopwatch import Stopwatch
|
|
|
|
from upload_result_helper import upload_results
|
2021-09-10 14:27:03 +00:00
|
|
|
|
2022-07-21 11:10:22 +00:00
|
|
|
NAME = "Style Check"
|
2021-09-15 09:02:38 +00:00
|
|
|
|
2021-09-10 14:27:03 +00:00
|
|
|
|
2022-11-14 19:02:33 +00:00
|
|
|
def process_result(
|
2023-09-22 11:16:46 +00:00
|
|
|
result_directory: Path,
|
|
|
|
) -> Tuple[str, str, TestResults, List[Path]]:
|
2023-01-03 14:23:19 +00:00
|
|
|
test_results = [] # type: TestResults
|
2021-09-10 11:52:21 +00:00
|
|
|
additional_files = []
|
2023-09-22 11:16:46 +00:00
|
|
|
# Just upload all files from result_directory.
|
2022-01-13 19:52:02 +00:00
|
|
|
# If task provides processed results, then it's responsible
|
2023-09-22 11:16:46 +00:00
|
|
|
# for content of result_directory.
|
|
|
|
if result_directory.exists():
|
|
|
|
additional_files = [p for p in result_directory.iterdir() if p.is_file()]
|
2021-09-10 11:52:21 +00:00
|
|
|
|
2021-12-12 12:09:44 +00:00
|
|
|
status = []
|
2023-09-22 11:16:46 +00:00
|
|
|
status_path = result_directory / "check_status.tsv"
|
|
|
|
if status_path.exists():
|
2023-01-03 14:23:19 +00:00
|
|
|
logging.info("Found check_status.tsv")
|
2022-01-13 19:52:02 +00:00
|
|
|
with open(status_path, "r", encoding="utf-8") as status_file:
|
|
|
|
status = list(csv.reader(status_file, delimiter="\t"))
|
2021-09-10 11:52:21 +00:00
|
|
|
if len(status) != 1 or len(status[0]) != 2:
|
2023-09-22 11:16:46 +00:00
|
|
|
logging.info("Files in result folder %s", os.listdir(result_directory))
|
2021-09-10 11:52:21 +00:00
|
|
|
return "error", "Invalid check_status.tsv", test_results, additional_files
|
|
|
|
state, description = status[0][0], status[0][1]
|
|
|
|
|
|
|
|
try:
|
2023-09-22 11:16:46 +00:00
|
|
|
results_path = result_directory / "test_results.tsv"
|
2023-01-03 14:23:19 +00:00
|
|
|
test_results = read_test_results(results_path)
|
2021-09-10 11:52:21 +00:00
|
|
|
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-11-29 14:17:17 +00:00
|
|
|
|
2022-07-19 13:17:05 +00:00
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser("Check and report style issues in the repository")
|
|
|
|
parser.add_argument("--push", default=True, help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-push",
|
|
|
|
action="store_false",
|
|
|
|
dest="push",
|
|
|
|
help="do not commit and push automatic fixes",
|
|
|
|
default=argparse.SUPPRESS,
|
|
|
|
)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2022-11-14 19:02:33 +00:00
|
|
|
def checkout_head(pr_info: PRInfo) -> None:
|
2022-07-19 13:17:05 +00:00
|
|
|
# It works ONLY for PRs, and only over ssh, so either
|
|
|
|
# ROBOT_CLICKHOUSE_SSH_KEY should be set or ssh-agent should work
|
|
|
|
assert pr_info.number
|
|
|
|
if not pr_info.head_name == pr_info.base_name:
|
|
|
|
# We can't push to forks, sorry folks
|
|
|
|
return
|
|
|
|
remote_url = pr_info.event["pull_request"]["base"]["repo"]["ssh_url"]
|
|
|
|
fetch_cmd = (
|
2022-12-01 12:01:35 +00:00
|
|
|
f"{GIT_PREFIX} fetch --depth=1 "
|
2022-07-19 13:17:05 +00:00
|
|
|
f"{remote_url} {pr_info.head_ref}:head-{pr_info.head_ref}"
|
|
|
|
)
|
|
|
|
if os.getenv("ROBOT_CLICKHOUSE_SSH_KEY", ""):
|
|
|
|
with SSHKey("ROBOT_CLICKHOUSE_SSH_KEY"):
|
|
|
|
git_runner(fetch_cmd)
|
|
|
|
else:
|
|
|
|
git_runner(fetch_cmd)
|
|
|
|
git_runner(f"git checkout -f head-{pr_info.head_ref}")
|
|
|
|
|
|
|
|
|
2022-11-14 19:02:33 +00:00
|
|
|
def commit_push_staged(pr_info: PRInfo) -> None:
|
2022-07-19 13:17:05 +00:00
|
|
|
# It works ONLY for PRs, and only over ssh, so either
|
|
|
|
# ROBOT_CLICKHOUSE_SSH_KEY should be set or ssh-agent should work
|
|
|
|
assert pr_info.number
|
|
|
|
if not pr_info.head_name == pr_info.base_name:
|
|
|
|
# We can't push to forks, sorry folks
|
|
|
|
return
|
|
|
|
git_staged = git_runner("git diff --cached --name-only")
|
|
|
|
if not git_staged:
|
|
|
|
return
|
|
|
|
remote_url = pr_info.event["pull_request"]["base"]["repo"]["ssh_url"]
|
2022-12-01 12:01:35 +00:00
|
|
|
git_runner(f"{GIT_PREFIX} commit -m 'Automatic style fix'")
|
2022-07-19 13:17:05 +00:00
|
|
|
push_cmd = (
|
2022-12-01 12:01:35 +00:00
|
|
|
f"{GIT_PREFIX} push {remote_url} head-{pr_info.head_ref}:{pr_info.head_ref}"
|
2022-07-19 13:17:05 +00:00
|
|
|
)
|
|
|
|
if os.getenv("ROBOT_CLICKHOUSE_SSH_KEY", ""):
|
|
|
|
with SSHKey("ROBOT_CLICKHOUSE_SSH_KEY"):
|
|
|
|
git_runner(push_cmd)
|
|
|
|
else:
|
|
|
|
git_runner(push_cmd)
|
|
|
|
|
|
|
|
|
2023-12-18 12:21:13 +00:00
|
|
|
def checkout_last_ref(pr_info: PRInfo) -> None:
|
|
|
|
# Checkout the merge commit back to avoid special effects
|
|
|
|
assert pr_info.number
|
|
|
|
if not pr_info.head_name == pr_info.base_name:
|
|
|
|
# We can't push to forks, sorry folks
|
|
|
|
return
|
|
|
|
git_runner("git checkout -f -")
|
|
|
|
|
|
|
|
|
2023-01-03 14:23:19 +00:00
|
|
|
def main():
|
2021-09-10 14:27:03 +00:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
2022-07-19 13:17:05 +00:00
|
|
|
logging.getLogger("git_helper").setLevel(logging.DEBUG)
|
|
|
|
args = parse_args()
|
2021-11-19 14:47:04 +00:00
|
|
|
|
|
|
|
stopwatch = Stopwatch()
|
|
|
|
|
2023-11-10 20:16:54 +00:00
|
|
|
repo_path = Path(REPO_COPY)
|
2023-10-04 12:28:38 +00:00
|
|
|
temp_path = Path(TEMP_PATH)
|
2023-09-22 11:16:46 +00:00
|
|
|
temp_path.mkdir(parents=True, exist_ok=True)
|
2021-09-15 12:59:39 +00:00
|
|
|
|
2021-11-26 14:00:09 +00:00
|
|
|
pr_info = PRInfo()
|
2023-12-15 14:48:01 +00:00
|
|
|
gh = GitHub(get_best_robot_token(), create_cache_dir=False)
|
|
|
|
commit = get_commit(gh, pr_info.sha)
|
|
|
|
|
2023-12-21 10:53:01 +00:00
|
|
|
atexit.register(update_mergeable_check, commit, pr_info, NAME)
|
2022-07-30 05:07:22 +00:00
|
|
|
|
2023-04-18 14:58:17 +00:00
|
|
|
rerun_helper = RerunHelper(commit, NAME)
|
2021-12-01 14:23:51 +00:00
|
|
|
if rerun_helper.is_already_finished_by_status():
|
|
|
|
logging.info("Check is already finished according to github status, exiting")
|
2022-07-21 16:25:22 +00:00
|
|
|
# Finish with the same code as previous
|
|
|
|
state = rerun_helper.get_finished_status().state # type: ignore
|
|
|
|
# state == "success" -> code = 0
|
|
|
|
code = int(state != "success")
|
|
|
|
sys.exit(code)
|
2021-12-01 14:23:51 +00:00
|
|
|
|
2022-08-11 13:01:32 +00:00
|
|
|
s3_helper = S3Helper()
|
2021-09-10 11:52:21 +00:00
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
IMAGE_NAME = "clickhouse/style-test"
|
|
|
|
image = pull_image(get_docker_image(IMAGE_NAME))
|
2022-02-03 13:06:21 +00:00
|
|
|
cmd = (
|
2022-01-13 19:52:02 +00:00
|
|
|
f"docker run -u $(id -u ${{USER}}):$(id -g ${{USER}}) --cap-add=SYS_PTRACE "
|
|
|
|
f"--volume={repo_path}:/ClickHouse --volume={temp_path}:/test_output "
|
2023-12-18 08:07:22 +00:00
|
|
|
f"{image}"
|
2022-02-03 13:06:21 +00:00
|
|
|
)
|
|
|
|
|
2023-12-18 12:21:13 +00:00
|
|
|
if args.push:
|
|
|
|
checkout_head(pr_info)
|
|
|
|
|
2022-02-03 13:06:21 +00:00
|
|
|
logging.info("Is going to run the command: %s", cmd)
|
|
|
|
subprocess.check_call(
|
|
|
|
cmd,
|
2022-01-13 19:52:02 +00:00
|
|
|
shell=True,
|
|
|
|
)
|
2022-02-03 13:06:21 +00:00
|
|
|
|
2022-07-19 13:17:05 +00:00
|
|
|
if args.push:
|
|
|
|
commit_push_staged(pr_info)
|
2023-12-18 12:21:13 +00:00
|
|
|
checkout_last_ref(pr_info)
|
2022-07-19 13:17:05 +00:00
|
|
|
|
2021-09-15 10:50:30 +00:00
|
|
|
state, description, test_results, additional_files = process_result(temp_path)
|
2021-11-19 14:47:04 +00:00
|
|
|
ch_helper = ClickHouseHelper()
|
|
|
|
|
2022-01-13 19:52:02 +00:00
|
|
|
report_url = upload_results(
|
|
|
|
s3_helper, pr_info.number, pr_info.sha, test_results, additional_files, NAME
|
|
|
|
)
|
2022-07-19 11:31:43 +00:00
|
|
|
print(f"::notice ::Report url: {report_url}")
|
2023-12-18 08:07:22 +00:00
|
|
|
post_commit_status(
|
|
|
|
commit, state, report_url, description, NAME, pr_info, dump_to_file=True
|
|
|
|
)
|
2021-11-19 14:47:04 +00:00
|
|
|
|
2022-01-13 19:52:02 +00:00
|
|
|
prepared_events = prepare_tests_results_for_clickhouse(
|
|
|
|
pr_info,
|
|
|
|
test_results,
|
|
|
|
state,
|
|
|
|
stopwatch.duration_seconds,
|
|
|
|
stopwatch.start_time_str,
|
|
|
|
report_url,
|
|
|
|
NAME,
|
|
|
|
)
|
2022-03-29 19:06:50 +00:00
|
|
|
ch_helper.insert_events_into(db="default", table="checks", events=prepared_events)
|
2022-03-29 12:41:47 +00:00
|
|
|
|
2022-07-18 19:15:21 +00:00
|
|
|
if state in ["error", "failure"]:
|
2022-03-29 12:41:47 +00:00
|
|
|
sys.exit(1)
|
2023-01-03 14:23:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|