From 38788addbad0910b2914eb276faf73f7314e863d Mon Sep 17 00:00:00 2001 From: rickstaa Date: Wed, 6 Jan 2021 18:57:19 +0100 Subject: [PATCH 1/4] Gets gh-action ready for marketplace release --- .github/workflows/test_gh_action.yml | 47 ++++++++++++++++++++++++++++ README.md | 11 +++++++ action.yml | 10 ++++++ action/entrypoint.sh | 22 ++++++++++--- 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/test_gh_action.yml diff --git a/.github/workflows/test_gh_action.yml b/.github/workflows/test_gh_action.yml new file mode 100644 index 00000000000..2640929fe00 --- /dev/null +++ b/.github/workflows/test_gh_action.yml @@ -0,0 +1,47 @@ +name: Test gh-action +on: + push: + paths: + - "action/**" + branches: + - main + pull_request: + paths: + - "action.yml" +jobs: + test-check: + name: runner / black-format + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./ + with: + fail_on_error: "false" + - name: Check if code is left untouched (not formatted) + run: | + changed_files=$(git status --porcelain | wc -l) + if [[ ${changed_files} -eq 0 ]]; then + echo "No changes detected!" + else + echo "Changes detected!" + exit 1 + fi + + test-format: + name: runner / black-format + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./ + with: + fail_on_error: "false" + black_args: "." + - name: Check if formatting was successful + run: | + changed_files=$(git status --porcelain | wc -l) + if [[ ${changed_files} -eq 0 ]]; then + echo "No changes detected!" + exit 1 + else + echo "Changes detected!" + fi \ No newline at end of file diff --git a/README.md b/README.md index a557a6fc6c8..1d742bfcccd 100644 --- a/README.md +++ b/README.md @@ -411,7 +411,18 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - uses: psf/black@stable + with: + black_args: ". --check" ``` +### Inputs + +#### `black_args` + +**optional**: Black input arguments. Defaults to `. --check --diff`. + +#### `fail_on_error` + +**optional**: Exit code when black formatting errors are found \[true, false]. Defaults to 'true'. ## Ignoring unmodified files diff --git a/action.yml b/action.yml index 60bf369a3d8..e2bcf40c5ae 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,16 @@ name: "Black" description: "The uncompromising Python code formatter." author: "Ɓukasz Langa and contributors to Black" +inputs: + black_args: + description: "Black input arguments." + required: false + default: "" + fail_on_error: + description: | + Exit code when black formatting errors are found [true, false]. Defaults to 'true'. + required: false + default: "true" branding: color: "black" icon: "check-circle" diff --git a/action/entrypoint.sh b/action/entrypoint.sh index dc86fa1996e..ef949410f02 100755 --- a/action/entrypoint.sh +++ b/action/entrypoint.sh @@ -1,10 +1,22 @@ #!/bin/sh set -e -if [ $# -eq 0 ]; then - # Default (if no args provided). - sh -c "black . --check --diff" +# If no arguments are given use current working directory +black_args=(".") +if [ "$#" -eq 0 ] && [ "${INPUT_BLACK_ARGS}" != "" ]; then + black_args+=(${INPUT_BLACK_ARGS}) +elif [ "$#" -ne 0 ] && [ "${INPUT_BLACK_ARGS}" != "" ]; then + black_args+=($* ${INPUT_BLACK_ARGS}) +elif [ "$#" -ne 0 ] && [ "${INPUT_BLACK_ARGS}" == "" ]; then + black_args+=($*) else - # Custom args. - sh -c "black $*" + # Default (if no args provided). + black_args+=("--check" "--diff") fi + +sh -c "black . ${black_args[@]}" || black_exit_val="$?" + +# Throw error if an error occurred and fail_on_error is true +if [[ "${INPUT_FAIL_ON_ERROR,,}" = 'true' && "${black_error}" = 'true' ]]; then + exit 1 +fi \ No newline at end of file From 5a969b63657a5bb281296f9d73f93af83d1e774f Mon Sep 17 00:00:00 2001 From: rickstaa Date: Wed, 6 Jan 2021 19:15:58 +0100 Subject: [PATCH 2/4] Updates documentation and removes redundant gh-action input argument --- .github/workflows/test_gh_action.yml | 47 ---------------------------- README.md | 4 --- action.yml | 5 --- action/entrypoint.sh | 7 +---- 4 files changed, 1 insertion(+), 62 deletions(-) delete mode 100644 .github/workflows/test_gh_action.yml diff --git a/.github/workflows/test_gh_action.yml b/.github/workflows/test_gh_action.yml deleted file mode 100644 index 2640929fe00..00000000000 --- a/.github/workflows/test_gh_action.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: Test gh-action -on: - push: - paths: - - "action/**" - branches: - - main - pull_request: - paths: - - "action.yml" -jobs: - test-check: - name: runner / black-format - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: ./ - with: - fail_on_error: "false" - - name: Check if code is left untouched (not formatted) - run: | - changed_files=$(git status --porcelain | wc -l) - if [[ ${changed_files} -eq 0 ]]; then - echo "No changes detected!" - else - echo "Changes detected!" - exit 1 - fi - - test-format: - name: runner / black-format - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: ./ - with: - fail_on_error: "false" - black_args: "." - - name: Check if formatting was successful - run: | - changed_files=$(git status --porcelain | wc -l) - if [[ ${changed_files} -eq 0 ]]; then - echo "No changes detected!" - exit 1 - else - echo "Changes detected!" - fi \ No newline at end of file diff --git a/README.md b/README.md index 1d742bfcccd..f330571f03b 100644 --- a/README.md +++ b/README.md @@ -420,10 +420,6 @@ jobs: **optional**: Black input arguments. Defaults to `. --check --diff`. -#### `fail_on_error` - -**optional**: Exit code when black formatting errors are found \[true, false]. Defaults to 'true'. - ## Ignoring unmodified files _Black_ remembers files it has already formatted, unless the `--diff` flag is used or diff --git a/action.yml b/action.yml index e2bcf40c5ae..59b16a9fb6c 100644 --- a/action.yml +++ b/action.yml @@ -6,11 +6,6 @@ inputs: description: "Black input arguments." required: false default: "" - fail_on_error: - description: | - Exit code when black formatting errors are found [true, false]. Defaults to 'true'. - required: false - default: "true" branding: color: "black" icon: "check-circle" diff --git a/action/entrypoint.sh b/action/entrypoint.sh index ef949410f02..21ff2fa76bc 100755 --- a/action/entrypoint.sh +++ b/action/entrypoint.sh @@ -14,9 +14,4 @@ else black_args+=("--check" "--diff") fi -sh -c "black . ${black_args[@]}" || black_exit_val="$?" - -# Throw error if an error occurred and fail_on_error is true -if [[ "${INPUT_FAIL_ON_ERROR,,}" = 'true' && "${black_error}" = 'true' ]]; then - exit 1 -fi \ No newline at end of file +sh -c "black . ${black_args[@]}" From 18dc0a6ff731b0a8786c4e7982b8a6797c34f537 Mon Sep 17 00:00:00 2001 From: rickstaa Date: Wed, 6 Jan 2021 19:23:51 +0100 Subject: [PATCH 3/4] Fixes gh-action bug This commit fixes a bug which caused not all input arguments were forwarder to the black formatter. --- action/entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action/entrypoint.sh b/action/entrypoint.sh index 21ff2fa76bc..92501f79db8 100755 --- a/action/entrypoint.sh +++ b/action/entrypoint.sh @@ -14,4 +14,4 @@ else black_args+=("--check" "--diff") fi -sh -c "black . ${black_args[@]}" +sh -c "black . ${black_args[*]}" From da4063c6facd69251f9aaacdbe29bab643835a48 Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Fri, 8 Jan 2021 20:41:08 +0100 Subject: [PATCH 4/4] Update README.md Co-authored-by: Cooper Lees --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f330571f03b..269bf5aaa43 100644 --- a/README.md +++ b/README.md @@ -414,6 +414,7 @@ jobs: with: black_args: ". --check" ``` + ### Inputs #### `black_args`