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

refactor: Streamline release process #290

Merged
merged 1 commit into from
Sep 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .bcr/config.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions .bcr/metadata.template.json

This file was deleted.

5 changes: 0 additions & 5 deletions .bcr/source.template.json

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/prepare_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
RELEASE_NOTES_TEMPLATE = """
## Using Bzlmod (Recommended)

> :construction: Not yet deployed to BCR :construction:
> Release will become usable via bzlmod as soon as it is no longer the pre-release phase.

Add to your `MODULE.bazel` file:

```starlark
Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module(
name = "depend_on_what_you_use",
version = "0.0.0",
# Keep in sync with setup_step_2.bzl
bazel_compatibility = [">=5.4.0"],
)
Expand Down
122 changes: 122 additions & 0 deletions scripts/deploy_to_bcr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env python3
from __future__ import annotations

import subprocess
import sys
from pathlib import Path
from shutil import rmtree

# No benefit for using logging, and we deliberately use '/tmp' which has no risk in this context
# ruff: noqa: T201n
# ruff: noqa: S108

BCR_DATA_TEMPLATE = """{{
"build_file": null,
"build_targets": [],
"compatibility_level": "0",
"deps": [],
"module_dot_bazel": "{module_dot_bazel}",
"name": "depend_on_what_you_use",
"patch_strip": 0,
"patches": [],
"presubmit_yml": "{presubmit_dot_yml}",
"strip_prefix": "depend_on_what_you_use-{version}",
"test_module_build_targets": [],
"test_module_path": null,
"test_module_test_targets": [],
"url": "https://github.com/martis42/depend_on_what_you_use/releases/download/{version}/depend_on_what_you_use-{version}.tar.gz",
"version": "{version}"
}}
"""

BCR_FORK_REPO = "[email protected]:martis42/bazel-central-registry-fork.git"
BCR_FORK_DIR = Path("/tmp/bcr_fork")


def get_dwyu_files() -> tuple[Path, Path]:
dwyu_dir = Path(
subprocess.run(["bazel", "info", "workspace"], capture_output=True, text=True, check=True).stdout.strip()
)
print(f"Detected DWYU directory: '{dwyu_dir}'")
module_file = dwyu_dir / "MODULE.bazel"
presubmit_file = dwyu_dir / ".bcr/presubmit.yml"

return module_file, presubmit_file


def setup_bcr_fork() -> None:
print(f"\nSetup BCR fork at '{BCR_FORK_DIR}'\n")
if BCR_FORK_DIR.exists():
rmtree(BCR_FORK_DIR)
subprocess.run(["git", "clone", BCR_FORK_REPO, BCR_FORK_DIR], check=True)


def prepare_bcr_data(version: str) -> Path:
module, presubmit = get_dwyu_files()

module_file_with_version = Path("/tmp/dwyu_with_version.MODULE.bazel")
with module.open(mode="rt", encoding="utf-8") as module_in:
module_content = module_in.read()
with module_file_with_version.open(mode="wt", encoding="utf-8") as module_out:
module_out.write(module_content.replace('version = "0.0.0",', f'version = "{version}",'))

bcr_data = Path("/tmp/dwyu_bcr_data.json")
with bcr_data.open(mode="wt", encoding="utf-8") as bcr_data_out:
bcr_data_out.write(
BCR_DATA_TEMPLATE.format(
version=version, module_dot_bazel=module_file_with_version, presubmit_dot_yml=presubmit
)
)

return bcr_data


def add_dwyu_to_bcr_fork(bcr_data: Path, version: str) -> None:
print(f"\n========= Adding DWYU '{version}' to BCR fork '{BCR_FORK_DIR}' =========\n")

subprocess.run(["bazel", "run", "//tools:add_module", "--", "--input", bcr_data], cwd=BCR_FORK_DIR, check=True)
check_process = subprocess.run(
["bazel", "run", "//tools:bcr_validation", "--", "--check", f"depend_on_what_you_use@{version}"],
cwd=BCR_FORK_DIR,
check=False,
)

print("\n================================================================================\n")

if check_process.returncode in [
0, # All good
42, # Check is fine, but a BCR maintainer will have to review
]:
print(f"Successfully added DWYU '{version}' to the BCR fork")
else:
print(f"Adding DWYU '{version}' to the BCR fork failed. Look into the terminal output above to find the issue.")
sys.exit(1)


def push_to_upstream(version: str) -> None:
yn = input(f"\nDo you want to push DWYU '{version}' [y/n]: ")
if yn != "y":
print("Aborting")
sys.exit(0)

print("\nPushing release to BCR\n")

branch = f"depend_on_what_you_use@{version}"
subprocess.run(["git", "checkout", "-B", branch], cwd=BCR_FORK_DIR, check=True)
subprocess.run(["git", "add", "modules/depend_on_what_you_use/"], cwd=BCR_FORK_DIR, check=True)
subprocess.run(["git", "commit", "-m", f"depend_on_what_you_use@{version}"], cwd=BCR_FORK_DIR, check=True)
subprocess.run(["git", "push", "--set-upstream", "origin", branch], cwd=BCR_FORK_DIR, check=True)


def main() -> None:
version = input("To be released version: ")
print()

bcr_data = prepare_bcr_data(version)
setup_bcr_fork()
add_dwyu_to_bcr_fork(bcr_data=bcr_data, version=version)
push_to_upstream(version)


if __name__ == "__main__":
main()
22 changes: 22 additions & 0 deletions scripts/release.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Semi automatic release process

## Release creation

1. `git tag VERSION`
1. `git push origin VERSION`
1. The CI will automatically create a draft PR
1. Check the release notes and fill manual sections
1. TODO do we have to set to 'pre-release', or can we continue with Draft for adding to BCR?

## Adding to BCR

1. Execute [scripts/deploy_to_bcr.py](/scripts/deploy_to_bcr.py) and provide version when asked for
1. Follow lnk in terminal and open PR on https://github.com/bazelbuild/bazel-central-registry

## Finish release

1. Wait until release is available via BCR
1. Make sure the release works via the [test repo](https://github.com/martis42/test_dwyu)
1. Finish release notes:
- Remove BCR warning
- Set to latest release