Skip to content

Commit 47dfcb6

Browse files
authored
Adjustments to fetch on get-changed-files (#54915)
1 parent f1e345e commit 47dfcb6

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

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

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,15 @@ then
2828
fi
2929

3030
if [ -z "$DIFF" ]; then
31+
# If HEAD was set from gh pr view but INPUT_HEAD is empty, use HEAD instead
32+
if [ -z "$INPUT_HEAD" ] && [ -n "$HEAD" ]; then
33+
INPUT_HEAD=$HEAD
34+
fi
3135
echo "__ using branch name $INPUT_HEAD __"
32-
git fetch origin main --depth 1
36+
git fetch origin $INPUT_HEAD:refs/remotes/origin/$INPUT_HEAD
3337
echo "__ running git diff __"
3438

35-
temp_file=$(mktemp)
36-
git diff --name-only origin/main $INPUT_HEAD > "$temp_file" 2>/dev/null
37-
GIT_EXIT_CODE=$?
38-
39-
DIFF=$(cat "$temp_file")
40-
rm -f "$temp_file"
41-
42-
if [ $GIT_EXIT_CODE -ne 0 ]; then
43-
echo "__ git diff failed with exit code $GIT_EXIT_CODE, fetching unshallow __"
44-
git fetch --depth=100 origin $INPUT_HEAD
45-
git diff --name-only origin/main $INPUT_HEAD > "$temp_file" 2>/dev/null
46-
DIFF=$(cat "$temp_file")
47-
rm -f "$temp_file"
48-
else
49-
echo "__ git diff succeeded __"
50-
fi
39+
DIFF=$(git diff --name-only origin/main origin/$INPUT_HEAD)
5140
fi
5241

5342
# So we can inspect the output
@@ -79,20 +68,36 @@ FORMATTED_DIFF=$(echo "$DIFF" | tr '\n' ' ' | tr -s ' ' | sed 's/^ *//' | sed 's
7968
echo "Formatted diff: '$FORMATTED_DIFF'"
8069

8170
# Set the output for GitHub Actions
82-
if [[ -n "$INPUT_OUTPUT_FILE" ]]; then
83-
ALL_FORMATTED=$(echo "$DIFF" | tr '\n' ' ' | tr -s ' ' | sed 's/^ *//' | sed 's/ *$//')
71+
ALL_FORMATTED=$(echo "$DIFF" | tr '\n' ' ' | tr -s ' ' | sed 's/^ *//' | sed 's/ *$//')
72+
HAS_CHANGES=true
73+
if [[ -z "$ALL_FORMATTED" ]]; then
74+
echo "No changed files detected"
75+
HAS_CHANGES=false
76+
fi
77+
78+
# Function to set outputs either to a file or GITHUB_OUTPUT
79+
set_outputs() {
80+
local target=$1
8481

85-
# Only set outputs if there are actually changed files
86-
if [[ -z "$ALL_FORMATTED" ]]; then
87-
echo "No changed files detected, setting empty outputs"
88-
echo "all_changed_files=" >> "$INPUT_OUTPUT_FILE"
89-
echo "filtered_changed_files=" >> "$INPUT_OUTPUT_FILE"
82+
if [[ "$HAS_CHANGES" == "false" ]]; then
83+
echo "Setting empty outputs to $target"
84+
echo "all_changed_files=" >> "$target"
85+
echo "filtered_changed_files=" >> "$target"
9086
else
91-
echo "Setting non-empty outputs"
92-
echo "all_changed_files=$ALL_FORMATTED" >> "$INPUT_OUTPUT_FILE"
93-
echo "filtered_changed_files=$FORMATTED_DIFF" >> "$INPUT_OUTPUT_FILE"
87+
echo "Setting non-empty outputs to $target"
88+
echo "all_changed_files<<EOF" >> "$target"
89+
echo "$ALL_FORMATTED" >> "$target"
90+
echo "EOF" >> "$target"
91+
92+
echo "filtered_changed_files<<EOF" >> "$target"
93+
echo "$FORMATTED_DIFF" >> "$target"
94+
echo "EOF" >> "$target"
9495
fi
96+
}
97+
98+
# Set outputs to the appropriate target
99+
if [[ -n "$INPUT_OUTPUT_FILE" ]]; then
100+
set_outputs "$INPUT_OUTPUT_FILE"
95101
else
96-
echo "all_changed_files='$DIFF'"
97-
echo "filtered_changed_files='$FORMATTED_DIFF'"
102+
set_outputs "$GITHUB_OUTPUT"
98103
fi

.github/workflows/reviewers-legal.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
uses: ./.github/actions/get-changed-files
4040
with:
4141
files: 'content/**'
42+
token: ${{ secrets.GITHUB_TOKEN }}
4243

4344
- name: Set up Node and dependencies
4445
if: steps.changed_files.outputs.filtered_changed_files

0 commit comments

Comments
 (0)