From 55a46b85e7aaccc8dd56c350f2875d3267831698 Mon Sep 17 00:00:00 2001 From: Stefan Foulis Date: Sat, 23 Jan 2021 00:51:00 +0100 Subject: [PATCH 1/5] Add black_version to github action --- CHANGES.md | 2 ++ action.yml | 15 +++++++++++++-- action/Dockerfile | 10 ---------- 3 files changed, 15 insertions(+), 12 deletions(-) delete mode 100644 action/Dockerfile diff --git a/CHANGES.md b/CHANGES.md index 97a3be33c93..82903ef0a6b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -45,6 +45,8 @@ - PR #2053: Python 2 support is now optional, install with `python3 -m pip install black[python2]` to maintain support. +- allow choosing black version with github action (#1940) + #### _Packaging_ - Self-contained native _Black_ binaries are now provided for releases via GitHub diff --git a/action.yml b/action.yml index 59b16a9fb6c..4d2ec1ac42b 100644 --- a/action.yml +++ b/action.yml @@ -6,9 +6,20 @@ inputs: description: "Black input arguments." required: false default: "" + black_version: + description: 'Version of black to use. E.g "==20.8b1"' + required: false + default: "" branding: color: "black" icon: "check-circle" runs: - using: "docker" - image: "action/Dockerfile" + using: composite + steps: + - run: pip install --upgrade --no-cache-dir black${{inputs.black_version}} + shell: bash + - id: run-black + run: + ${{ github.action_path }}/action/entrypoint.sh ${{ inputs.configuration }} ${{ + inputs.sortPaths }} + shell: bash diff --git a/action/Dockerfile b/action/Dockerfile deleted file mode 100644 index eb2209940db..00000000000 --- a/action/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM python:3 - -ENV PYTHONDONTWRITEBYTECODE 1 -ENV PYTHONUNBUFFERED 1 - -RUN pip install --upgrade --no-cache-dir black - -COPY entrypoint.sh /entrypoint.sh - -ENTRYPOINT ["/entrypoint.sh"] From fb23ede824ae0822ab5482c6eeffeb37f21168dc Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Sun, 23 May 2021 22:59:24 -0400 Subject: [PATCH 2/5] Add version support for the Black action pt.2 Since we're moving to a composite based action, quite a few changes were made. 1) Support was added for all OSes (Windows was painful). 2) Isolation from the rest of the workflow had to be done manually with a virtual environment. Other noteworthy changes: - Rewrote basically all of the logic and put it in a Python script for easy testing (not doing it here tho cause I'm lazy and I can't think of a reasonable way of testing it). - Renamed `black_version` to `version` to better fit the existing input naming scheme. - Added support for log groups, this makes our action's output a bit more fancy (I may or may have not added some debug output too). --- action.yml | 43 +++++++++++++++++++++++++++--------- action/entrypoint.sh | 9 -------- action/main.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 19 deletions(-) delete mode 100755 action/entrypoint.sh create mode 100644 action/main.py diff --git a/action.yml b/action.yml index 66a9e5d6bee..dbf7bcb0e40 100644 --- a/action.yml +++ b/action.yml @@ -4,20 +4,20 @@ author: "Ɓukasz Langa and contributors to Black" inputs: options: description: - "Options passed to black. Use `black --help` to see available options. Default: + "Options passed to Black. Use `black --help` to see available options. Default: '--check'" required: false default: "--check --diff" src: - description: "Source to run black. Default: '.'" + description: "Source to run Black. Default: '.'" required: false default: "." black_args: description: "[DEPRECATED] Black input arguments." required: false default: "" - black_version: - description: 'Version of black to use. E.g "==20.8b1"' + version: + description: 'Python Version specifier (PEP440) - e.g. "21.5b1"' required: false default: "" branding: @@ -26,10 +26,33 @@ branding: runs: using: composite steps: - - run: pip install --upgrade --no-cache-dir black${{inputs.black_version}} - shell: bash - - id: run-black - run: - ${{ github.action_path }}/action/entrypoint.sh ${{ inputs.configuration }} ${{ - inputs.sortPaths }} + - run: | + # Exists since using github.action_path + path to main script doesn't work because bash + # interprets the bashslashes in github.action_path (which are used when the runner OS + # is Windows) destroying the path to the target file. + + # Also semicolons are necessary because I can't get the newlines to work + + entrypoint="import sys; + import subprocess; + from pathlib import Path; + + MAIN_SCRIPT = Path(r'${{ github.action_path }}') / 'action' / 'main.py'; + + proc = subprocess.run([sys.executable, str(MAIN_SCRIPT)]); + sys.exit(proc.returncode) + " + + if [ "$RUNNER_OS" == "Windows" ]; then + echo $entrypoint | python + else + echo $entrypoint | python3 + fi + env: + # TODO: Remove once https://github.com/actions/runner/issues/665 is fixed. + INPUT_OPTIONS: ${{ inputs.options }} + INPUT_SRC: ${{ inputs.src }} + INPUT_BLACK_ARGS: ${{ inputs.black_args }} + INPUT_VERSION: ${{ inputs.version }} + pythonioencoding: utf-8 shell: bash diff --git a/action/entrypoint.sh b/action/entrypoint.sh deleted file mode 100755 index 30bf4eb688f..00000000000 --- a/action/entrypoint.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -e - -if [ -n "$INPUT_BLACK_ARGS" ]; then - echo '::warning::Input `with.black_args` is deprecated. Use `with.options` and `with.src` instead.' - black $INPUT_BLACK_ARGS - exit $? -fi - -black $INPUT_OPTIONS $INPUT_SRC diff --git a/action/main.py b/action/main.py new file mode 100644 index 00000000000..b5fc4590534 --- /dev/null +++ b/action/main.py @@ -0,0 +1,52 @@ +import os +import shlex +import sys +from pathlib import Path +from subprocess import run, PIPE, STDOUT + +ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"]) +ENV_PATH = ACTION_PATH / ".black-env" +ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin") +OPTIONS = os.getenv("INPUT_OPTIONS", default="") +SRC = os.getenv("INPUT_SRC", default="") +BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="") +VERSION = os.getenv("INPUT_VERSION", default="") + +# TODO: Uncomment these once https://github.com/actions/runner/issues/664 is resolved, +# right now these cause more confusion than clarity :( +# print("::group:: Setup & Install Black") + +run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True) +# print(f"Created virtual environment at `{ENV_PATH!s}`.") + +req = "black[colorama,python2]" +if VERSION: + req += f"=={VERSION}" +# TODO: remove output capturing / hiding once the log grouping works (because the logs +# aren't out of order) +pip_proc = run( + [str(ENV_BIN / "python"), "-m", "pip", "install", req], + stdout=PIPE, + stderr=STDOUT, + encoding="utf-8", +) +if pip_proc.returncode: + print(pip_proc.stdout) + print("::error::Failed to install Black.") + sys.exit(pip_proc.returncode) + +# print("::endgroup::") +# print("::group:: Run Black") + +base_cmd = [str(ENV_BIN / "black")] +if BLACK_ARGS: + # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS. + print( + "::warning::Input `with.black_args` is deprecated. Use `with.options` and `with.src` instead." + ) + proc = run([*base_cmd, *shlex.split(BLACK_ARGS)]) +else: + proc = run([*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)]) + +# print("::endgroup::") +sys.exit(proc.returncode) From fb531573865414ac38754f852ad2404b3eeaec6e Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Wed, 19 May 2021 15:50:44 -0400 Subject: [PATCH 3/5] Add more to and sorta rewrite the Action's docs Reflect compatability and gotchas. --- docs/integrations/github_actions.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/integrations/github_actions.md b/docs/integrations/github_actions.md index 9e8cf436453..9d4e7a8120e 100644 --- a/docs/integrations/github_actions.md +++ b/docs/integrations/github_actions.md @@ -3,6 +3,14 @@ You can use _Black_ within a GitHub Actions workflow without setting your own Python environment. Great for enforcing that your code matches the _Black_ code style. +## Compatiblity + +This action is known to support all GitHub-hosted runner OSes. In addition, only +published versions of _Black_ are supported (i.e. whatever is available on PyPI). + +Finally, this action installs _Black_ with both the `colorama` and `python2` extras so +the `--color` flag and formatting Python 2 code are supported. + ## Usage Create a file named `.github/workflows/black.yml` inside your repository with: @@ -17,19 +25,26 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 - uses: psf/black@stable ``` We recommend the use of the `@stable` tag, but per version tags also exist if you prefer -that. +that. Note that the action's version you select is independent of the version of _Black_ +the action will use. + +The version of _Black_ the action will use can configured via `version`. The action +defaults to the latest release available on PyPI. Only versions available from PyPI are +supported, so no commit SHAs or branch names. + +You can also configure the arguments passed to _Black_ via `options` (defaults to +`'--check --diff'`) and `src` (default is `'.'`) -You may use `options` (Default is `'--check --diff'`) and `src` (Default is `'.'`) as -follows: +Here's an example configuration: ```yaml - uses: psf/black@stable with: options: "--check --verbose" src: "./src" + version: "21.5b1" ``` From 4881c7ba2b14011e45850a2fa10d659416bc3ba1 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Sat, 29 May 2021 16:40:11 -0400 Subject: [PATCH 4/5] Add CHANGELOG entry --- CHANGES.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 603554cd8b7..d43b3fb444e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,11 @@ - Add a lower bound for the `aiohttp-cors` dependency. Only 0.4.0 or higher is supported. (#2231) +### Integrations + +- The official Black action now supports choosing what version to use, and supports the + major 3 OSes. (#1940) + ### Documentation - Fix typos discovered by codespell (#2228) From 2e40c836bb3142718a3b42967fbce2c31fa5d9e6 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Mon, 31 May 2021 17:22:37 -0400 Subject: [PATCH 5/5] Remove debug; address typos; clean up action.yml --- action.yml | 7 ++++--- action/main.py | 15 +-------------- docs/integrations/github_actions.md | 2 +- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/action.yml b/action.yml index dbf7bcb0e40..ddf07933a3e 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,8 @@ inputs: description: "[DEPRECATED] Black input arguments." required: false default: "" + deprecationMessage: + "Input `with.black_args` is deprecated. Use `with.options` and `with.src` instead." version: description: 'Python Version specifier (PEP440) - e.g. "21.5b1"' required: false @@ -28,11 +30,10 @@ runs: steps: - run: | # Exists since using github.action_path + path to main script doesn't work because bash - # interprets the bashslashes in github.action_path (which are used when the runner OS + # interprets the backslashes in github.action_path (which are used when the runner OS # is Windows) destroying the path to the target file. - + # # Also semicolons are necessary because I can't get the newlines to work - entrypoint="import sys; import subprocess; from pathlib import Path; diff --git a/action/main.py b/action/main.py index b5fc4590534..fde312553bf 100644 --- a/action/main.py +++ b/action/main.py @@ -12,18 +12,11 @@ BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="") VERSION = os.getenv("INPUT_VERSION", default="") -# TODO: Uncomment these once https://github.com/actions/runner/issues/664 is resolved, -# right now these cause more confusion than clarity :( -# print("::group:: Setup & Install Black") - run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True) -# print(f"Created virtual environment at `{ENV_PATH!s}`.") req = "black[colorama,python2]" if VERSION: req += f"=={VERSION}" -# TODO: remove output capturing / hiding once the log grouping works (because the logs -# aren't out of order) pip_proc = run( [str(ENV_BIN / "python"), "-m", "pip", "install", req], stdout=PIPE, @@ -32,21 +25,15 @@ ) if pip_proc.returncode: print(pip_proc.stdout) - print("::error::Failed to install Black.") + print("::error::Failed to install Black.", flush=True) sys.exit(pip_proc.returncode) -# print("::endgroup::") -# print("::group:: Run Black") base_cmd = [str(ENV_BIN / "black")] if BLACK_ARGS: # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS. - print( - "::warning::Input `with.black_args` is deprecated. Use `with.options` and `with.src` instead." - ) proc = run([*base_cmd, *shlex.split(BLACK_ARGS)]) else: proc = run([*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)]) -# print("::endgroup::") sys.exit(proc.returncode) diff --git a/docs/integrations/github_actions.md b/docs/integrations/github_actions.md index 9d4e7a8120e..d293c40dadf 100644 --- a/docs/integrations/github_actions.md +++ b/docs/integrations/github_actions.md @@ -32,7 +32,7 @@ We recommend the use of the `@stable` tag, but per version tags also exist if yo that. Note that the action's version you select is independent of the version of _Black_ the action will use. -The version of _Black_ the action will use can configured via `version`. The action +The version of _Black_ the action will use can be configured via `version`. The action defaults to the latest release available on PyPI. Only versions available from PyPI are supported, so no commit SHAs or branch names.