ClickHouse/tests/ci/auto_release.py

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

216 lines
6.6 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
2023-09-14 10:23:26 +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 ci_utils import Shell
from env_helper import GITHUB_REPOSITORY
from report import SUCCESS
from ci_buddy import CIBuddy
from ci_config import CI
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"
@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)
@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
refs.sort(key=lambda ref: ref.ref)
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)
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
2024-08-02 07:23:40 +00:00
commit_ci_status = CI.GH.get_commit_status_by_name(
token=token,
commit_sha=commit,
status_name=(CI.JobNames.BUILD_CHECK, "ClickHouse build check"),
2023-09-14 11:44:34 +00:00
)
commit_sha = commit
if commit_ci_status == SUCCESS:
break
2024-07-19 09:35:43 +00:00
print(f"CI status [{commit_ci_status}] - 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(
title=f"Autorelease completed",
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()