Skip to content

Commit 7168aad

Browse files
authored
Update main test workflow to use composite changed files action (#54889)
1 parent 0f4060a commit 7168aad

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

.github/actions/get-changed-files/action.yml

+16-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@ name: Get changed files
22
description: Get a list of changed files
33

44
inputs:
5-
files:
6-
description: 'Files or directories to check for changes'
5+
file_filter:
6+
description: 'Files or directories to check for changes, supports names, directories, trailing slashes, and single trailing wildcard'
77
required: false
88
default: '.'
99
head:
1010
description: 'Head ref to check for changes against'
1111
required: false
12+
pr:
13+
description: 'The PR to check for changes against'
14+
required: false
15+
token:
16+
description: 'The token'
17+
required: true
18+
output_file:
19+
description: 'Optional file path to write the changes to'
20+
required: false
1221

1322
outputs:
1423
all_changed_files:
1524
description: 'List of all changed files (unfiltered)'
1625
value: ${{ steps.get_changes.outputs.all_changed_files }}
1726
filtered_changed_files:
18-
description: 'List of changed files matching the filter'
27+
description: 'List of changed files matching the `files` filter'
1928
value: ${{ steps.get_changes.outputs.filtered_changed_files }}
2029

2130
runs:
@@ -25,7 +34,9 @@ runs:
2534
id: get_changes
2635
env:
2736
INPUT_FILES: ${{ inputs.files }}
28-
PR: ${{ github.event.pull_request.number }}
29-
HEAD: ${{ github.event.pull_request.head.ref || github.event.merge_group.head_ref || inputs.head || github.ref_name }}
37+
INPUT_PR: ${{ inputs.pr || github.event.pull_request.number }}
38+
INPUT_HEAD: ${{ inputs.head || github.event.pull_request.head.ref || github.event.merge_group.head_ref || github.ref_name }}
39+
INPUT_OUTPUT_FILE: ${{ inputs.output_file }}
40+
GH_TOKEN: ${{ inputs.token }}
3041
shell: bash
3142
run: ${{ github.action_path }}/get-changed-files.sh

.github/actions/get-changed-files/get-changed-files.sh

+17-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
# Required environment variables:
44
# $INPUT_FILES: Pattern(s) to filter files by (e.g., "content/** data/**")
5-
# $FILTER: Derived from INPUT_FILES, defaults to "." if not provided
6-
# $PR: Pull request number (if running in PR context)
7-
# $HEAD: Current branch or SHA for git diff
5+
# $INPUT_PR: Pull request number (if running in PR context)
6+
# $INPUT_HEAD: Current branch or SHA for git diff
7+
# $INPUT_OUTPUT_FILE: Optional file to redirect output to.
8+
# $GH_TOKEN: the access token
89

910
# Default value for files parameter if not provided
1011
FILTER=${INPUT_FILES:-.}
@@ -16,21 +17,21 @@ echo "$FILTER"
1617
# Find the file diff in the pull request or merge group
1718
# If its a pull request, use the faster call to the GitHub API
1819
# For push, workflow_dispatch, and merge_group, use git diff
19-
if [ -n "$PR" ]
20+
if [ -n "$INPUT_PR" ]
2021
then
2122
echo "__ running gh pr diff __"
22-
DIFF=`gh pr diff $PR --name-only`
23+
DIFF=$(gh pr diff $INPUT_PR --name-only)
2324
if [ -z "$DIFF" ]; then
2425
echo "__ gh pr diff failed, falling back to git diff __"
25-
HEAD=$(gh pr view $PR --json headRefName --jq .headRefName)
26+
HEAD=$(gh pr view $INPUT_PR --json headRefName --jq .headRefName)
2627
fi
2728
fi
2829

2930
if [ -z "$DIFF" ]; then
30-
echo "__ using branch name $HEAD __"
31+
echo "__ using branch name $INPUT_HEAD __"
3132
git fetch origin main --depth 1
3233
echo "__ running git diff __"
33-
DIFF=`git diff --name-only origin/main $HEAD`
34+
DIFF=$(git diff --name-only origin/main $INPUT_HEAD)
3435
fi
3536

3637
# So we can inspect the output
@@ -64,9 +65,16 @@ echo "$FORMATTED_DIFF"
6465

6566
# Set the output for GitHub Actions
6667
if [[ -n "$GITHUB_OUTPUT" ]]; then
67-
echo "all_changed_files=$DIFF" >> "$GITHUB_OUTPUT"
68+
ALL_FORMATTED=$(echo "$DIFF" | tr '\n' ' ' | tr -s ' ')
69+
echo "all_changed_files=$ALL_FORMATTED" >> "$GITHUB_OUTPUT"
6870
echo "filtered_changed_files=$FORMATTED_DIFF" >> "$GITHUB_OUTPUT"
6971
else
7072
echo "all_changed_files=$DIFF"
7173
echo "filtered_changed_files=$FORMATTED_DIFF"
7274
fi
75+
76+
# If output file is specified, write the filtered changes to it
77+
if [[ -n "$INPUT_OUTPUT_FILE" ]]; then
78+
echo "$FORMATTED_DIFF" > "$INPUT_OUTPUT_FILE"
79+
echo "__ wrote changes to $INPUT_OUTPUT_FILE __"
80+
fi

.github/workflows/test.yml

+6-31
Original file line numberDiff line numberDiff line change
@@ -131,37 +131,12 @@ jobs:
131131

132132
- name: Gather files changed
133133
if: ${{ matrix.name == 'content-linter' }}
134-
env:
135-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
136-
PR: ${{ github.event.pull_request.number }}
137-
HEAD: ${{ github.event.pull_request.head.ref || github.event.merge_group.head_ref }}
138-
run: |
139-
# Find the file diff in the pull request or merge group
140-
# If its a pull request, use the faster call to the GitHub API
141-
# For push, workflow_dispatch, and merge_group, use git diff
142-
if [ -n "$PR" ]
143-
then
144-
echo __ running gh pr diff __
145-
DIFF=`gh pr diff $PR --name-only`
146-
elif [ -n "$HEAD" ]
147-
then
148-
echo __ running git fetch main __
149-
git fetch origin main --depth 1
150-
echo __ running git diff __
151-
DIFF=`git diff --name-only origin/main`
152-
else
153-
echo __ no head, empty diff __
154-
DIFF=''
155-
fi
156-
# So we can inspect the output
157-
echo __ DIFF found __
158-
echo $DIFF
159-
160-
# So that becomes a string like `foo.js path/bar.md`
161-
# Must do this because the list of files can be HUGE. Especially
162-
# in a repo-sync when there are lots of translation files involved.
163-
echo __ format, write to get_diff_files.txt __
164-
echo $DIFF | tr '\n' ' ' > get_diff_files.txt
134+
uses: ./.github/actions/get-changed-files
135+
with:
136+
token: ${{ secrets.GITHUB_TOKEN }}
137+
pr: ${{ github.event.pull_request.number }}
138+
head: ${{ github.event.pull_request.head.ref || github.event.merge_group.head_ref }}
139+
output_file: get_diff_files.txt
165140

166141
- uses: ./.github/actions/cache-nextjs
167142

0 commit comments

Comments
 (0)