2021-09-15 12:59:39 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-09-15 13:04:29 +00:00
|
|
|
import logging
|
2024-08-03 08:40:12 +00:00
|
|
|
import re
|
2024-01-31 16:59:13 +00:00
|
|
|
import sys
|
2022-02-03 12:27:32 +00:00
|
|
|
from typing import Tuple
|
2021-11-26 14:00:09 +00:00
|
|
|
|
2024-10-17 15:06:42 +00:00
|
|
|
from build_download_helper import APIException
|
|
|
|
from ci_config import CI
|
2022-07-18 19:15:21 +00:00
|
|
|
from commit_status_helper import (
|
2023-04-25 15:02:41 +00:00
|
|
|
create_ci_report,
|
2023-02-23 14:21:19 +00:00
|
|
|
format_description,
|
2022-07-18 19:15:21 +00:00
|
|
|
get_commit,
|
2023-04-06 09:40:32 +00:00
|
|
|
post_commit_status,
|
2022-07-18 19:15:21 +00:00
|
|
|
post_labels,
|
|
|
|
remove_labels,
|
|
|
|
)
|
2023-04-24 13:41:43 +00:00
|
|
|
from env_helper import GITHUB_REPOSITORY, GITHUB_SERVER_URL
|
2021-10-20 11:25:14 +00:00
|
|
|
from get_robot_token import get_best_robot_token
|
2024-10-17 15:06:42 +00:00
|
|
|
from github_helper import GitHub
|
2024-01-29 17:13:32 +00:00
|
|
|
from pr_info import PRInfo
|
2024-06-06 05:53:41 +00:00
|
|
|
from report import FAILURE, PENDING, SUCCESS, StatusType
|
2024-07-17 15:19:56 +00:00
|
|
|
|
2021-09-15 12:59:39 +00:00
|
|
|
TRUSTED_ORG_IDS = {
|
|
|
|
54801242, # clickhouse
|
|
|
|
}
|
|
|
|
|
2024-08-03 08:40:12 +00:00
|
|
|
TRUSTED_CONTRIBUTORS = {
|
|
|
|
e.lower()
|
|
|
|
for e in [
|
|
|
|
"amosbird",
|
|
|
|
"azat", # SEMRush
|
|
|
|
"bharatnc", # Many contributions.
|
|
|
|
"cwurm", # ClickHouse, Inc
|
|
|
|
"den-crane", # Documentation contributor
|
|
|
|
"ildus", # adjust, ex-pgpro
|
|
|
|
"nvartolomei", # Seasoned contributor, CloudFlare
|
|
|
|
"taiyang-li",
|
|
|
|
"ucasFL", # Amos Bird's friend
|
|
|
|
"thomoco", # ClickHouse, Inc
|
|
|
|
"tonickkozlov", # Cloudflare
|
|
|
|
"tylerhannan", # ClickHouse, Inc
|
|
|
|
"tsolodov", # ClickHouse, Inc
|
|
|
|
"justindeguzman", # ClickHouse, Inc
|
|
|
|
"XuJia0210", # ClickHouse, Inc
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2024-07-17 15:19:56 +00:00
|
|
|
OK_SKIP_LABELS = {CI.Labels.RELEASE, CI.Labels.PR_BACKPORT, CI.Labels.PR_CHERRYPICK}
|
2021-09-15 12:59:39 +00:00
|
|
|
|
|
|
|
|
2024-08-03 08:40:12 +00:00
|
|
|
LABEL_CATEGORIES = {
|
|
|
|
"pr-backward-incompatible": ["Backward Incompatible Change"],
|
|
|
|
"pr-bugfix": [
|
|
|
|
"Bug Fix",
|
|
|
|
"Bug Fix (user-visible misbehavior in an official stable release)",
|
|
|
|
"Bug Fix (user-visible misbehaviour in official stable or prestable release)",
|
|
|
|
"Bug Fix (user-visible misbehavior in official stable or prestable release)",
|
|
|
|
],
|
2024-11-22 20:09:25 +00:00
|
|
|
"pr-critical-bugfix": [
|
|
|
|
"Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR"
|
|
|
|
],
|
2024-08-03 08:40:12 +00:00
|
|
|
"pr-build": [
|
|
|
|
"Build/Testing/Packaging Improvement",
|
|
|
|
"Build Improvement",
|
|
|
|
"Build/Testing Improvement",
|
|
|
|
"Build",
|
|
|
|
"Packaging Improvement",
|
|
|
|
],
|
|
|
|
"pr-documentation": [
|
|
|
|
"Documentation (changelog entry is not required)",
|
|
|
|
"Documentation",
|
|
|
|
],
|
|
|
|
"pr-feature": ["New Feature"],
|
|
|
|
"pr-improvement": ["Improvement"],
|
|
|
|
"pr-not-for-changelog": [
|
|
|
|
"Not for changelog (changelog entry is not required)",
|
|
|
|
"Not for changelog",
|
|
|
|
],
|
|
|
|
"pr-performance": ["Performance Improvement"],
|
|
|
|
"pr-ci": ["CI Fix or Improvement (changelog entry is not required)"],
|
|
|
|
}
|
|
|
|
|
|
|
|
CATEGORY_TO_LABEL = {
|
|
|
|
c: lb for lb, categories in LABEL_CATEGORIES.items() for c in categories
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def check_pr_description(pr_body: str, repo_name: str) -> Tuple[str, str]:
|
|
|
|
"""The function checks the body to being properly formatted according to
|
|
|
|
.github/PULL_REQUEST_TEMPLATE.md, if the first returned string is not empty,
|
|
|
|
then there is an error."""
|
|
|
|
lines = list(map(lambda x: x.strip(), pr_body.split("\n") if pr_body else []))
|
|
|
|
lines = [re.sub(r"\s+", " ", line) for line in lines]
|
|
|
|
|
|
|
|
# Check if body contains "Reverts ClickHouse/ClickHouse#36337"
|
|
|
|
if [True for line in lines if re.match(rf"\AReverts {repo_name}#[\d]+\Z", line)]:
|
|
|
|
return "", LABEL_CATEGORIES["pr-not-for-changelog"][0]
|
|
|
|
|
|
|
|
category = ""
|
|
|
|
entry = ""
|
|
|
|
description_error = ""
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
while i < len(lines):
|
|
|
|
if re.match(r"(?i)^[#>*_ ]*change\s*log\s*category", lines[i]):
|
|
|
|
i += 1
|
|
|
|
if i >= len(lines):
|
|
|
|
break
|
|
|
|
# Can have one empty line between header and the category
|
|
|
|
# itself. Filter it out.
|
|
|
|
if not lines[i]:
|
|
|
|
i += 1
|
|
|
|
if i >= len(lines):
|
|
|
|
break
|
|
|
|
category = re.sub(r"^[-*\s]*", "", lines[i])
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
# Should not have more than one category. Require empty line
|
|
|
|
# after the first found category.
|
|
|
|
if i >= len(lines):
|
|
|
|
break
|
|
|
|
if lines[i]:
|
|
|
|
second_category = re.sub(r"^[-*\s]*", "", lines[i])
|
|
|
|
description_error = (
|
|
|
|
"More than one changelog category specified: "
|
|
|
|
f"'{category}', '{second_category}'"
|
|
|
|
)
|
|
|
|
return description_error, category
|
|
|
|
|
|
|
|
elif re.match(
|
|
|
|
r"(?i)^[#>*_ ]*(short\s*description|change\s*log\s*entry)", lines[i]
|
|
|
|
):
|
|
|
|
i += 1
|
|
|
|
# Can have one empty line between header and the entry itself.
|
|
|
|
# Filter it out.
|
|
|
|
if i < len(lines) and not lines[i]:
|
|
|
|
i += 1
|
|
|
|
# All following lines until empty one are the changelog entry.
|
|
|
|
entry_lines = []
|
|
|
|
while i < len(lines) and lines[i]:
|
|
|
|
entry_lines.append(lines[i])
|
|
|
|
i += 1
|
|
|
|
entry = " ".join(entry_lines)
|
|
|
|
# Don't accept changelog entries like '...'.
|
|
|
|
entry = re.sub(r"[#>*_.\- ]", "", entry)
|
|
|
|
# Don't accept changelog entries like 'Close #12345'.
|
|
|
|
entry = re.sub(r"^[\w\-\s]{0,10}#?\d{5,6}\.?$", "", entry)
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if not category:
|
|
|
|
description_error = "Changelog category is empty"
|
|
|
|
# Filter out the PR categories that are not for changelog.
|
|
|
|
elif "(changelog entry is not required)" in category:
|
|
|
|
pass # to not check the rest of the conditions
|
|
|
|
elif category not in CATEGORY_TO_LABEL:
|
|
|
|
description_error, category = f"Category '{category}' is not valid", ""
|
|
|
|
elif not entry:
|
|
|
|
description_error = f"Changelog entry required for category '{category}'"
|
|
|
|
|
|
|
|
return description_error, category
|
|
|
|
|
|
|
|
|
2021-09-15 12:59:39 +00:00
|
|
|
def pr_is_by_trusted_user(pr_user_login, pr_user_orgs):
|
2024-08-03 08:40:12 +00:00
|
|
|
if pr_user_login.lower() in TRUSTED_CONTRIBUTORS:
|
2021-10-27 07:03:24 +00:00
|
|
|
logging.info("User '%s' is trusted", pr_user_login)
|
2021-09-15 12:59:39 +00:00
|
|
|
return True
|
|
|
|
|
2021-10-27 07:03:24 +00:00
|
|
|
logging.info("User '%s' is not trusted", pr_user_login)
|
2021-09-15 12:59:39 +00:00
|
|
|
|
|
|
|
for org_id in pr_user_orgs:
|
|
|
|
if org_id in TRUSTED_ORG_IDS:
|
2022-01-13 11:06:50 +00:00
|
|
|
logging.info(
|
|
|
|
"Org '%s' is trusted; will mark user %s as trusted",
|
|
|
|
org_id,
|
|
|
|
pr_user_login,
|
|
|
|
)
|
2021-09-15 12:59:39 +00:00
|
|
|
return True
|
2021-10-27 07:03:24 +00:00
|
|
|
logging.info("Org '%s' is not trusted", org_id)
|
2021-09-15 12:59:39 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
2022-01-13 11:06:50 +00:00
|
|
|
|
2021-09-15 12:59:39 +00:00
|
|
|
# Returns whether we should look into individual checks for this PR. If not, it
|
|
|
|
# can be skipped entirely.
|
2023-12-21 10:53:01 +00:00
|
|
|
# Returns can_run, description
|
|
|
|
def should_run_ci_for_pr(pr_info: PRInfo) -> Tuple[bool, str]:
|
2021-09-15 12:59:39 +00:00
|
|
|
# Consider the labels and whether the user is trusted.
|
2024-06-06 05:54:38 +00:00
|
|
|
logging.info("Got labels: %s", pr_info.labels)
|
2021-09-15 12:59:39 +00:00
|
|
|
|
2023-03-20 12:45:23 +00:00
|
|
|
if OK_SKIP_LABELS.intersection(pr_info.labels):
|
2023-12-21 10:53:01 +00:00
|
|
|
return True, "Don't try new checks for release/backports/cherry-picks"
|
2023-03-20 12:45:23 +00:00
|
|
|
|
2024-07-17 15:19:56 +00:00
|
|
|
if CI.Labels.CAN_BE_TESTED not in pr_info.labels and not pr_is_by_trusted_user(
|
2022-01-13 11:06:50 +00:00
|
|
|
pr_info.user_login, pr_info.user_orgs
|
|
|
|
):
|
2024-06-06 05:54:38 +00:00
|
|
|
logging.info(
|
|
|
|
"PRs by untrusted users need the '%s' label - "
|
|
|
|
"please contact a member of the core team",
|
2024-07-17 15:19:56 +00:00
|
|
|
CI.Labels.CAN_BE_TESTED,
|
2022-09-07 08:51:02 +00:00
|
|
|
)
|
2023-12-21 10:53:01 +00:00
|
|
|
return False, "Needs 'can be tested' label"
|
2021-09-15 12:59:39 +00:00
|
|
|
|
2023-12-21 10:53:01 +00:00
|
|
|
return True, "No special conditions apply"
|
2021-09-15 12:59:39 +00:00
|
|
|
|
2021-11-26 14:00:09 +00:00
|
|
|
|
2023-04-06 09:03:32 +00:00
|
|
|
def main():
|
2021-09-15 12:59:39 +00:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
2024-10-17 15:06:42 +00:00
|
|
|
fail_early = False
|
|
|
|
try:
|
|
|
|
pr_info = PRInfo(
|
|
|
|
need_orgs=True, pr_event_from_api=True, need_changed_files=True
|
|
|
|
)
|
|
|
|
except APIException as e:
|
|
|
|
logging.exception(
|
|
|
|
"Failed to receive the PRInfo, backport to a simple case and exit with error",
|
|
|
|
exc_info=e,
|
|
|
|
)
|
|
|
|
pr_info = PRInfo()
|
|
|
|
fail_early = True
|
|
|
|
|
2023-03-20 12:45:23 +00:00
|
|
|
# The case for special branches like backports and releases without created
|
|
|
|
# PRs, like merged backport branches that are reset immediately after merge
|
2024-10-17 15:06:42 +00:00
|
|
|
if pr_info.number == 0 or fail_early:
|
2023-03-20 12:45:23 +00:00
|
|
|
print("::notice ::Cannot run, no PR exists for the commit")
|
2024-10-17 15:06:42 +00:00
|
|
|
gh = GitHub(get_best_robot_token(), per_page=100)
|
|
|
|
commit = get_commit(gh, pr_info.sha)
|
|
|
|
post_commit_status(
|
|
|
|
commit,
|
|
|
|
FAILURE,
|
|
|
|
"",
|
|
|
|
"No PRs found for the commit, finished early",
|
|
|
|
CI.StatusNames.PR_CHECK,
|
|
|
|
pr_info,
|
|
|
|
)
|
2023-03-20 12:45:23 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2023-12-21 10:53:01 +00:00
|
|
|
can_run, description = should_run_ci_for_pr(pr_info)
|
2023-03-20 12:45:23 +00:00
|
|
|
if can_run and OK_SKIP_LABELS.intersection(pr_info.labels):
|
|
|
|
print("::notice :: Early finish the check, running in a special PR")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2023-02-27 12:11:17 +00:00
|
|
|
description = format_description(description)
|
2024-10-17 15:06:42 +00:00
|
|
|
gh = GitHub(get_best_robot_token(), per_page=100)
|
2021-10-20 11:25:14 +00:00
|
|
|
commit = get_commit(gh, pr_info.sha)
|
2024-06-06 05:53:41 +00:00
|
|
|
status = SUCCESS # type: StatusType
|
2022-01-13 11:08:31 +00:00
|
|
|
|
2024-08-03 08:40:12 +00:00
|
|
|
description_error, category = check_pr_description(pr_info.body, GITHUB_REPOSITORY)
|
2022-03-30 08:58:34 +00:00
|
|
|
pr_labels_to_add = []
|
|
|
|
pr_labels_to_remove = []
|
2022-03-29 17:50:06 +00:00
|
|
|
if (
|
2024-08-03 08:40:12 +00:00
|
|
|
category in CATEGORY_TO_LABEL
|
|
|
|
and CATEGORY_TO_LABEL[category] not in pr_info.labels
|
2022-03-29 17:50:06 +00:00
|
|
|
):
|
2024-08-03 08:40:12 +00:00
|
|
|
pr_labels_to_add.append(CATEGORY_TO_LABEL[category])
|
2022-03-30 08:58:34 +00:00
|
|
|
|
|
|
|
for label in pr_info.labels:
|
2022-03-30 09:19:11 +00:00
|
|
|
if (
|
2024-08-03 08:40:12 +00:00
|
|
|
label in CATEGORY_TO_LABEL.values()
|
|
|
|
and category in CATEGORY_TO_LABEL
|
|
|
|
and label != CATEGORY_TO_LABEL[category]
|
2022-03-30 09:19:11 +00:00
|
|
|
):
|
2022-03-30 08:58:34 +00:00
|
|
|
pr_labels_to_remove.append(label)
|
2022-03-29 13:48:57 +00:00
|
|
|
|
2022-03-29 17:28:18 +00:00
|
|
|
if pr_info.has_changes_in_submodules():
|
2024-07-17 15:19:56 +00:00
|
|
|
pr_labels_to_add.append(CI.Labels.SUBMODULE_CHANGED)
|
|
|
|
elif CI.Labels.SUBMODULE_CHANGED in pr_info.labels:
|
|
|
|
pr_labels_to_remove.append(CI.Labels.SUBMODULE_CHANGED)
|
2022-03-30 08:58:34 +00:00
|
|
|
|
2024-07-17 15:19:56 +00:00
|
|
|
if any(label in CI.Labels.AUTO_BACKPORT for label in pr_labels_to_add):
|
|
|
|
backport_labels = [CI.Labels.MUST_BACKPORT, CI.Labels.MUST_BACKPORT_CLOUD]
|
2024-02-27 16:33:46 +00:00
|
|
|
pr_labels_to_add += [
|
|
|
|
label for label in backport_labels if label not in pr_info.labels
|
|
|
|
]
|
|
|
|
print(
|
|
|
|
f"::notice :: Add backport labels [{backport_labels}] for a given PR category"
|
|
|
|
)
|
|
|
|
|
2024-06-06 05:54:38 +00:00
|
|
|
logging.info(
|
|
|
|
"Change labels: add %s, remove %s", pr_labels_to_add, pr_labels_to_remove
|
|
|
|
)
|
2022-03-30 08:58:34 +00:00
|
|
|
if pr_labels_to_add:
|
|
|
|
post_labels(gh, pr_info, pr_labels_to_add)
|
|
|
|
|
|
|
|
if pr_labels_to_remove:
|
|
|
|
remove_labels(gh, pr_info, pr_labels_to_remove)
|
2022-03-29 17:28:18 +00:00
|
|
|
|
2024-06-06 05:53:41 +00:00
|
|
|
# 1. Next three IFs are in a correct order. First - fatal error
|
2022-04-19 12:47:18 +00:00
|
|
|
if description_error:
|
2022-04-04 23:06:46 +00:00
|
|
|
print(
|
|
|
|
"::error ::Cannot run, PR description does not match the template: "
|
2022-04-19 12:47:18 +00:00
|
|
|
f"{description_error}"
|
2022-04-04 23:06:46 +00:00
|
|
|
)
|
2022-01-26 12:23:20 +00:00
|
|
|
logging.info(
|
2023-04-20 11:55:33 +00:00
|
|
|
"PR body doesn't match the template: (start)\n%s\n(end)\nReason: %s",
|
2022-04-04 23:06:46 +00:00
|
|
|
pr_info.body,
|
2022-04-19 12:47:18 +00:00
|
|
|
description_error,
|
2022-01-26 12:23:20 +00:00
|
|
|
)
|
2022-01-13 11:08:31 +00:00
|
|
|
url = (
|
|
|
|
f"{GITHUB_SERVER_URL}/{GITHUB_REPOSITORY}/"
|
|
|
|
"blob/master/.github/PULL_REQUEST_TEMPLATE.md?plain=1"
|
|
|
|
)
|
2024-06-06 05:53:41 +00:00
|
|
|
status = FAILURE
|
2023-04-06 09:40:32 +00:00
|
|
|
post_commit_status(
|
2023-04-20 11:55:33 +00:00
|
|
|
commit,
|
2024-06-06 05:53:41 +00:00
|
|
|
status,
|
2023-04-20 11:55:33 +00:00
|
|
|
url,
|
|
|
|
format_description(description_error),
|
2024-10-17 15:06:42 +00:00
|
|
|
CI.StatusNames.PR_CHECK,
|
2023-04-20 11:55:33 +00:00
|
|
|
pr_info,
|
2022-01-13 11:08:31 +00:00
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2024-06-06 05:53:41 +00:00
|
|
|
# 2. Then we check if the documentation is not created to fail the Mergeable check
|
2024-03-15 15:57:59 +00:00
|
|
|
if (
|
2024-07-17 15:19:56 +00:00
|
|
|
CI.Labels.PR_FEATURE in pr_info.labels
|
2024-03-15 15:57:59 +00:00
|
|
|
and not pr_info.has_changes_in_documentation()
|
|
|
|
):
|
2023-12-21 10:53:01 +00:00
|
|
|
print(
|
2024-07-17 15:19:56 +00:00
|
|
|
f"::error ::The '{CI.Labels.PR_FEATURE}' in the labels, "
|
2023-12-21 10:53:01 +00:00
|
|
|
"but there's no changed documentation"
|
2023-04-20 11:55:33 +00:00
|
|
|
)
|
2024-06-06 05:53:41 +00:00
|
|
|
status = FAILURE
|
2024-07-17 15:19:56 +00:00
|
|
|
description = f"expect adding docs for {CI.Labels.PR_FEATURE}"
|
2024-06-06 05:53:41 +00:00
|
|
|
# 3. But we allow the workflow to continue
|
2023-12-21 10:53:01 +00:00
|
|
|
|
2024-06-06 05:53:41 +00:00
|
|
|
# 4. And post only a single commit status on a failure
|
2023-12-21 10:53:01 +00:00
|
|
|
if not can_run:
|
2024-01-31 16:59:13 +00:00
|
|
|
post_commit_status(
|
|
|
|
commit,
|
2024-06-06 05:53:41 +00:00
|
|
|
status,
|
2024-01-31 16:59:13 +00:00
|
|
|
"",
|
|
|
|
description,
|
2024-10-17 15:06:42 +00:00
|
|
|
CI.StatusNames.PR_CHECK,
|
2024-01-31 16:59:13 +00:00
|
|
|
pr_info,
|
|
|
|
)
|
2024-06-06 05:54:38 +00:00
|
|
|
print("::error ::Cannot run")
|
2023-12-21 10:53:01 +00:00
|
|
|
sys.exit(1)
|
2024-02-28 12:37:07 +00:00
|
|
|
|
2024-06-06 05:53:41 +00:00
|
|
|
# The status for continue can be posted only one time, not more.
|
2024-02-28 11:22:19 +00:00
|
|
|
post_commit_status(
|
|
|
|
commit,
|
2024-06-06 05:53:41 +00:00
|
|
|
status,
|
2024-02-28 11:22:19 +00:00
|
|
|
"",
|
2024-06-06 05:53:41 +00:00
|
|
|
description,
|
2024-10-17 15:06:42 +00:00
|
|
|
CI.StatusNames.PR_CHECK,
|
2024-02-28 11:22:19 +00:00
|
|
|
pr_info,
|
|
|
|
)
|
2023-12-21 10:53:01 +00:00
|
|
|
|
|
|
|
ci_report_url = create_ci_report(pr_info, [])
|
|
|
|
print("::notice ::Can run")
|
2024-04-08 16:09:47 +00:00
|
|
|
|
2024-04-17 20:23:41 +00:00
|
|
|
if not pr_info.is_merge_queue:
|
2024-04-08 16:09:47 +00:00
|
|
|
# we need clean CI status for MQ to merge (no pending statuses)
|
|
|
|
post_commit_status(
|
|
|
|
commit,
|
|
|
|
PENDING,
|
|
|
|
ci_report_url,
|
|
|
|
description,
|
2024-06-10 09:18:03 +00:00
|
|
|
CI.StatusNames.CI,
|
2024-04-08 16:09:47 +00:00
|
|
|
pr_info,
|
|
|
|
)
|
2023-04-06 09:03:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|