Skip to content

Commit d4a4526

Browse files
authored
A homegrown check-files (#54875)
1 parent 4d7daa3 commit d4a4526

3 files changed

Lines changed: 114 additions & 9 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Get changed files
2+
description: Get a list of changed files
3+
4+
inputs:
5+
files:
6+
description: 'Files or directories to check for changes'
7+
required: false
8+
default: '.'
9+
head:
10+
description: 'Head ref to check for changes against'
11+
required: false
12+
13+
outputs:
14+
all_changed_files:
15+
description: 'List of all changed files (unfiltered)'
16+
value: ${{ steps.get_changes.outputs.all_changed_files }}
17+
filtered_changed_files:
18+
description: 'List of changed files matching the filter'
19+
value: ${{ steps.get_changes.outputs.filtered_changed_files }}
20+
21+
runs:
22+
using: 'composite'
23+
steps:
24+
- name: Gather changed files
25+
id: get_changes
26+
env:
27+
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 }}
30+
shell: bash
31+
run: ${{ github.action_path }}/get-changed-files.sh
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
3+
# Required environment variables:
4+
# $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
8+
9+
# Default value for files parameter if not provided
10+
FILTER=${INPUT_FILES:-.}
11+
12+
# Print the filter
13+
echo "__ using filter: __"
14+
echo "$FILTER"
15+
16+
# Find the file diff in the pull request or merge group
17+
# If its a pull request, use the faster call to the GitHub API
18+
# For push, workflow_dispatch, and merge_group, use git diff
19+
if [ -n "$PR" ]
20+
then
21+
echo "__ running gh pr diff __"
22+
DIFF=`gh pr diff $PR --name-only`
23+
if [ -z "$DIFF" ]; then
24+
echo "__ gh pr diff failed, falling back to git diff __"
25+
HEAD=$(gh pr view $PR --json headRefName --jq .headRefName)
26+
fi
27+
fi
28+
29+
if [ -z "$DIFF" ]; then
30+
echo "__ using branch name $HEAD __"
31+
git fetch origin main --depth 1
32+
echo "__ running git diff __"
33+
DIFF=`git diff --name-only origin/main $HEAD`
34+
fi
35+
36+
# So we can inspect the output
37+
echo "__ DIFF found __"
38+
echo "$DIFF"
39+
40+
# Filter the DIFF to just the directories specified in the input files
41+
if [ "$FILTER" != "." ]; then
42+
echo "__ filtering DIFF to only include $FILTER __"
43+
FILTERED_DIFF=""
44+
IFS=$'\n'
45+
for file in $DIFF; do
46+
while IFS= read -r pattern || [ -n "$pattern" ]; do
47+
clean_pattern=${pattern%/}
48+
if [[ $file == $clean_pattern || $file == $clean_pattern/* ]]; then
49+
FILTERED_DIFF="$FILTERED_DIFF $file"
50+
break
51+
fi
52+
done <<< "$FILTER"
53+
done
54+
unset IFS
55+
DIFF=$FILTERED_DIFF
56+
echo "__ filtered DIFF __"
57+
echo "$DIFF"
58+
fi
59+
60+
# Format the output
61+
echo "__ formatting output __"
62+
FORMATTED_DIFF=$(echo $DIFF | tr '\n' ' ' | tr -s ' ')
63+
echo "$FORMATTED_DIFF"
64+
65+
# Set the output for GitHub Actions
66+
if [[ -n "$GITHUB_OUTPUT" ]]; then
67+
echo "all_changed_files=$DIFF" >> "$GITHUB_OUTPUT"
68+
echo "filtered_changed_files=$FORMATTED_DIFF" >> "$GITHUB_OUTPUT"
69+
else
70+
echo "all_changed_files=$DIFF"
71+
echo "filtered_changed_files=$FORMATTED_DIFF"
72+
fi

.github/workflows/content-lint-markdown.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ name: 'Content Lint Markdown'
77
on:
88
pull_request:
99
merge_group:
10-
10+
workflow_dispatch:
11+
inputs:
12+
branch:
13+
description: The branch containing the changes we want to lint.
14+
required: true
15+
type: string
16+
default: main
1117
permissions:
1218
contents: read
1319

@@ -30,23 +36,19 @@ jobs:
3036

3137
- name: Get changed content/data files
3238
id: changed-files
33-
uses: tj-actions/changed-files@40853de9f8ce2d6cfdc73c1b96f14e22ba44aec4 # v45.0.0
39+
uses: ./.github/actions/get-changed-files
3440
with:
35-
# No need to escape the file names because we make the output of
36-
# tj-actions/changed-files be set as an environment variable. Not
37-
# as a direct input to the line of bash that uses it.
38-
safe_output: false
3941
files: |
4042
content/**
4143
data/**
4244
4345
- name: Print content linter annotations if changed content/data files
44-
if: steps.changed-files.outputs.any_changed == 'true'
46+
if: steps.changed-files.outputs.filtered_changed_files
4547
env:
4648
# Make it an environment variable so that its value doesn't need to be escaped.
4749
# See https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
4850
CHANGED_FILES: |-
49-
${{ steps.changed-files.outputs.all_changed_files }}
51+
${{ steps.changed-files.outputs.filtered_changed_files }}
5052
# If there are errors, using `--print-annotations` will make it
5153
# so it does *not* exit non-zero.
5254
# This is so that all warnings and errors are printed.
@@ -58,5 +60,5 @@ jobs:
5860
# Make it an environment variable so that its value doesn't need to be escaped.
5961
# See https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
6062
CHANGED_FILES: |-
61-
${{ steps.changed-files.outputs.all_changed_files }}
63+
${{ steps.changed-files.outputs.filtered_changed_files }}
6264
run: npm run lint-content -- --errors-only --paths $CHANGED_FILES

0 commit comments

Comments
 (0)