Skip to content

Commit babe44d

Browse files
authored
fix(detect-changes): Extract BASE_SHA, and short-circuit on non-PR (#92)
* fix(detect-changes): Populate BASE_SHA env var * feat(detect-changes): Short-circuit when not a PR * fix(detect-changes): Replace return with exit * chore(detect-changes): Add comment for future improvements
1 parent 9905bf8 commit babe44d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

detect-changes/action.yaml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,40 @@ inputs:
88
outputs:
99
detected:
1010
description: Returns if any changed files were matched by the provided glob patterns.
11-
value: ${{ steps.check.outputs.MATCHED }}
11+
value: ${{ steps.check.outputs.DETECTED }}
1212
runs:
1313
using: composite
1414
steps:
1515
- name: Check for changed files
1616
id: check
1717
env:
1818
PATTERNS: ${{ inputs.patterns }}
19+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
20+
GITHUB_EVENT_NAME: ${{ github.event_name }}
1921
shell: bash
2022
run: |
2123
set -euo pipefail
2224
25+
if [ "$GITHUB_EVENT_NAME" != 'pull_request' ]; then
26+
# This is a short-circuit for when not run on a PR.
27+
# It saves having to make the job conditions even more complicated.
28+
echo "Not running on a PR. Hard-coding result:"
29+
echo "DETECTED=true" | tee -a "$GITHUB_OUTPUT"
30+
exit 0
31+
fi
32+
33+
# TODO (@NickLarsenNZ): validate the YAML list. mikefarah-yq doesn't do this.
34+
2335
# This is cursed... we use `.[] | .` to remove quotes and any yaml formatting.
2436
readarray -t GLOBS < <(echo -n "$PATTERNS" | yq '.[] | .')
2537
26-
MATCHED="false"
38+
DETECTED="false"
2739
for GLOB in "${GLOBS[@]}"; do
2840
if ! git diff --exit-code --name-only "${BASE_SHA}.." -- "$GLOB"; then
2941
# When git diff exist with an error, that means there was a diff
3042
# for the glob.
31-
MATCHED="true"
43+
DETECTED="true"
3244
fi
3345
done
3446
35-
echo "MATCHED=$MATCHED" | tee -a "$GITHUB_OUTPUT"
47+
echo "DETECTED=$DETECTED" | tee -a "$GITHUB_OUTPUT"

0 commit comments

Comments
 (0)