diff --git a/action.yml b/action.yml index 1fc6b8bf6..8be6f4631 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,19 @@ inputs: Example: flake8,pylint==2.13.1 required: false default: '' + working-directory: + description: >- + Directory to run Darker in, either absolute or relative to + $GITHUB_WORKSPACE. By default, Darker is run in $GITHUB_WORKSPACE. + required: false + default: '.' +outputs: + exitcode: + description: "Exit code of Darker" + value: ${{ steps.darker.outputs.exitcode }} + stdout: + description: "Standard output of Darker" + value: ${{ steps.darker.outputs.stdout }} branding: color: "black" icon: "check-circle" @@ -37,8 +50,13 @@ runs: steps: - name: Commit Range id: commit-range - uses: akaihola/darker/.github/actions/commit-range@1.7.1 + uses: ./.github/actions/commit-range + - name: Install dependencies + run: | + pip install pip-requirements-parser + shell: bash - name: Run Darker + id: darker run: | # Exists since using github.action_path + path to main script doesn't # work because bash interprets the backslashes in github.action_path @@ -56,7 +74,6 @@ runs: sys.exit(proc.returncode) " - pip install pip-requirements-parser if [ "$RUNNER_OS" == "Windows" ]; then echo $entrypoint | python else @@ -71,5 +88,6 @@ runs: INPUT_REVISION: ${{ inputs.revision }} INPUT_LINT: ${{ inputs.lint }} INPUT_COMMIT_RANGE: ${{ steps.commit-range.outputs.commit-range }} + INPUT_WORKING_DIRECTORY: ${{ inputs.working-directory }} pythonioencoding: utf-8 shell: bash diff --git a/action/main.py b/action/main.py index e196079c9..0d7aef9d2 100644 --- a/action/main.py +++ b/action/main.py @@ -17,10 +17,52 @@ VERSION = os.getenv("INPUT_VERSION", default="") LINT = os.getenv("INPUT_LINT", default="") REVISION = os.getenv("INPUT_REVISION") or os.getenv("INPUT_COMMIT_RANGE") or "HEAD^" +WORKING_DIRECTORY = os.getenv("INPUT_WORKING_DIRECTORY", ".") + + +def set_github_output(key: str, val: str) -> None: + """Write a key-value pair to the output file.""" + with Path(os.environ["GITHUB_OUTPUT"]).open("a", encoding="UTF-8") as f: + if "\n" in val: + print(f"{key}< None: + """Write the exit code to the output file and exit with it.""" + set_github_output("exitcode", str(exitcode)) + sys.exit(exitcode) + + +# Check if the working directory exists +if not os.path.isdir(WORKING_DIRECTORY): + print(f"::error::Working directory does not exist: {WORKING_DIRECTORY}", flush=True) + exit(21) + + +def pip_install(*packages): + """Install the specified Python packages using a pip subprocess.""" + python = str(ENV_BIN / "python") + args = [python, "-m", "pip", "install", *packages] + pip_proc = run( # nosec + args, + check=False, + stdout=PIPE, + stderr=STDOUT, + encoding="utf-8", + ) + print(pip_proc.stdout, end="") + if pip_proc.returncode: + print(f"::error::Failed to install {' '.join(packages)}.", flush=True) + sys.exit(pip_proc.returncode) + run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True) # nosec -req = ["darker[color,isort]"] +req = ["darker[color, isort]"] if VERSION: if VERSION.startswith("@"): req[0] = f"git+https://github.com/akaihola/darker{VERSION}#egg={req[0]}" @@ -41,17 +83,7 @@ req.append(str(linter_requirement)) linter_options.extend(["--lint", linter]) -pip_proc = run( # nosec - [str(ENV_BIN / "python"), "-m", "pip", "install"] + req, - check=False, - stdout=PIPE, - stderr=STDOUT, - encoding="utf-8", -) -print(pip_proc.stdout, end="") -if pip_proc.returncode: - print(f"::error::Failed to install {' '.join(req)}.", flush=True) - sys.exit(pip_proc.returncode) +pip_install(*req) base_cmd = [str(ENV_BIN / "darker")] @@ -69,7 +101,9 @@ stderr=STDOUT, env={**os.environ, "PATH": f"{ENV_BIN}:{os.environ['PATH']}"}, encoding="utf-8", + cwd=WORKING_DIRECTORY, ) print(proc.stdout, end="") -sys.exit(proc.returncode) +set_github_output("stdout", proc.stdout) +exit(proc.returncode)