2021-09-15 12:59:39 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
2021-09-15 13:04:29 +00:00
|
|
|
import logging
|
2022-01-13 11:08:31 +00:00
|
|
|
import re
|
2021-10-20 11:25:14 +00:00
|
|
|
from github import Github
|
2021-11-26 14:00:09 +00:00
|
|
|
|
|
|
|
from env_helper import GITHUB_RUN_ID, GITHUB_REPOSITORY, GITHUB_SERVER_URL
|
|
|
|
from pr_info import PRInfo
|
2021-10-20 11:25:14 +00:00
|
|
|
from get_robot_token import get_best_robot_token
|
2021-11-12 12:36:25 +00:00
|
|
|
from commit_status_helper import get_commit
|
2021-09-15 12:59:39 +00:00
|
|
|
|
2022-01-13 11:06:50 +00:00
|
|
|
NAME = "Run Check (actions)"
|
2021-09-15 12:59:39 +00:00
|
|
|
|
|
|
|
TRUSTED_ORG_IDS = {
|
2022-01-13 11:06:50 +00:00
|
|
|
7409213, # yandex
|
2021-09-15 12:59:39 +00:00
|
|
|
28471076, # altinity
|
|
|
|
54801242, # clickhouse
|
|
|
|
}
|
|
|
|
|
|
|
|
OK_TEST_LABEL = set(["can be tested", "release", "pr-documentation", "pr-doc-fix"])
|
|
|
|
DO_NOT_TEST_LABEL = "do not test"
|
|
|
|
|
|
|
|
# Individual trusted contirbutors who are not in any trusted organization.
|
|
|
|
# Can be changed in runtime: we will append users that we learned to be in
|
|
|
|
# a trusted org, to save GitHub API calls.
|
2022-01-13 11:06:50 +00:00
|
|
|
TRUSTED_CONTRIBUTORS = {
|
|
|
|
e.lower()
|
|
|
|
for e in [
|
|
|
|
"achimbab",
|
|
|
|
"adevyatova ", # DOCSUP
|
|
|
|
"Algunenano", # Raúl Marín, Tinybird
|
|
|
|
"AnaUvarova", # DOCSUP
|
|
|
|
"anauvarova", # technical writer, Yandex
|
|
|
|
"annvsh", # technical writer, Yandex
|
|
|
|
"atereh", # DOCSUP
|
|
|
|
"azat",
|
|
|
|
"bharatnc", # Newbie, but already with many contributions.
|
|
|
|
"bobrik", # Seasoned contributor, CloundFlare
|
|
|
|
"BohuTANG",
|
|
|
|
"codyrobert", # Flickerbox engineer
|
|
|
|
"cwurm", # Employee
|
|
|
|
"damozhaeva", # DOCSUP
|
|
|
|
"den-crane",
|
|
|
|
"flickerbox-tom", # Flickerbox
|
|
|
|
"gyuton", # technical writer, Yandex
|
|
|
|
"hagen1778", # Roman Khavronenko, seasoned contributor
|
|
|
|
"hczhcz",
|
|
|
|
"hexiaoting", # Seasoned contributor
|
|
|
|
"ildus", # adjust, ex-pgpro
|
|
|
|
"javisantana", # a Spanish ClickHouse enthusiast, ex-Carto
|
|
|
|
"ka1bi4", # DOCSUP
|
|
|
|
"kirillikoff", # DOCSUP
|
|
|
|
"kitaisreal", # Seasoned contributor
|
|
|
|
"kreuzerkrieg",
|
|
|
|
"lehasm", # DOCSUP
|
|
|
|
"michon470", # DOCSUP
|
|
|
|
"MyroTk", # Tester in Altinity
|
|
|
|
"myrrc", # Michael Kot, Altinity
|
|
|
|
"nikvas0",
|
|
|
|
"nvartolomei",
|
|
|
|
"olgarev", # DOCSUP
|
|
|
|
"otrazhenia", # Yandex docs contractor
|
|
|
|
"pdv-ru", # DOCSUP
|
|
|
|
"podshumok", # cmake expert from QRator Labs
|
|
|
|
"s-mx", # Maxim Sabyanin, former employee, present contributor
|
|
|
|
"sevirov", # technical writer, Yandex
|
|
|
|
"spongedu", # Seasoned contributor
|
|
|
|
"ucasFL", # Amos Bird's friend
|
|
|
|
"vdimir", # Employee
|
|
|
|
"vzakaznikov",
|
|
|
|
"YiuRULE",
|
|
|
|
"zlobober", # Developer of YT
|
|
|
|
"ilejn", # Arenadata, responsible for Kerberized Kafka
|
|
|
|
"thomoco", # ClickHouse
|
|
|
|
"BoloniniD", # Seasoned contributor, HSE
|
|
|
|
]
|
|
|
|
}
|
2021-09-15 12:59:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pr_is_by_trusted_user(pr_user_login, pr_user_orgs):
|
2021-12-13 14:34:17 +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.
|
|
|
|
def should_run_checks_for_pr(pr_info):
|
|
|
|
# Consider the labels and whether the user is trusted.
|
2021-12-22 08:13:04 +00:00
|
|
|
print("Got labels", pr_info.labels)
|
2022-01-13 11:06:50 +00:00
|
|
|
force_labels = set(["force tests"]).intersection(pr_info.labels)
|
2021-09-15 12:59:39 +00:00
|
|
|
if force_labels:
|
2022-01-13 11:06:50 +00:00
|
|
|
return True, "Labeled '{}'".format(", ".join(force_labels))
|
2021-09-15 12:59:39 +00:00
|
|
|
|
2022-01-13 11:06:50 +00:00
|
|
|
if "do not test" in pr_info.labels:
|
2021-09-15 12:59:39 +00:00
|
|
|
return False, "Labeled 'do not test'"
|
|
|
|
|
2022-01-13 11:06:50 +00:00
|
|
|
if "can be tested" not in pr_info.labels and not pr_is_by_trusted_user(
|
|
|
|
pr_info.user_login, pr_info.user_orgs
|
|
|
|
):
|
2021-09-15 12:59:39 +00:00
|
|
|
return False, "Needs 'can be tested' label"
|
|
|
|
|
2022-01-13 11:06:50 +00:00
|
|
|
if (
|
|
|
|
"release" in pr_info.labels
|
|
|
|
or "pr-backport" in pr_info.labels
|
|
|
|
or "pr-cherrypick" in pr_info.labels
|
|
|
|
):
|
2021-09-30 11:26:46 +00:00
|
|
|
return False, "Don't try new checks for release/backports/cherry-picks"
|
|
|
|
|
2021-09-15 12:59:39 +00:00
|
|
|
return True, "No special conditions apply"
|
|
|
|
|
2021-11-26 14:00:09 +00:00
|
|
|
|
2022-01-13 11:08:31 +00:00
|
|
|
def check_pr_description(pr_info):
|
|
|
|
description = pr_info.body
|
|
|
|
|
|
|
|
lines = [
|
|
|
|
line
|
|
|
|
for line in map(
|
|
|
|
lambda x: x.strip(), description.split("\n") if description else []
|
|
|
|
)
|
|
|
|
]
|
|
|
|
lines = [re.sub(r"\s+", " ", l) for l in lines]
|
|
|
|
|
|
|
|
category = ""
|
|
|
|
entry = ""
|
|
|
|
|
|
|
|
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])
|
|
|
|
result_status = (
|
|
|
|
"More than one changelog category specified: '"
|
|
|
|
+ category
|
|
|
|
+ "', '"
|
|
|
|
+ second_category
|
|
|
|
+ "'"
|
|
|
|
)
|
|
|
|
return result_status[:140]
|
|
|
|
|
|
|
|
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)
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if not category:
|
|
|
|
return "Changelog category is empty"
|
|
|
|
|
|
|
|
# Filter out the PR categories that are not for changelog.
|
|
|
|
if re.match(
|
|
|
|
r"(?i)doc|((non|in|not|un)[-\s]*significant)|(not[ ]*for[ ]*changelog)",
|
|
|
|
category,
|
|
|
|
):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
if not entry:
|
|
|
|
return "Changelog entry required for category '{}'".format(category)
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2021-09-15 12:59:39 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
2021-12-22 07:54:50 +00:00
|
|
|
pr_info = PRInfo(need_orgs=True, labels_from_api=True)
|
2021-09-15 12:59:39 +00:00
|
|
|
can_run, description = should_run_checks_for_pr(pr_info)
|
2021-10-20 11:25:14 +00:00
|
|
|
gh = Github(get_best_robot_token())
|
|
|
|
commit = get_commit(gh, pr_info.sha)
|
2022-01-13 11:08:31 +00:00
|
|
|
|
|
|
|
description_report = check_pr_description(pr_info)[:139]
|
|
|
|
if description_report:
|
|
|
|
print("::notice ::Cannot run, description does not match the template")
|
|
|
|
url = (
|
|
|
|
f"{GITHUB_SERVER_URL}/{GITHUB_REPOSITORY}/"
|
|
|
|
"blob/master/.github/PULL_REQUEST_TEMPLATE.md?plain=1"
|
|
|
|
)
|
|
|
|
commit.create_status(
|
|
|
|
context=NAME,
|
|
|
|
description=description_report,
|
|
|
|
state="failure",
|
|
|
|
target_url=url,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-11-26 14:00:09 +00:00
|
|
|
url = f"{GITHUB_SERVER_URL}/{GITHUB_REPOSITORY}/actions/runs/{GITHUB_RUN_ID}"
|
2021-09-15 12:59:39 +00:00
|
|
|
if not can_run:
|
2021-09-16 10:39:36 +00:00
|
|
|
print("::notice ::Cannot run")
|
2022-01-13 11:06:50 +00:00
|
|
|
commit.create_status(
|
|
|
|
context=NAME, description=description, state="failure", target_url=url
|
|
|
|
)
|
2021-09-15 12:59:39 +00:00
|
|
|
sys.exit(1)
|
2021-09-15 13:36:48 +00:00
|
|
|
else:
|
2022-01-13 11:06:50 +00:00
|
|
|
if "pr-documentation" in pr_info.labels or "pr-doc-fix" in pr_info.labels:
|
|
|
|
commit.create_status(
|
|
|
|
context=NAME,
|
|
|
|
description="Skipping checks for documentation",
|
|
|
|
state="success",
|
|
|
|
target_url=url,
|
|
|
|
)
|
2021-10-26 10:34:12 +00:00
|
|
|
print("::notice ::Can run, but it's documentation PR, skipping")
|
|
|
|
else:
|
|
|
|
print("::notice ::Can run")
|
2022-01-13 11:06:50 +00:00
|
|
|
commit.create_status(
|
|
|
|
context=NAME, description=description, state="pending", target_url=url
|
|
|
|
)
|