Skip to content

Commit

Permalink
chore: add partial release scripts (#11250)
Browse files Browse the repository at this point in the history
* chore: add partial release scripts

* bring back apply_current_version.sh

* change impl

* add unit test

* add unit test

* setup ci
  • Loading branch information
JoeWang1127 authored Oct 15, 2024
1 parent e85ab6b commit a9a85b1
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 0 deletions.
Empty file added .github/release/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions .github/release/fixture/asset/versions-asset.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Format:
# module:released-version:current-version

google-cloud-asset:3.56.0:3.56.0
grpc-google-cloud-asset-v1:3.56.0:3.56.0
grpc-google-cloud-asset-v1p1beta1:0.156.0:0.156.0
grpc-google-cloud-asset-v1p2beta1:0.156.0:0.156.0
grpc-google-cloud-asset-v1p5beta1:0.156.0:0.156.0
grpc-google-cloud-asset-v1p7beta1:3.56.0:3.56.0
proto-google-cloud-asset-v1:3.56.0:3.56.0
proto-google-cloud-asset-v1p1beta1:0.156.0:0.156.0
proto-google-cloud-asset-v1p2beta1:0.156.0:0.156.0
proto-google-cloud-asset-v1p5beta1:0.156.0:0.156.0
proto-google-cloud-asset-v1p7beta1:3.56.0:3.56.0
27 changes: 27 additions & 0 deletions .github/release/fixture/multiple_modules/versions-multiple.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Format:
# module:released-version:current-version

google-cloud-java:1.46.0:1.46.0
google-cloud-beyondcorp-appconnections:0.50.0:0.50.0
proto-google-cloud-beyondcorp-appconnections-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-appconnections-v1:0.50.0:0.50.0
google-cloud-beyondcorp-appconnectors:0.50.0:0.50.0
proto-google-cloud-beyondcorp-appconnectors-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-appconnectors-v1:0.50.0:0.50.0
google-cloud-beyondcorp-appgateways:0.50.0:0.50.0
proto-google-cloud-beyondcorp-appgateways-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-appgateways-v1:0.50.0:0.50.0
google-cloud-beyondcorp-clientconnectorservices:0.50.0:0.50.0
proto-google-cloud-beyondcorp-clientconnectorservices-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-clientconnectorservices-v1:0.50.0:0.50.0
google-cloud-beyondcorp-clientgateways:0.50.0:0.50.0
proto-google-cloud-beyondcorp-clientgateways-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-clientgateways-v1:0.50.0:0.50.0
google-cloud-bigqueryconnection:2.54.0:2.54.0
grpc-google-cloud-bigqueryconnection-v1:2.54.0:2.54.0
grpc-google-cloud-bigqueryconnection-v1beta1:0.62.0:0.62.0
proto-google-cloud-bigqueryconnection-v1:2.54.0:2.54.0
proto-google-cloud-bigqueryconnection-v1beta1:0.62.0:0.62.0
google-cloud-bigquery-data-exchange:2.47.0:2.47.0
proto-google-cloud-bigquery-data-exchange-v1beta1:2.47.0:2.47.0
grpc-google-cloud-bigquery-data-exchange-v1beta1:2.47.0:2.47.0
123 changes: 123 additions & 0 deletions .github/release/partial_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# GitHub action job to test core java library features on
# downstream client libraries before they are released.
import re
from enum import Enum
import click as click

VERSION_PREFIX = r"^(grpc|proto)-"
VERSION_SUFFIX = r"-v[1-9].*$"


class VersionType(Enum):
MAJOR = (1,)
MINOR = (2,)
PATCH = 3


@click.group(invoke_without_command=False)
@click.pass_context
@click.version_option(message="%(version)s")
def main(ctx):
pass


@main.command()
@click.option(
"--artifact_ids",
required=True,
type=str,
help="""
Artifact IDs whose version needs to update, separated by comma.
""",
)
@click.option(
"--version-type",
required=False,
default="patch",
type=str,
help="""
The type of version bump, one of major, minor or patch.
""",
)
@click.option(
"--versions",
required=False,
default="./versions.txt",
type=str,
help="""
The path to the versions.txt.
""",
)
def bump_released_version(artifact_ids: str, version_type: str, versions: str) -> None:
target_artifact_ids = set(artifact_ids.split(","))
version_enum = _parse_type_or_raise(version_type)
newlines = []
with open(versions) as versions_file:
for num, line in enumerate(versions_file):
striped_line = line.strip()
# case 1: skip an empty line.
if striped_line == "":
newlines.append("")
continue
# case 2: keep a comment.
if striped_line.startswith("#"):
newlines.append(f"{striped_line}")
continue
values = striped_line.split(":")
artifact_id = values[0]
released_version = values[1]
sanitized_artifact_id = _sanitize(artifact_id)
# case 3: keep the line if the artifact id is not matched.
if sanitized_artifact_id not in target_artifact_ids:
newlines.append(f"{striped_line}")
continue
# case 4: bump version according to version type.
major, minor, patch = [
int(ver_num) for ver_num in released_version.split(".")
]
match version_enum:
case VersionType.MAJOR:
major += 1
case VersionType.MINOR:
minor += 1
case VersionType.PATCH:
patch += 1
newlines.append(
f"{artifact_id}:{major}.{minor}.{patch}:{major}.{minor}.{patch}"
)
with open(versions, "w") as versions_file:
versions_file.writelines("\n".join(newlines))


def _parse_type_or_raise(version_type: str) -> VersionType:
try:
version_enum = VersionType[version_type.upper()]
except KeyError:
raise KeyError(
f"Version type {version_type} is not supported.\n"
f"Supported types are MAJOR, MINOR AND PATCH."
)
return version_enum


def _sanitize(artifact_id: str) -> str:
# Remove the matching regex.
temp = re.sub(VERSION_PREFIX, "", artifact_id)
return re.sub(VERSION_SUFFIX, "", temp)


if __name__ == "__main__":
main()
76 changes: 76 additions & 0 deletions .github/release/release_unit_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from click.testing import CliRunner
import contextlib
import os
import shutil
import tempfile
import unittest
from partial_release import bump_released_version

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
GOLDEN = os.path.join(SCRIPT_DIR, "testdata")
FIXTURES = os.path.join(SCRIPT_DIR, "fixture")
runner = CliRunner()


class TestCase(unittest.TestCase):
def test_bump_single_version_success(self):
golden = f"{GOLDEN}/asset/versions-asset-golden.txt"
with copied_fixtures_dir(f"{FIXTURES}/asset"):
runner.invoke(
bump_released_version,
[
"--artifact_ids=google-cloud-asset",
"--versions=versions-asset.txt",
],
)
with open(golden) as g:
expected = g.read()
with open("./versions-asset.txt") as f:
actual = f.read()
self.assertEqual(expected, actual)

def test_bump_multiple_versions_success(self):
golden = f"{GOLDEN}/multiple_modules/versions-multiple-golden.txt"
with copied_fixtures_dir(f"{FIXTURES}/multiple_modules"):
runner.invoke(
bump_released_version,
[
"--artifact_ids=google-cloud-bigqueryconnection,google-cloud-java",
"--versions=versions-multiple.txt",
],
)
with open(golden) as g:
expected = g.read()
with open("./versions-multiple.txt") as f:
actual = f.read()
self.assertEqual(expected, actual)


@contextlib.contextmanager
def change_dir_to(path: str) -> str:
"""
Context Manager to change the current working directory and restore the
previous working directory after completing the context.
"""
old_cwd = os.getcwd()
os.chdir(path)
try:
yield path
finally:
os.chdir(old_cwd)


@contextlib.contextmanager
def copied_fixtures_dir(source: str) -> str:
"""
Context Manager to copy from a fixtures directory into a new temporary
directory and change the current working directory to that copy.
Restores the original current working directory after completing the
context.
"""
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(source, f"{tempdir}/workspace")
with change_dir_to(workdir):
yield workdir
2 changes: 2 additions & 0 deletions .github/release/requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
black==24.8.0
click==8.1.7
52 changes: 52 additions & 0 deletions .github/release/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --generate-hashes --strip-extras generation/requirements.in
#
black==24.8.0 \
--hash=sha256:09cdeb74d494ec023ded657f7092ba518e8cf78fa8386155e4a03fdcc44679e6 \
--hash=sha256:1f13f7f386f86f8121d76599114bb8c17b69d962137fc70efe56137727c7047e \
--hash=sha256:2500945420b6784c38b9ee885af039f5e7471ef284ab03fa35ecdde4688cd83f \
--hash=sha256:2b59b250fdba5f9a9cd9d0ece6e6d993d91ce877d121d161e4698af3eb9c1018 \
--hash=sha256:3c4285573d4897a7610054af5a890bde7c65cb466040c5f0c8b732812d7f0e5e \
--hash=sha256:505289f17ceda596658ae81b61ebbe2d9b25aa78067035184ed0a9d855d18afd \
--hash=sha256:62e8730977f0b77998029da7971fa896ceefa2c4c4933fcd593fa599ecbf97a4 \
--hash=sha256:649f6d84ccbae73ab767e206772cc2d7a393a001070a4c814a546afd0d423aed \
--hash=sha256:6e55d30d44bed36593c3163b9bc63bf58b3b30e4611e4d88a0c3c239930ed5b2 \
--hash=sha256:707a1ca89221bc8a1a64fb5e15ef39cd755633daa672a9db7498d1c19de66a42 \
--hash=sha256:72901b4913cbac8972ad911dc4098d5753704d1f3c56e44ae8dce99eecb0e3af \
--hash=sha256:73bbf84ed136e45d451a260c6b73ed674652f90a2b3211d6a35e78054563a9bb \
--hash=sha256:7c046c1d1eeb7aea9335da62472481d3bbf3fd986e093cffd35f4385c94ae368 \
--hash=sha256:81c6742da39f33b08e791da38410f32e27d632260e599df7245cccee2064afeb \
--hash=sha256:837fd281f1908d0076844bc2b801ad2d369c78c45cf800cad7b61686051041af \
--hash=sha256:972085c618ee94f402da1af548a4f218c754ea7e5dc70acb168bfaca4c2542ed \
--hash=sha256:9e84e33b37be070ba135176c123ae52a51f82306def9f7d063ee302ecab2cf47 \
--hash=sha256:b19c9ad992c7883ad84c9b22aaa73562a16b819c1d8db7a1a1a49fb7ec13c7d2 \
--hash=sha256:d6417535d99c37cee4091a2f24eb2b6d5ec42b144d50f1f2e436d9fe1916fe1a \
--hash=sha256:eab4dd44ce80dea27dc69db40dab62d4ca96112f87996bca68cd75639aeb2e4c \
--hash=sha256:f490dbd59680d809ca31efdae20e634f3fae27fba3ce0ba3208333b713bc3920 \
--hash=sha256:fb6e2c0b86bbd43dee042e48059c9ad7830abd5c94b0bc518c0eeec57c3eddc1
# via -r generation/requirements.in
click==8.1.7 \
--hash=sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28 \
--hash=sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de
# via
# -r generation/requirements.in
# black
mypy-extensions==1.0.0 \
--hash=sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d \
--hash=sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782
# via black
packaging==24.1 \
--hash=sha256:026ed72c8ed3fcce5bf8950572258698927fd1dbda10a5e981cdf0ac37f4f002 \
--hash=sha256:5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124
# via black
pathspec==0.12.1 \
--hash=sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08 \
--hash=sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712
# via black
platformdirs==4.3.6 \
--hash=sha256:357fb2acbc885b0419afd3ce3ed34564c13c9b95c89360cd9563f73aa5e2b907 \
--hash=sha256:73e575e1408ab8103900836b97580d5307456908a03e92031bab39e4554cc3fb
# via black
14 changes: 14 additions & 0 deletions .github/release/testdata/asset/versions-asset-golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Format:
# module:released-version:current-version

google-cloud-asset:3.56.1:3.56.1
grpc-google-cloud-asset-v1:3.56.1:3.56.1
grpc-google-cloud-asset-v1p1beta1:0.156.1:0.156.1
grpc-google-cloud-asset-v1p2beta1:0.156.1:0.156.1
grpc-google-cloud-asset-v1p5beta1:0.156.1:0.156.1
grpc-google-cloud-asset-v1p7beta1:3.56.1:3.56.1
proto-google-cloud-asset-v1:3.56.1:3.56.1
proto-google-cloud-asset-v1p1beta1:0.156.1:0.156.1
proto-google-cloud-asset-v1p2beta1:0.156.1:0.156.1
proto-google-cloud-asset-v1p5beta1:0.156.1:0.156.1
proto-google-cloud-asset-v1p7beta1:3.56.1:3.56.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Format:
# module:released-version:current-version

google-cloud-java:1.46.1:1.46.1
google-cloud-beyondcorp-appconnections:0.50.0:0.50.0
proto-google-cloud-beyondcorp-appconnections-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-appconnections-v1:0.50.0:0.50.0
google-cloud-beyondcorp-appconnectors:0.50.0:0.50.0
proto-google-cloud-beyondcorp-appconnectors-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-appconnectors-v1:0.50.0:0.50.0
google-cloud-beyondcorp-appgateways:0.50.0:0.50.0
proto-google-cloud-beyondcorp-appgateways-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-appgateways-v1:0.50.0:0.50.0
google-cloud-beyondcorp-clientconnectorservices:0.50.0:0.50.0
proto-google-cloud-beyondcorp-clientconnectorservices-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-clientconnectorservices-v1:0.50.0:0.50.0
google-cloud-beyondcorp-clientgateways:0.50.0:0.50.0
proto-google-cloud-beyondcorp-clientgateways-v1:0.50.0:0.50.0
grpc-google-cloud-beyondcorp-clientgateways-v1:0.50.0:0.50.0
google-cloud-bigqueryconnection:2.54.1:2.54.1
grpc-google-cloud-bigqueryconnection-v1:2.54.1:2.54.1
grpc-google-cloud-bigqueryconnection-v1beta1:0.62.1:0.62.1
proto-google-cloud-bigqueryconnection-v1:2.54.1:2.54.1
proto-google-cloud-bigqueryconnection-v1beta1:0.62.1:0.62.1
google-cloud-bigquery-data-exchange:2.47.0:2.47.0
proto-google-cloud-bigquery-data-exchange-v1beta1:2.47.0:2.47.0
grpc-google-cloud-bigquery-data-exchange-v1beta1:2.47.0:2.47.0
36 changes: 36 additions & 0 deletions .github/workflows/release_tools_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# GitHub action job to test core java library features on
# downstream client libraries before they are released.
name: Release Tools test
on:
pull_request:
paths:
- .github/release/**
jobs:
release-tool-unit-test:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependency
shell: bash
run: |
pip install -r .github/release/requirements.txt
- name: Run unit tests
shell: bash
run: |
python -m unittest discover -s .github/release/ -p "*unit_tests.py"
Loading

0 comments on commit a9a85b1

Please sign in to comment.