From c99bc118ff8ec2d674a4af472d3776e1507e64cd Mon Sep 17 00:00:00 2001 From: Richard Bullington-McGuire Date: Tue, 14 May 2024 16:16:58 -0400 Subject: [PATCH] Adapt isort-action more closely --- LICENSE | 2 ++ README.md | 2 +- action.yml | 45 ++++++++++++++++++++++++++++++++++++++--- bin/ensure_python | 12 +++++++++++ bin/install_packages | 22 ++++++++++++++++++++ bin/run_fawltydeps | 18 +++++++++++++++++ fawltydeps-matcher.json | 23 +++++++++++++++++++++ 7 files changed, 120 insertions(+), 4 deletions(-) create mode 100755 bin/ensure_python create mode 100755 bin/install_packages create mode 100755 bin/run_fawltydeps create mode 100644 fawltydeps-matcher.json diff --git a/LICENSE b/LICENSE index 49d29e7..4e95194 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,8 @@ MIT License Copyright (c) 2024 Tweag I/O Limited. +Copyright (c) 2022 James W. Curtin and Contributors +Copyright (c) 2018 Ɓukasz Langa Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 31e92f4..7f2fb78 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ More advanced example with extra command line options: ## Documentation -This action was inspired by the [black]() and [isort-action]() GitHub actions. It currently uses the Docker method of executing actions. +This action was inspired by and partially derived from the MIT-licensed [black](https://black.readthedocs.io/en/stable/integrations/github_actions.html) and [isort-action](https://github.com/isort/isort-action) GitHub actions. ## Development diff --git a/action.yml b/action.yml index 798a131..a230d5d 100644 --- a/action.yml +++ b/action.yml @@ -1,19 +1,58 @@ name: 'FawltyDeps' description: 'Check a Python project for _undeclared_ and _unused_ 3rd party dependencies' -# Inspired by and adapted from https://github.com/psf/black/blob/main/action.yml + # Inspired by and adapted from: + # https://github.com/psf/black/blob/main/action.yml + # https://github.com/isort/isort-action inputs: options: description: "Options passed to fawltydeps. Use `fawltydeps --help` to see available options. Default: ''" required: false default: "" + requirements-files: + description: > + path(s) to requirements files that should be installed to properly + configure third-party imports + required: false + paths: + description: > + files or directories to scan for dependency issues + required: false + default: . branding: color: "purple" icon: "check-circle" +outputs: + fawltydeps-result: + description: fawltydeps result + value: ${{ steps.run-fawltydeps.outputs.fawltydeps-output }} runs: - using: 'docker' - image: 'Dockerfile' + using: 'composite' + steps: + - run: $GITHUB_ACTION_PATH/bin/ensure_python + shell: bash + args: - fawltydeps - ${{ inputs.options }} +runs: + using: composite + steps: + - run: $GITHUB_ACTION_PATH/bin/ensure_python + shell: bash + - run: > + $GITHUB_ACTION_PATH/bin/install_packages + ${{ inputs.fawltydeps-version }} + ${{ inputs.requirements-files || inputs.requirementsFiles }} + shell: bash + - run: echo "::add-matcher::$GITHUB_ACTION_PATH/fawltydeps-matcher.json" + shell: bash + - id: run-isort + run: > + $GITHUB_ACTION_PATH/bin/run_fawltydeps + ${{ inputs.options }} + ${{ inputs.paths }} + shell: bash + - run: echo "::remove-matcher owner=fawltydeps-matcher::" + shell: bash diff --git a/bin/ensure_python b/bin/ensure_python new file mode 100755 index 0000000..f57215e --- /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..a2404ac --- /dev/null +++ b/bin/install_packages @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +isort_version=$1 +requirements_files=$2 + +echo "::group::Install isort" +if [ -z "$isort_version" ] || [ "$isort_version" == "latest" ]; then + echo "Installing latest version of isort" + python3 -m pip install "isort[requirements_deprecated_finder,pipfile_deprecated_finder]" +else + echo "Installing isort==$isort_version" + python3 -m pip install "isort[requirements_deprecated_finder,pipfile_deprecated_finder]==$isort_version" +fi +echo "::endgroup::" + +if [ -n "$requirements_files" ]; then + echo "::group::Install modules from requirements arg" + for file in $requirements_files; do + python3 -m pip install -r "$file" + done + echo "::endgroup::" +fi diff --git a/bin/run_fawltydeps b/bin/run_fawltydeps new file mode 100755 index 0000000..1f9fa9d --- /dev/null +++ b/bin/run_fawltydeps @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +echo "Running fawltydeps $*" +fawltydeps_result=$(fawltydeps "$@") +exit_code=$? + +# The fawltydeps output can be a multiline string. By default, GITHUB_OUTPUT expects +# output to be on a single line, so a (random) delimiter needs to be used +# so that the output is parsed properly. +# See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings +DELIMITER=$(echo $RANDOM | md5sum | head -c 20) +{ + echo "fawltydeps-output<<${DELIMITER}" + echo "${fawltydeps_result}" + echo "${DELIMITER}" +} >> "${GITHUB_OUTPUT}" + +exit $exit_code diff --git a/fawltydeps-matcher.json b/fawltydeps-matcher.json new file mode 100644 index 0000000..f5b7c58 --- /dev/null +++ b/fawltydeps-matcher.json @@ -0,0 +1,23 @@ +{ + "problemMatcher": [ + { + "owner": "fawltydeps-matcher", + "pattern": [ + { + "regexp": "(These imports appear to be .* dependencies):$", + "message": 1 + }, + { + "regexp": "^- '([^']+)' imported at:$", + "message": 1 + }, + { + "regexp": "^ (.+):([0-9]+)$", + "file": 1, + "line": 2, + "loop": true + } + ] + } + ] +}