diff --git a/README.md b/README.md new file mode 100644 index 0000000..6e75168 --- /dev/null +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..547c0d9 --- /dev/null +++ b/action.yml @@ -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 diff --git a/bin/ensure_python b/bin/ensure_python new file mode 100755 index 0000000..00a55df --- /dev/null +++ b/bin/ensure_python @@ -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 diff --git a/bin/install_packages b/bin/install_packages new file mode 100755 index 0000000..76b7749 --- /dev/null +++ b/bin/install_packages @@ -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 diff --git a/bin/run_isort b/bin/run_isort new file mode 100755 index 0000000..903b366 --- /dev/null +++ b/bin/run_isort @@ -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