From ad2cc6729623ae4c5b4e555939cb848a820d157c Mon Sep 17 00:00:00 2001 From: "David W.H. Swenson" Date: Mon, 23 Nov 2020 15:43:37 +0100 Subject: [PATCH 1/4] New CLI skeleton; support for vendoring GH actions --- .../autorelease-default-env.sh | 4 ++ .../gh_actions_stages/autorelease-deploy.yml | 31 ++++++++++ .../gh_actions_stages/autorelease-gh-rel.yml | 27 +++++++++ .../gh_actions_stages/autorelease-prep.yml | 56 +++++++++++++++++++ autorelease/scripts/cli.py | 22 ++++++++ autorelease/scripts/vendor.py | 23 ++++++++ setup.cfg | 6 ++ 7 files changed, 169 insertions(+) create mode 100644 autorelease/gh_actions_stages/autorelease-default-env.sh create mode 100644 autorelease/gh_actions_stages/autorelease-deploy.yml create mode 100644 autorelease/gh_actions_stages/autorelease-gh-rel.yml create mode 100644 autorelease/gh_actions_stages/autorelease-prep.yml create mode 100644 autorelease/scripts/cli.py create mode 100644 autorelease/scripts/vendor.py diff --git a/autorelease/gh_actions_stages/autorelease-default-env.sh b/autorelease/gh_actions_stages/autorelease-default-env.sh new file mode 100644 index 0000000..5609e9b --- /dev/null +++ b/autorelease/gh_actions_stages/autorelease-default-env.sh @@ -0,0 +1,4 @@ +INSTALL_AUTORELEASE="python -m pip install autorelease==0.2.3" +DIR=`dirname $0` +source autorelease-env.sh + diff --git a/autorelease/gh_actions_stages/autorelease-deploy.yml b/autorelease/gh_actions_stages/autorelease-deploy.yml new file mode 100644 index 0000000..89aaeac --- /dev/null +++ b/autorelease/gh_actions_stages/autorelease-deploy.yml @@ -0,0 +1,31 @@ +name: Autorelease +on: + release: + types: [published] + +jobs: + deploy_pypi: + runs-on: ubuntu-latest + name: "Deploy to PyPI" + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: "3.x" + - run: | # TODO: move this to an action + source ./.github/workflows/autorelease-default-env.sh + cat autorelease-env.sh >> $GITHUB_ENV + eval $INSTALL_AUTORELEASE + name: "Install autorelease" + - run: | + python -m pip install twine wheel + name: "Install release tools" + - run: | + python setup.py sdist bdist_wheel + twine check dist/* + name: "Build and check package" + - uses: pypa/gh-action-pypi-publish@master + with: + password: ${{ secrets.pypi_password }} + name: "Deploy to testpypi" + diff --git a/autorelease/gh_actions_stages/autorelease-gh-rel.yml b/autorelease/gh_actions_stages/autorelease-gh-rel.yml new file mode 100644 index 0000000..f9e294e --- /dev/null +++ b/autorelease/gh_actions_stages/autorelease-gh-rel.yml @@ -0,0 +1,27 @@ +name: Autorelease +on: + push: + branches: + - stable + +jobs: + release-gh: + runs-on: ubuntu-latest + name: "Cut release" + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: "3.7" + - run: | # TODO: move this to an action + source ./.github/workflows/autorelease-default-env.sh + cat autorelease-env.sh >> $GITHUB_ENV + eval $INSTALL_AUTORELEASE + name: "Install autorelease" + - run: | + VERSION=`python setup.py --version` + PROJECT=`python setup.py --name` + echo $PROJECT $VERSION + autorelease-release --project $PROJECT --version $VERSION --token $AUTORELEASE_TOKEN + env: + AUTORELEASE_TOKEN: ${{ secrets.AUTORELEASE_TOKEN }} diff --git a/autorelease/gh_actions_stages/autorelease-prep.yml b/autorelease/gh_actions_stages/autorelease-prep.yml new file mode 100644 index 0000000..48c82ba --- /dev/null +++ b/autorelease/gh_actions_stages/autorelease-prep.yml @@ -0,0 +1,56 @@ +name: "Autorelease" +on: + pull_request: + branches: + - stable + +defaults: + run: + shell: bash + +jobs: + deploy_testpypi: + runs-on: ubuntu-latest + name: "Deployment test" + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: "3.x" + - run: | # TODO: move this to an action + source ./.github/workflows/autorelease-default-env.sh + cat autorelease-env.sh >> $GITHUB_ENV + eval $INSTALL_AUTORELEASE + name: "Install autorelease" + - run: | + python -m pip install twine wheel + name: "Install release tools" + - run: | + bump-dev-version + python setup.py --version + name: "Bump testpypi dev version" + - run: | + python setup.py sdist bdist_wheel + twine check dist/* + name: "Build and check package" + - uses: pypa/gh-action-pypi-publish@master + with: + password: ${{ secrets.testpypi_password }} + repository_url: https://test.pypi.org/legacy/ + name: "Deploy to testpypi" + test_testpypi: + runs-on: ubuntu-latest + name: "Test deployed" + needs: deploy_testpypi + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: "3.x" + - run: | # TODO: move this to an action + source ./.github/workflows/autorelease-default-env.sh + cat autorelease-env.sh >> $GITHUB_ENV + eval $INSTALL_AUTORELEASE + name: "Install autorelease" + - run: test-testpypi + diff --git a/autorelease/scripts/cli.py b/autorelease/scripts/cli.py new file mode 100644 index 0000000..624d90b --- /dev/null +++ b/autorelease/scripts/cli.py @@ -0,0 +1,22 @@ +import click + +from autorelease.scripts.vendor import vendor_actions + +@click.group() +def cli(): + pass + + +@click.group() +def vendor(): + pass + +@vendor.command() +def actions(): + print("vendoring actions") + vendor_actions(base_path='.') + +cli.add_command(vendor) + +if __name__ == "__main__": + cli() diff --git a/autorelease/scripts/vendor.py b/autorelease/scripts/vendor.py new file mode 100644 index 0000000..787415d --- /dev/null +++ b/autorelease/scripts/vendor.py @@ -0,0 +1,23 @@ +import pkg_resources +import pathlib +import shutil + +import click + +def vendor(resources, base_path, relative_target_dir): + for resource in resources: + orig_loc = pkg_resources.resource_filename('autorelease', resource) + name = pathlib.Path(orig_loc).name + target_dir = base_path / relative_target_dir + target_dir.mkdir(parents=True, exist_ok=True) + target_loc = base_path / relative_target_dir / name + # print(f"cp {orig_loc} {target_loc}") + shutil.copy(orig_loc, target_loc) + +def vendor_actions(base_path): + resources = ['autorelease-default-env.sh', 'autorelease-prep.yml', + 'autorelease-gh-rel.yml', 'autorelease-deploy.yml'] + resources = ['gh_actions_stages/' + res for res in resources] + target_dir = pathlib.Path('.github/workflows') + vendor(resources, base_path, target_dir) + diff --git a/setup.cfg b/setup.cfg index 4a6425c..8c1c9fc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,11 +35,16 @@ install_requires = future requests python-dateutil + click packages = find: scripts = script_stages/deploy-pypi script_stages/test-testpypi +[options.package_data] +autorelease = + - ../gh_actions_stages/* + [options.entry_points] console_scripts = autorelease-release = autorelease.scripts.release:main @@ -47,6 +52,7 @@ console_scripts = bump-dev-version = autorelease.scripts.bump_dev_version:main pypi-max-version = autorelease.scripts.bump_dev_version:get_max wait-for-testpypi = autorelease.scripts.bump_dev_version:wait_for_max + autorelease = autorelease.scripts.cli:cli [bdist_wheel] universal=1 From 897348a9f90a8d381f4344df6a2036f5f8561220 Mon Sep 17 00:00:00 2001 From: "David W.H. Swenson" Date: Tue, 24 Nov 2020 08:04:59 +0100 Subject: [PATCH 2/4] autorelease-env.sh is optional --- autorelease/gh_actions_stages/autorelease-default-env.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/autorelease/gh_actions_stages/autorelease-default-env.sh b/autorelease/gh_actions_stages/autorelease-default-env.sh index 5609e9b..74dd461 100644 --- a/autorelease/gh_actions_stages/autorelease-default-env.sh +++ b/autorelease/gh_actions_stages/autorelease-default-env.sh @@ -1,4 +1,5 @@ INSTALL_AUTORELEASE="python -m pip install autorelease==0.2.3" -DIR=`dirname $0` -source autorelease-env.sh +if [ -f autorelease-env.sh ]; then + source autorelease-env.sh +fi From 350d2970e8dfab91234ed0a086bbd177164a96d0 Mon Sep 17 00:00:00 2001 From: "David W.H. Swenson" Date: Tue, 24 Nov 2020 11:31:10 +0100 Subject: [PATCH 3/4] Fix autorelease for tags, too --- .github/workflows/unit-tests.yml | 2 +- autorelease/check_runners.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index a88fb06..96addf4 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -47,7 +47,7 @@ jobs: EVENT: ${{ github.event_name }} #EVENT_INFO: ${{ toJson(github.event) }} # useful debug run: | - if [ "$EVENT" = "push" ]; then + if [ "$EVENT" != "pull_request" ]; then BRANCH=$REF else BRANCH=$PR_BRANCH diff --git a/autorelease/check_runners.py b/autorelease/check_runners.py index 036fcec..1008cc1 100644 --- a/autorelease/check_runners.py +++ b/autorelease/check_runners.py @@ -93,6 +93,8 @@ def select_tests_from_sysargs(self): opts = parser.parse_args() if opts.branch.startswith('refs/heads/'): branch = opts.branch[11:] + elif opts.branch.startswith('refs/tags/'): + branch = opts.branch[10:] else: branch = opts.branch From 4e0500ce0da40543a894eb176c843067875f7d6a Mon Sep 17 00:00:00 2001 From: "David W.H. Swenson" Date: Tue, 24 Nov 2020 11:36:45 +0100 Subject: [PATCH 4/4] refactor; treat "schedule" same as "cron" --- autorelease/check_runners.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/autorelease/check_runners.py b/autorelease/check_runners.py index 1008cc1..c3e1804 100644 --- a/autorelease/check_runners.py +++ b/autorelease/check_runners.py @@ -83,6 +83,17 @@ def _reasonable_desired_version_test(self, allow_equal, ) ] + @staticmethod + def _get_branch_name(branch_name): + if branch_name.startswith('refs/heads/'): + branch = branch_name[11:] + elif branch_name.startswith('refs/tags/'): + branch = branch_name[10:] + else: + branch = branch_name + + return branch + def select_tests_from_sysargs(self): # TODO: this can be cleaned up by separating reusable parts parser = argparse.ArgumentParser() @@ -91,17 +102,13 @@ def select_tests_from_sysargs(self): parser.add_argument('--allow-patch-skip', action='store_true', default=False) opts = parser.parse_args() - if opts.branch.startswith('refs/heads/'): - branch = opts.branch[11:] - elif opts.branch.startswith('refs/tags/'): - branch = opts.branch[10:] - else: - branch = opts.branch + branch = self._get_branch_name(opts.branch) if branch in self.release_branches: print("TESTING AS RELEASE") allow_equal = (opts.event == 'cron' - or opts.branch == self.tag_branch) + or opts.event == 'schedule' + or branch == self.tag_branch) tests = (self.tests + self._reasonable_desired_version_test( allow_equal=allow_equal,