ClickHouse/tests/ci/auto_release.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

245 lines
7.9 KiB
Python
Raw Normal View History

import argparse
2024-07-16 13:37:50 +00:00
import dataclasses
import json
2023-09-14 12:03:01 +00:00
import os
import sys
2024-07-16 13:37:50 +00:00
from typing import List
2024-09-26 14:05:34 +00:00
from ci_buddy import CIBuddy
from ci_config import CI
2024-07-16 13:37:50 +00:00
from ci_utils import Shell
from env_helper import GITHUB_REPOSITORY
2024-09-26 14:05:34 +00:00
from get_robot_token import get_best_robot_token
from github_helper import GitHub
2024-07-16 13:37:50 +00:00
from report import SUCCESS
def parse_args():
parser = argparse.ArgumentParser(
2023-09-14 14:45:23 +00:00
"Checks if enough days elapsed since the last release on each release "
"branches and do a release in case for green builds."
)
parser.add_argument("--token", help="GitHub token, if not set, used from smm")
parser.add_argument(
"--post-status",
action="store_true",
help="Post release branch statuses",
)
2024-07-19 18:43:14 +00:00
parser.add_argument(
"--post-auto-release-complete",
action="store_true",
help="Post autorelease completion status",
)
parser.add_argument(
"--prepare",
action="store_true",
help="Prepare autorelease info",
)
2024-07-19 18:43:14 +00:00
parser.add_argument(
"--wf-status",
type=str,
default="",
help="overall workflow status [success|failure]",
)
return parser.parse_args(), parser
2024-07-16 13:37:50 +00:00
MAX_NUMBER_OF_COMMITS_TO_CONSIDER_FOR_RELEASE = 5
AUTORELEASE_INFO_FILE = "/tmp/autorelease_info.json"
2024-08-16 09:11:11 +00:00
AUTORELEASE_MATRIX_PARAMS = "/tmp/autorelease_params.json"
2024-07-16 13:37:50 +00:00
@dataclasses.dataclass
class ReleaseParams:
ready: bool
ci_status: str
num_patches: int
2024-07-16 13:37:50 +00:00
release_branch: str
commit_sha: str
commits_to_branch_head: int
latest: bool
def to_dict(self):
return dataclasses.asdict(self)
2024-07-16 13:37:50 +00:00
@dataclasses.dataclass
class AutoReleaseInfo:
releases: List[ReleaseParams]
2024-07-19 09:35:43 +00:00
def add_release(self, release_params: ReleaseParams) -> None:
2024-07-16 13:37:50 +00:00
self.releases.append(release_params)
def dump(self):
print(f"Dump release info into [{AUTORELEASE_INFO_FILE}]")
with open(AUTORELEASE_INFO_FILE, "w", encoding="utf-8") as f:
print(json.dumps(dataclasses.asdict(self), indent=2), file=f)
2024-08-16 09:11:11 +00:00
# dump file for GH action matrix that is similar to the file above but with dropped not ready release branches
params = dataclasses.asdict(self)
params["releases"] = [
release for release in params["releases"] if release["ready"]
]
2024-08-16 09:11:11 +00:00
with open(AUTORELEASE_MATRIX_PARAMS, "w", encoding="utf-8") as f:
print(json.dumps(params, indent=2), file=f)
@staticmethod
def from_file() -> "AutoReleaseInfo":
with open(AUTORELEASE_INFO_FILE, "r", encoding="utf-8") as json_file:
res = json.load(json_file)
releases = [ReleaseParams(**release) for release in res["releases"]]
return AutoReleaseInfo(releases=releases)
2024-07-16 13:37:50 +00:00
def _prepare(token):
2024-07-16 13:37:50 +00:00
assert len(token) > 10
os.environ["GH_TOKEN"] = token
2024-08-01 09:57:54 +00:00
Shell.check("gh auth status")
gh = GitHub(token)
2024-07-16 13:37:50 +00:00
prs = gh.get_release_pulls(GITHUB_REPOSITORY)
prs.sort(key=lambda x: x.head.ref)
branch_names = [pr.head.ref for pr in prs]
2024-07-16 13:37:50 +00:00
print(f"Found release branches [{branch_names}]")
repo = gh.get_repo(GITHUB_REPOSITORY)
2024-07-16 13:37:50 +00:00
autoRelease_info = AutoReleaseInfo(releases=[])
for pr in prs:
print(f"\nChecking PR [{pr.head.ref}]")
2023-09-14 13:41:34 +00:00
refs = list(repo.get_git_matching_refs(f"tags/v{pr.head.ref}"))
assert refs
latest_release_tag_ref = refs[-1]
latest_release_tag = repo.get_git_tag(latest_release_tag_ref.object.sha)
2024-08-01 09:57:54 +00:00
commits = Shell.get_output_or_raise(
f"git rev-list --first-parent {latest_release_tag.tag}..origin/{pr.head.ref}",
).split("\n")
commit_num = len(commits)
if latest_release_tag.tag.endswith("new"):
print("It's a new release branch - skip auto release for it")
continue
2024-07-16 13:37:50 +00:00
print(
f"Previous release [{latest_release_tag.tag}] was [{commit_num}] commits ago, date [{latest_release_tag.tagger.date}]"
2023-09-13 17:16:17 +00:00
)
commits_to_check = commits[:-1] # Exclude the version bump commit
2024-07-16 13:37:50 +00:00
commit_sha = ""
commit_ci_status = ""
commits_to_branch_head = 0
for idx, commit in enumerate(
commits_to_check[:MAX_NUMBER_OF_COMMITS_TO_CONSIDER_FOR_RELEASE]
2024-07-16 13:37:50 +00:00
):
print(
f"Check commit [{commit}] [{pr.head.ref}~{idx+1}] as release candidate"
)
commit_num -= 1
2024-08-02 07:23:40 +00:00
is_completed = CI.GH.check_wf_completed(token=token, commit_sha=commit)
if not is_completed:
print(f"CI is in progress for [{commit}] - check previous commit")
commits_to_branch_head += 1
continue
# TODO: switch to check if CI is entirely green
statuses = [
CI.GH.get_commit_status_by_name(
token=token,
commit_sha=commit,
# handle old name for old releases
status_name=(CI.JobNames.BUILD_CHECK, "ClickHouse build check"),
),
CI.GH.get_commit_status_by_name(
token=token,
commit_sha=commit,
# handle old name for old releases
status_name=CI.JobNames.STATELESS_TEST_RELEASE,
),
CI.GH.get_commit_status_by_name(
token=token,
commit_sha=commit,
# handle old name for old releases
status_name=CI.JobNames.STATEFUL_TEST_RELEASE,
),
]
commit_sha = commit
if any(status == SUCCESS for status in statuses):
commit_ci_status = SUCCESS
break
2024-07-19 09:35:43 +00:00
print(f"CI status [{statuses}] - skip")
commits_to_branch_head += 1
2024-07-19 09:35:43 +00:00
ready = False
if commit_ci_status == SUCCESS and commit_sha:
2024-07-16 13:37:50 +00:00
print(
f"Add release ready info for commit [{commit_sha}] and release branch [{pr.head.ref}]"
)
2024-07-19 09:35:43 +00:00
ready = True
2024-07-16 13:37:50 +00:00
else:
print(f"WARNING: No ready commits found for release branch [{pr.head.ref}]")
autoRelease_info.add_release(
ReleaseParams(
release_branch=pr.head.ref,
commit_sha=commit_sha,
ready=ready,
ci_status=commit_ci_status,
num_patches=commit_num,
commits_to_branch_head=commits_to_branch_head,
latest=False,
)
)
if autoRelease_info.releases:
autoRelease_info.releases[-1].latest = True
2024-07-16 13:37:50 +00:00
autoRelease_info.dump()
def main():
args, parser = parse_args()
if args.post_status:
info = AutoReleaseInfo.from_file()
for release_info in info.releases:
if release_info.ready:
CIBuddy(dry_run=False).post_info(
title=f"Auto Release Status for {release_info.release_branch}",
body=release_info.to_dict(),
)
else:
CIBuddy(dry_run=False).post_warning(
title=f"Auto Release Status for {release_info.release_branch}",
body=release_info.to_dict(),
)
2024-07-22 11:17:25 +00:00
elif args.post_auto_release_complete:
2024-07-19 18:43:14 +00:00
assert args.wf_status, "--wf-status Required with --post-auto-release-complete"
if args.wf_status != SUCCESS:
CIBuddy(dry_run=False).post_job_error(
error_description="Autorelease workflow failed",
job_name="Autorelease",
with_instance_info=False,
with_wf_link=True,
critical=True,
)
else:
CIBuddy(dry_run=False).post_info(
2024-09-26 14:05:34 +00:00
title="Autorelease completed",
2024-07-19 18:43:14 +00:00
body="",
with_wf_link=True,
)
elif args.prepare:
_prepare(token=args.token or get_best_robot_token())
2023-09-14 12:03:01 +00:00
else:
parser.print_help()
sys.exit(2)
if __name__ == "__main__":
main()