Skip to content

Commit

Permalink
Add failIfEmpty argument (fixes #84) (#86)
Browse files Browse the repository at this point in the history
* Add fail if empty argument

If no links were found during a run, this could indicate a configuration
issue. In order to warn users, this new option allows failing the pipeline
in such a scenario.
  • Loading branch information
mre committed Apr 25, 2024
1 parent 16d24de commit ad3b13a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
43 changes: 33 additions & 10 deletions .github/workflows/test.yml
@@ -1,18 +1,19 @@
on:
- repository_dispatch
- workflow_dispatch
- push
- pull_request
push:
branches:
- master
pull_request:
workflow_dispatch:
repository_dispatch:

env:
CUSTOM_OUTPUT_RELATIVE_PATH: lychee/custom_output.md
CUSTOM_OUTPUT_ABSOLUTE_PATH: /tmp/report.md
CUSTOM_OUTPUT_DUMP_PATH: /tmp/dump.md

jobs:
lychee-action:
runs-on: ubuntu-latest
continue-on-error: true
name: Test the lychee link checker action
steps:
- name: Checkout
Expand Down Expand Up @@ -57,7 +58,7 @@ jobs:
with:
output: "${{ env.CUSTOM_OUTPUT_RELATIVE_PATH }}"
debug: true

- name: test custom output relative path - validation
run: |
echo "Checking custom output file at ${{ env.CUSTOM_OUTPUT_RELATIVE_PATH }}"
Expand All @@ -68,13 +69,13 @@ jobs:
echo "Found. Contents:"
cat "${{ env.CUSTOM_OUTPUT_RELATIVE_PATH }}"
fi
- name: test custom output absolute path - creation
uses: ./
with:
output: "${{ env.CUSTOM_OUTPUT_ABSOLUTE_PATH }}"
debug: true

- name: test custom output absolute path - validation
run: |
echo "Checking custom output file at ${{ env.CUSTOM_OUTPUT_ABSOLUTE_PATH }}"
Expand All @@ -92,7 +93,7 @@ jobs:
args: --dump './**/*.md' './**/*.html' './**/*.rst'
output: "${{ env.CUSTOM_OUTPUT_DUMP_PATH }}"
debug: true

- name: test dump with custom output path - validation
run: |
echo "Checking dump output file at ${{ env.CUSTOM_OUTPUT_DUMP_PATH }}"
Expand All @@ -104,6 +105,28 @@ jobs:
cat "${{ env.CUSTOM_OUTPUT_DUMP_PATH }}"
fi
- name: test failIfEmpty - no links in input should fail the pipeline
id: fail_if_empty_test
uses: ./
with:
args: --verbose --no-progress fixtures/empty.md
debug: true
continue-on-error: true

# Explictly check the exit code of the previous step
# as it's expected to fail
- name: Check failIfEmpty
if: steps.fail_if_empty_test.outcome != 'failure'
run: |
echo "FailIfEmpty should have failed because no links were found."
exit 1
- name: test disable failIfEmpty - it's okay if no links are found
uses: ./
with:
args: --no-progress fixtures/empty.md
failIfEmpty: false

- name: Install jq
run: sudo apt-get install jq

Expand Down
7 changes: 6 additions & 1 deletion action.yml
Expand Up @@ -13,6 +13,10 @@ inputs:
description: "Fail entire pipeline on error (i.e. when lychee exit code is not 0)"
default: false
required: false
failIfEmpty:
description: "Fail entire pipeline if no links were found"
default: true
required: false
format:
description: "Summary output format (e.g. json)"
default: "markdown"
Expand All @@ -30,7 +34,7 @@ inputs:
default: "lychee/out.md"
required: false
token:
description: 'Your GitHub Access Token, defaults to: {{ github.token }}'
description: "Your GitHub Access Token, defaults to: {{ github.token }}"
default: ${{ github.token }}
required: false
outputs:
Expand Down Expand Up @@ -60,6 +64,7 @@ runs:
INPUT_ARGS: ${{ inputs.ARGS }}
INPUT_DEBUG: ${{ inputs.DEBUG }}
INPUT_FAIL: ${{ inputs.FAIL }}
INPUT_FAILIFEMPTY: ${{ inputs.FAILIFEMPTY }}
INPUT_FORMAT: ${{ inputs.FORMAT }}
INPUT_JOBSUMMARY: ${{ inputs.JOBSUMMARY }}
INPUT_OUTPUT: ${{ inputs.OUTPUT }}
Expand Down
14 changes: 14 additions & 0 deletions entrypoint.sh
Expand Up @@ -25,6 +25,20 @@ FORMAT=""
eval lychee ${FORMAT} --output ${LYCHEE_TMP} ${ARGS}
exit_code=$?

# Overwrite the exit code in case no links were found
# and `fail-if-empty` is set to `true` (and it is by default)
if [ "${INPUT_FAILIFEMPTY}" = "true" ]; then
# Explicitly set INPUT_FAIL to true to ensure the script fails
# if no links are found
INPUT_FAIL=true
# This is a somewhat crude way to check the Markdown output of lychee
if grep -E 'Total\s+\|\s+0' "${LYCHEE_TMP}"; then
echo "No links were found. This usually indicates a configuration error." >> "${LYCHEE_TMP}"
echo "If this was expected, set 'fail-if-empty: false' in the args." >> "${LYCHEE_TMP}"
exit_code=1
fi
fi

if [ ! -f "${LYCHEE_TMP}" ]; then
echo "No output. Check pipeline run to see if lychee panicked." > "${LYCHEE_TMP}"
else
Expand Down
1 change: 1 addition & 0 deletions fixtures/empty.md
@@ -0,0 +1 @@
This file contains no links. Used for checking `failIfEmpty` flag.

0 comments on commit ad3b13a

Please sign in to comment.