Skip to content

Commit

Permalink
GitHub action enhancements
Browse files Browse the repository at this point in the history
- change to working directory
- install `pip-requirements-parser` in a separate step
- include Darker exit code in outputs
- include Darker standard output in outputs
- run `commit-range` action locally
  • Loading branch information
akaihola committed Jul 29, 2024
1 parent d5f5c7b commit 06dfd00
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
22 changes: 20 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -37,8 +50,13 @@ runs:
steps:
- name: Commit Range
id: commit-range
uses: akaihola/darker/.github/actions/[email protected]
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
Expand All @@ -56,7 +74,6 @@ runs:
sys.exit(proc.returncode)
"
pip install pip-requirements-parser
if [ "$RUNNER_OS" == "Windows" ]; then
echo $entrypoint | python
else
Expand All @@ -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
60 changes: 47 additions & 13 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}<<DARKER_ACTION_EOF", file=f)
print(val, file=f, end="" if val.endswith("\n") else "\n")
print("DARKER_ACTION_EOF", file=f)
else:
print(f"{key}={val}", file=f)


def exit(exitcode: int) -> 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]}"
Expand All @@ -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")]
Expand All @@ -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)

0 comments on commit 06dfd00

Please sign in to comment.