Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
James Curtin committed Oct 5, 2020
0 parents commit b61edb2
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# isort Github Action

This action runs isort on a Python repository.

It requires that the [`checkout`][github-checkout] and [`setup-python`][github-setup-python] actions be used first.

## Inputs

### `isortVersion`

Optional. Version of `isort` to use. Defaults to latest version of `isort`.

### `sortPaths`

Optional. List of paths to sort, relative to your project root. Defaults to `.`

### `configuration`

Optional. `isort` configuration options to pass to the `isort` CLI. Defaults to `-check-only --diff`.

### `requirementsFiles`

Optional. Paths to python requirements files to install before running isort.
If multiple requirements files are provided, they should be separated by a space.
If custom package installation is required, dependencies should be installed in a separate step before using this action.

## Outputs

### `isort-result`

Output of the `isort` CLI.

## Example usage

```yaml
name: Run isort
on:
- push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.8
- uses: jamescurtin/isort-action@master
with:
requirementsFiles: "requirements.txt requirements-test.txt"
```
[github-checkout]: https://github.com/actions/checkout
[github-setup-python]: https://github.com/actions/setup-python
37 changes: 37 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "python-isort"
author: "jamescurtin"
description: "Run isort on a Python project"

inputs:
isortVersion:
description: "Version of isort to use"
required: false
default: ""
sortPaths:
description: "files or directories to sort"
required: false
default: "."
configuration:
description: "isort configuration options"
required: false
default: "--check-only --diff"
requirementsFiles:
description: "path(s) to requirements files that should be installed to properly configure third-party imports"
required: false
default: ""

outputs:
isort-result:
description: "isort result"
value: ${{ steps.run-isort.outputs.isort-output }}

runs:
using: "composite"
steps:
- run: ${{ github.action_path }}/bin/ensure_python
shell: bash
- run: ${{ github.action_path }}/bin/install_packages "${{ inputs.isortVersion }}" "${{ inputs.requirementsFiles }}"
shell: bash
- id: run-isort
run: ${{ github.action_path }}/bin/run_isort "${{ inputs.configuration }}" "${{ inputs.sortPaths }}"
shell: bash
12 changes: 12 additions & 0 deletions bin/ensure_python
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# If Python 3 isn't installed, exit
python3 -V > /dev/null 2>&1 || (echo "python3 is not available. Use the setup-python action" && exit 1)

# Make sure we're using a supported version of Python
major_version=$(python3 -c 'import sys; print(sys.version_info[0])')
minor_version=$(python3 -c 'import sys; print(sys.version_info[1])')
if [ $major_version -lt 3 ] || [ $minor_version -lt 6 ]; then
echo "Minimum supported version of python is 3.6, but $(python3 -V) is installed"
exit 1
fi
18 changes: 18 additions & 0 deletions bin/install_packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

isort_version=$1
requirements_files=$2

if [ -z "$isort_version" ]; then
echo "Installing latest version of isort"
pip3 install isort[requirements_deprecated_finder,pipfile_deprecated_finder] > /dev/null 2>&1
else
echo "Installing isort==$isort_version"
pip3 install isort[requirements_deprecated_finder,pipfile_deprecated_finder]==$isort_version > /dev/null 2>&1
fi

for file in $requirements_files
do
echo "Running pip install -r $file"
pip install -r $file > /dev/null 2>&1
done
11 changes: 11 additions & 0 deletions bin/run_isort
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

configuration=$1
sort_paths=$2

echo "Running isort $configuration $sort_paths"
isort_result=$(isort $configuration $sort_paths)
exit_code=$?

echo "::set-output name=isort-output::${isort_result}"
exit $exit_code

0 comments on commit b61edb2

Please sign in to comment.