From 8e590d1fc5dbbf5b4ac1d0530557c94c7b815765 Mon Sep 17 00:00:00 2001 From: zemnmez Date: Sun, 5 Jun 2022 21:00:15 -0700 Subject: [PATCH 1/2] add version testing and bumping --- .bazelrc | 3 ++ bzl/versioning/BUILD | 2 +- bzl/versioning/bump.py | 35 +++++++++++++ bzl/versioning/rules.bzl | 49 +++++++++++++++++++ bzl/versioning/test/bump_on_change_test/BUILD | 9 ++++ bzl/versioning/test/bump_on_change_test/MAJOR | 1 + .../test/bump_on_change_test/contents.txt | 1 + .../test/bump_on_change_test/version.lock | 2 + .../test/{1 => semver_version}/BUILD | 0 .../test/{1 => semver_version}/MAJOR | 0 .../test/{1 => semver_version}/MINOR | 0 .../test/{1 => semver_version}/PATCH | 0 .../test/{1 => semver_version}/expected.txt | 0 13 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 bzl/versioning/bump.py create mode 100644 bzl/versioning/test/bump_on_change_test/BUILD create mode 100644 bzl/versioning/test/bump_on_change_test/MAJOR create mode 100644 bzl/versioning/test/bump_on_change_test/contents.txt create mode 100644 bzl/versioning/test/bump_on_change_test/version.lock rename bzl/versioning/test/{1 => semver_version}/BUILD (100%) rename bzl/versioning/test/{1 => semver_version}/MAJOR (100%) rename bzl/versioning/test/{1 => semver_version}/MINOR (100%) rename bzl/versioning/test/{1 => semver_version}/PATCH (100%) rename bzl/versioning/test/{1 => semver_version}/expected.txt (100%) diff --git a/.bazelrc b/.bazelrc index f563f09c1a..0bafbd1fe5 100644 --- a/.bazelrc +++ b/.bazelrc @@ -89,3 +89,6 @@ try-import %workspace%/.bazelrc.user build --action_env=AWS_ACCESS_KEY_ID --action_env=AWS_SECRET_ACCESS_KEY --action_env=PULUMI_ACCESS_TOKEN test --action_env=PULUMI_ACCESS_TOKEN + +# be careful with this! +test --test_tag_filters=-do_not_run_on_main diff --git a/bzl/versioning/BUILD b/bzl/versioning/BUILD index c3fbf22ade..cc83dbb72e 100644 --- a/bzl/versioning/BUILD +++ b/bzl/versioning/BUILD @@ -1,6 +1,6 @@ exports_files( [ - "version.tmpl.txt" + "bump.py" ], visibility = ["//visibility:public"], ) diff --git a/bzl/versioning/bump.py b/bzl/versioning/bump.py new file mode 100644 index 0000000000..09dbb6ee07 --- /dev/null +++ b/bzl/versioning/bump.py @@ -0,0 +1,35 @@ +import argparse +import os +import subprocess +import shutil + +parser = argparse.ArgumentParser(description="Performs the action of a version bump.") +parser.add_argument('--to_bump_in', help="The version file to bump, as a root-relative path.", type=str) +parser.add_argument('--to_bump_out', help="The version file to bump, as a root-relative path.", type=str) +parser.add_argument('--lockfile_build_label', help="A label that points to the generated (new) version lockfile.", type=str) +parser.add_argument('--lockfile_build_rootpath', help="The path from the repo root that the lockfile is generated into", type=str) +parser.add_argument('--lockfile_out_rootpath', help="The location to place the newly minted version lockfile at.", type=str) + +# This happens directly on the real workspace -- also, needs to be +# run from bazel to have this set. +os.chdir(os.environ.get('BUILD_WORKSPACE_DIRECTORY')) + +args = parser.parse_args() + +number = 0 + +with open(args.to_bump_in, mode='r', encoding='utf-8') as f: + number = int(f.read()) + + +with open(args.to_bump_out, mode='w', encoding='utf-8') as f: + f.write(str(number+1)) + +# Once the version has been bumped, generate the new version bump file. +subprocess.run(["bazelisk", "build", args.lockfile_build_label]) + +# Copy the newly created lockfile across +shutil.copyfile( + os.path.join("dist", "bin", args.lockfile_build_rootpath), + args.lockfile_out_rootpath +) \ No newline at end of file diff --git a/bzl/versioning/rules.bzl b/bzl/versioning/rules.bzl index 9d1be03572..afd8c35bff 100644 --- a/bzl/versioning/rules.bzl +++ b/bzl/versioning/rules.bzl @@ -1,3 +1,6 @@ +load("//bzl/hash:rules.bzl", "hashes") +load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test") +load("@rules_python//python:defs.bzl", "py_binary") def semver_version(name, **kwargs): _semver_version( @@ -34,3 +37,49 @@ _semver_version = rule( "output": attr.output(mandatory = True) } ) + +def _absolute_label(label): + if label.startswith('@') or label.startswith('/'): + return label + if label.startswith(':'): + return native.repository_name() + '//' + native.package_name() + label + return native.repository_name() + '//' + native.package_name() + ':' + label + +def bump_on_change_test(name, srcs = [], version_lock = None, version = None, run_on_main = False): + tags = [] + + if not run_on_main: + tags = [ 'do_not_run_on_main' ] + + hashes_name = name + "_version_lock_validator" + hashes( + name = hashes_name, + srcs = srcs + [ version ], + ) + + generated_file_test( + name = name, + generated = hashes_name, + src = version_lock, + tags = tags + [ 'version_check' ] + ) + + py_binary( + name = name + ".bump", + srcs = [ "//bzl/versioning:bump.py" ], + main = "//bzl/versioning:bump.py", + data = [ version, hashes_name, version_lock ], + args = [ + "--to_bump_in", "$(rootpath " + version + ")", + "--to_bump_out", "$(rootpath " + version + ")", + "--lockfile_build_label", _absolute_label(hashes_name), + "--lockfile_build_rootpath", "$(rootpath " + hashes_name + ")", + "--lockfile_out_rootpath", "$(rootpath " + version_lock + ")" + ] + ) + + + + + + diff --git a/bzl/versioning/test/bump_on_change_test/BUILD b/bzl/versioning/test/bump_on_change_test/BUILD new file mode 100644 index 0000000000..3e9169db0c --- /dev/null +++ b/bzl/versioning/test/bump_on_change_test/BUILD @@ -0,0 +1,9 @@ +load("//bzl/versioning:rules.bzl", "bump_on_change_test") + +bump_on_change_test( + name = "version", + srcs = [ "contents.txt" ], + version_lock = "version.lock", + version = "MAJOR", + run_on_main = True +) \ No newline at end of file diff --git a/bzl/versioning/test/bump_on_change_test/MAJOR b/bzl/versioning/test/bump_on_change_test/MAJOR new file mode 100644 index 0000000000..c7930257df --- /dev/null +++ b/bzl/versioning/test/bump_on_change_test/MAJOR @@ -0,0 +1 @@ +7 \ No newline at end of file diff --git a/bzl/versioning/test/bump_on_change_test/contents.txt b/bzl/versioning/test/bump_on_change_test/contents.txt new file mode 100644 index 0000000000..442483cd5f --- /dev/null +++ b/bzl/versioning/test/bump_on_change_test/contents.txt @@ -0,0 +1 @@ +This is some content of our program \ No newline at end of file diff --git a/bzl/versioning/test/bump_on_change_test/version.lock b/bzl/versioning/test/bump_on_change_test/version.lock new file mode 100644 index 0000000000..788eaea7c0 --- /dev/null +++ b/bzl/versioning/test/bump_on_change_test/version.lock @@ -0,0 +1,2 @@ +040e04c69d4995ce0f768474f63263ccc87c3670b137516fcc1213a8b0f29406 bzl/versioning/test/bump_on_change_test/contents.txt +7902699be42c8a8e46fbbb4501726517e86b22c56a189f7625a6da49081b2451 bzl/versioning/test/bump_on_change_test/MAJOR diff --git a/bzl/versioning/test/1/BUILD b/bzl/versioning/test/semver_version/BUILD similarity index 100% rename from bzl/versioning/test/1/BUILD rename to bzl/versioning/test/semver_version/BUILD diff --git a/bzl/versioning/test/1/MAJOR b/bzl/versioning/test/semver_version/MAJOR similarity index 100% rename from bzl/versioning/test/1/MAJOR rename to bzl/versioning/test/semver_version/MAJOR diff --git a/bzl/versioning/test/1/MINOR b/bzl/versioning/test/semver_version/MINOR similarity index 100% rename from bzl/versioning/test/1/MINOR rename to bzl/versioning/test/semver_version/MINOR diff --git a/bzl/versioning/test/1/PATCH b/bzl/versioning/test/semver_version/PATCH similarity index 100% rename from bzl/versioning/test/1/PATCH rename to bzl/versioning/test/semver_version/PATCH diff --git a/bzl/versioning/test/1/expected.txt b/bzl/versioning/test/semver_version/expected.txt similarity index 100% rename from bzl/versioning/test/1/expected.txt rename to bzl/versioning/test/semver_version/expected.txt From d690982fe8aa138ba3e6da37fe02f69f046eff50 Mon Sep 17 00:00:00 2001 From: zemnmez Date: Sun, 5 Jun 2022 21:02:50 -0700 Subject: [PATCH 2/2] maybe it's automatic now?? --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 470bf09e20..1b4f2bfb8f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -102,5 +102,5 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Set PR to merge automatically - run: gh pr merge --auto --merge --head main --base versioned + run: gh pr merge versioned --auto \ No newline at end of file