Unverified Commit 4005246f authored by Stefan Foulis's avatar Stefan Foulis Committed by GitHub
Browse files

Add `version` to github action (and rewrite the whole thing while at it) (#1940)

Commit history before merge:

* Add black_version to github action
* Merge upstream/main into this branch
* 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).

* Add more to and sorta rewrite the Action's docs

  Reflect compatability and gotchas.

* Add CHANGELOG entry
* Merge main into this branch
* Remove debug; address typos; clean up action.yml

Co-authored-by: default avatarRichard Si <63936253+ichard26@users.noreply.github.com>
parent a4e35b31
Loading
Loading
Loading
Loading
+6 −1
Changes for CHANGES.md: 6 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -25,7 +25,12 @@
- Add a lower bound for the `aiohttp-cors` dependency. Only 0.4.0 or higher is
  supported. (#2231)

### _Packaging_
### Integrations

- The official Black action now supports choosing what version to use, and supports the
  major 3 OSes. (#1940)

### Packaging

- Release self-contained x86_64 MacOS binaries as part of the GitHub release pipeline
  (#2198)
+39 −4
Changes for action.yml: 39 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -4,21 +4,56 @@ 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: ""
    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
    default: ""
branding:
  color: "black"
  icon: "check-circle"
runs:
  using: "docker"
  image: "action/Dockerfile"
  using: composite
  steps:
    - run: |
        # Exists since using github.action_path + path to main script doesn't work because bash
        # 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;

        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

action/Dockerfile

deleted100644 → 0
+0 −10
Changes for action/Dockerfile: 0 added lines, 10 removed lines.
Original line number Diff line number Diff line
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"]

action/entrypoint.sh

deleted100755 → 0
+0 −9
Changes for action/entrypoint.sh: 0 added lines, 9 removed lines.
Original line number Diff line number Diff line
#!/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

action/main.py

0 → 100644
+39 −0
Changes for action/main.py: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
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="")

run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True)

req = "black[colorama,python2]"
if VERSION:
    req += f"=={VERSION}"
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.", flush=True)
    sys.exit(pip_proc.returncode)


base_cmd = [str(ENV_BIN / "black")]
if BLACK_ARGS:
    # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS.
    proc = run([*base_cmd, *shlex.split(BLACK_ARGS)])
else:
    proc = run([*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)])

sys.exit(proc.returncode)
Loading