Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #46781 to 22.12: Reduce updates of Mergeable Check #46949

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions tests/ci/ast_fuzzer_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

from github import Github

from build_download_helper import get_build_name_for_check, read_build_urls
from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
from commit_status_helper import format_description, post_commit_status
from docker_pull_helper import get_image_with_version
from env_helper import (
GITHUB_REPOSITORY,
GITHUB_RUN_URL,
Expand All @@ -17,10 +21,6 @@
from s3_helper import S3Helper
from get_robot_token import get_best_robot_token
from pr_info import PRInfo
from build_download_helper import get_build_name_for_check, read_build_urls
from docker_pull_helper import get_image_with_version
from commit_status_helper import post_commit_status
from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
from stopwatch import Stopwatch
from rerun_helper import RerunHelper

Expand Down Expand Up @@ -151,11 +151,13 @@ def get_commit(gh, commit_sha):
with open(
os.path.join(workspace_path, "description.txt"), "r", encoding="utf-8"
) as desc_f:
description = desc_f.readline().rstrip("\n")[:140]
description = desc_f.readline().rstrip("\n")
except:
status = "failure"
description = "Task failed: $?=" + str(retcode)

description = format_description(description)

if "fail" in status:
test_result = [(description, "FAIL")]
else:
Expand Down
73 changes: 42 additions & 31 deletions tests/ci/commit_status_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import csv
import os
import time
from typing import List
from typing import List, Literal
import logging

from ci_config import CI_CONFIG, REQUIRED_CHECKS
Expand All @@ -15,6 +15,7 @@

RETRY = 5
CommitStatuses = List[CommitStatus]
MERGEABLE_NAME = "Mergeable Check"


def override_status(status: str, check_name: str, invert: bool = False) -> str:
Expand Down Expand Up @@ -102,59 +103,69 @@ def post_labels(gh: Github, pr_info: PRInfo, labels_names: List[str]) -> None:
pull_request.add_to_labels(label)


def fail_mergeable_check(commit: Commit, description: str) -> None:
commit.create_status(
context="Mergeable Check",
description=description,
state="failure",
target_url=GITHUB_RUN_URL,
)
def format_description(description: str) -> str:
if len(description) > 140:
description = description[:137] + "..."
return description


def reset_mergeable_check(commit: Commit, description: str = "") -> None:
def set_mergeable_check(
commit: Commit,
description: str = "",
state: Literal["success", "failure"] = "success",
) -> None:
commit.create_status(
context="Mergeable Check",
context=MERGEABLE_NAME,
description=description,
state="success",
state=state,
target_url=GITHUB_RUN_URL,
)


def update_mergeable_check(gh: Github, pr_info: PRInfo, check_name: str) -> None:
if SKIP_MERGEABLE_CHECK_LABEL in pr_info.labels:
not_run = (
pr_info.labels.intersection({SKIP_MERGEABLE_CHECK_LABEL, "release"})
or check_name not in REQUIRED_CHECKS
or pr_info.release_pr
or pr_info.number == 0
)
if not_run:
# Let's avoid unnecessary work
return

logging.info("Update Mergeable Check by %s", check_name)

commit = get_commit(gh, pr_info.sha)
checks = {
check.context: check.state
for check in filter(
lambda check: (check.context in REQUIRED_CHECKS),
# get_statuses() returns generator, which cannot be reversed - we need comprehension
# pylint: disable=unnecessary-comprehension
reversed([status for status in commit.get_statuses()]),
)
}
statuses = get_commit_filtered_statuses(commit)

required_checks = [
status for status in statuses if status.context in REQUIRED_CHECKS
]

mergeable_status = None
for status in statuses:
if status.context == MERGEABLE_NAME:
mergeable_status = status
break

success = []
fail = []
for name, state in checks.items():
if state == "success":
success.append(name)
for status in required_checks:
if status.state == "success":
success.append(status.context)
else:
fail.append(name)
fail.append(status.context)

if fail:
description = "failed: " + ", ".join(fail)
if success:
description += "; succeeded: " + ", ".join(success)
if len(description) > 140:
description = description[:137] + "..."
fail_mergeable_check(commit, description)
description = format_description(description)
if mergeable_status is None or mergeable_status.description != description:
set_mergeable_check(commit, description, "failure")
return

description = ", ".join(success)
if len(description) > 140:
description = description[:137] + "..."
reset_mergeable_check(commit, description)
description = format_description(description)
if mergeable_status is None or mergeable_status.description != description:
set_mergeable_check(commit, description)
5 changes: 2 additions & 3 deletions tests/ci/docker_images_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from github import Github

from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
from commit_status_helper import post_commit_status
from commit_status_helper import format_description, post_commit_status
from env_helper import GITHUB_WORKSPACE, RUNNER_TEMP, GITHUB_RUN_URL
from get_robot_token import get_best_robot_token, get_parameter_from_ssm
from pr_info import PRInfo
Expand Down Expand Up @@ -458,8 +458,7 @@ def main():
else:
description = "Nothing to update"

if len(description) >= 140:
description = description[:136] + "..."
description = format_description(description)

with open(changed_json, "w", encoding="utf-8") as images_file:
json.dump(result_images, images_file)
Expand Down
5 changes: 2 additions & 3 deletions tests/ci/docker_manifests_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from github import Github

from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
from commit_status_helper import post_commit_status
from commit_status_helper import format_description, post_commit_status
from env_helper import RUNNER_TEMP
from get_robot_token import get_best_robot_token, get_parameter_from_ssm
from pr_info import PRInfo
Expand Down Expand Up @@ -217,8 +217,7 @@ def main():
else:
description = "Nothing to update"

if len(description) >= 140:
description = description[:136] + "..."
description = format_description(description)

gh = Github(get_best_robot_token(), per_page=100)
post_commit_status(gh, pr_info.sha, NAME, description, status, url)
Expand Down
5 changes: 2 additions & 3 deletions tests/ci/docker_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from build_check import get_release_or_pr
from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
from commit_status_helper import post_commit_status
from commit_status_helper import format_description, post_commit_status
from docker_images_check import DockerImage
from env_helper import CI, GITHUB_RUN_URL, RUNNER_TEMP, S3_BUILDS_BUCKET, S3_DOWNLOAD
from get_robot_token import get_best_robot_token, get_parameter_from_ssm
Expand Down Expand Up @@ -346,8 +346,7 @@ def main():

description = f"Processed tags: {', '.join(tags)}"

if len(description) >= 140:
description = description[:136] + "..."
description = format_description(description)

gh = Github(get_best_robot_token(), per_page=100)
post_commit_status(gh, pr_info.sha, NAME, description, status, url)
Expand Down
9 changes: 6 additions & 3 deletions tests/ci/install_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
mark_flaky_tests,
prepare_tests_results_for_clickhouse,
)
from commit_status_helper import post_commit_status, update_mergeable_check
from commit_status_helper import (
format_description,
post_commit_status,
update_mergeable_check,
)
from compress_files import compress_fast
from docker_pull_helper import get_image_with_version, DockerImage
from env_helper import CI, TEMP_PATH as TEMP, REPORTS_PATH
Expand Down Expand Up @@ -348,8 +352,7 @@ def filter_artifacts(path: str) -> bool:
ch_helper = ClickHouseHelper()
mark_flaky_tests(ch_helper, args.check_name, test_results)

if len(description) >= 140:
description = description[:136] + "..."
description = format_description(description)

post_commit_status(gh, pr_info.sha, args.check_name, description, state, report_url)

Expand Down
10 changes: 6 additions & 4 deletions tests/ci/run_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from github import Github

from commit_status_helper import (
format_description,
get_commit,
post_labels,
remove_labels,
reset_mergeable_check,
set_mergeable_check,
)
from env_helper import GITHUB_RUN_URL, GITHUB_REPOSITORY, GITHUB_SERVER_URL
from get_robot_token import get_best_robot_token
Expand Down Expand Up @@ -157,7 +158,7 @@ def check_pr_description(pr_info: PRInfo) -> Tuple[str, str]:
+ second_category
+ "'"
)
return result_status[:140], category
return result_status, category

elif re.match(
r"(?i)^[#>*_ ]*(short\s*description|change\s*log\s*entry)", lines[i]
Expand Down Expand Up @@ -199,6 +200,7 @@ def check_pr_description(pr_info: PRInfo) -> Tuple[str, str]:

pr_info = PRInfo(need_orgs=True, pr_event_from_api=True, need_changed_files=True)
can_run, description, labels_state = should_run_checks_for_pr(pr_info)
description = format_description(description)
gh = Github(get_best_robot_token(), per_page=100)
commit = get_commit(gh, pr_info.sha)

Expand Down Expand Up @@ -231,7 +233,7 @@ def check_pr_description(pr_info: PRInfo) -> Tuple[str, str]:
if pr_labels_to_remove:
remove_labels(gh, pr_info, pr_labels_to_remove)

reset_mergeable_check(commit, "skipped")
set_mergeable_check(commit, "skipped")

if description_error:
print(
Expand All @@ -249,7 +251,7 @@ def check_pr_description(pr_info: PRInfo) -> Tuple[str, str]:
)
commit.create_status(
context=NAME,
description=description_error[:139],
description=format_description(description_error),
state="failure",
target_url=url,
)
Expand Down
12 changes: 7 additions & 5 deletions tests/ci/sqlancer_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

from github import Github

from build_download_helper import get_build_name_for_check, read_build_urls
from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
from commit_status_helper import format_description, post_commit_status
from docker_pull_helper import get_image_with_version
from env_helper import (
GITHUB_REPOSITORY,
GITHUB_RUN_URL,
Expand All @@ -18,10 +22,6 @@
from s3_helper import S3Helper
from get_robot_token import get_best_robot_token
from pr_info import PRInfo
from build_download_helper import get_build_name_for_check, read_build_urls
from docker_pull_helper import get_image_with_version
from commit_status_helper import post_commit_status
from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
from upload_result_helper import upload_results
from stopwatch import Stopwatch
from rerun_helper import RerunHelper
Expand Down Expand Up @@ -157,11 +157,13 @@ def get_commit(gh, commit_sha):
with open(
os.path.join(workspace_path, "description.txt"), "r", encoding="utf-8"
) as desc_f:
description = desc_f.readline().rstrip("\n")[:140]
description = desc_f.readline().rstrip("\n")
except:
# status = "failure"
description = "Task failed: $?=" + str(retcode)

description = format_description(description)

report_url = upload_results(
s3_helper,
pr_info.number,
Expand Down