Repository: psf/black

Issue:
GitHub Action: Use action version as default Black version, instead of latest
> I'm alright with making the default Black version tied to the action version being used. For context `version` was introduced because the action didn't exist for a long time so tying black version to action version wouldn't work for version 19.10b0 for example. In hidesight, having the default being the action version keeping the `version` configuration option around as an escape hatch is the better solution. This will involve some complexity since commit SHAs aren't supported by the version code (but are by GHA) but there might be some pre-existing logic in scripts/diff_shades_gha_helper.py we could reuse.

_Originally posted by @ichard26 in https://github.com/psf/black/issues/1140#issuecomment-1026379455_
      


Discussion:
Currently, when `dependabot` highlights the `black-github-action` need to be updated and we choose to ignore that update, because we want to stick with that version, the code will be checked/formatted with the new release!

Here's an example:

```
      - name: 'Check code with black'
        id: black
        uses: psf/black@22.8.0
        with:
          options: '--target-version py39 --line-length 120 --diff --check'
```

Even being aware of the documentation, thus defining the desired `version`, may lead to an unintended configuration:

```
      - name: 'Check code with black'
        id: black
        uses: psf/black@22.10.0 # <-- dependabot update!
        with:
          options: '--target-version py39 --line-length 120 --diff --check'
          version: '22.8.0'
```

So, definitely I'm in favour of tying the _Black_ version with the action version. :smiley:

---

On the web site one can read:

> The version of _Black_ the action will use can be configured via `version`. This can be any [valid version specifier](https://packaging.python.org/en/latest/glossary/#term-Version-Specifier) or just the version number if you want an exact 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.

On the action YML one can read:

```
  version:
    description: 'Python Version specifier (PEP440) - e.g. "21.5b1"'
    required: false
    default: ""
```

Notice that the field description gives the idea that it's purpose is to set the python version, or maybe the `--target-version`, but the documentation clearly states it's intended to define the _Black_ version to use. :confused:


Resolving patch:
diff --git a/action/main.py b/action/main.py
--- a/action/main.py
+++ b/action/main.py
@@ -22,12 +22,34 @@
     extra_deps = "[colorama,jupyter]"
 else:
     extra_deps = "[colorama]"
-req = f"black{extra_deps}{version_specifier}"
+if version_specifier:
+    req = f"black{extra_deps}{version_specifier}"
+else:
+    describe_name = ""
+    with open(ACTION_PATH / ".git_archival.txt", encoding="utf-8") as fp:
+        for line in fp:
+            if line.startswith("describe-name: "):
+                describe_name = line[len("describe-name: ") :].rstrip()
+                break
+    if not describe_name:
+        print("::error::Failed to detect action version.", flush=True)
+        sys.exit(1)
+    # expected format is one of:
+    # - 23.1.0
+    # - 23.1.0-51-g448bba7
+    if describe_name.count("-") < 2:
+        # the action's commit matches a tag exactly, install exact version from PyPI
+        req = f"black{extra_deps}=={describe_name}"
+    else:
+        # the action's commit does not match any tag, install from the local git repo
+        req = f".{extra_deps}"
+print(f"Installing {req}...", flush=True)
 pip_proc = run(
     [str(ENV_BIN / "python"), "-m", "pip", "install", req],
     stdout=PIPE,
     stderr=STDOUT,
     encoding="utf-8",
+    cwd=ACTION_PATH,
 )
 if pip_proc.returncode:
     print(pip_proc.stdout)


Test patch: