Skip to content

Commit f02f81e

Browse files
committed
Merge remote-tracking branch 'upstream/test-redirect-gh-action' into regex
2 parents daa90bd + 2f5e22d commit f02f81e

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

.github/workflows/add-redirects.yml

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ jobs:
88
name: Verifying Redirects
99
runs-on: ubuntu-latest
1010
env:
11-
REDIRECTS_FILE: "config/redirects"
11+
REDIRECTS_FILE: "pr/config/redirects"
1212
permissions:
1313
pull-requests: write
1414
steps:
1515
- name: Check Out Base Branch
1616
uses: actions/checkout@v4
17-
17+
18+
- name: Checkout PR Head Branch
19+
uses: actions/checkout@v4
20+
with:
21+
token: ${{ secrets.GITHUB_TOKEN }}
22+
ref: ${{ github.event.pull_request.head.ref }}
23+
repository: ${{ github.event.pull_request.head.repo.full_name }}
24+
path: pr
25+
1826
- name: Get Changed Files
1927
id: changed-files
2028
uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c
@@ -27,7 +35,7 @@ jobs:
2735
env:
2836
RENAMED_FILES: ${{ steps.changed-files.outputs.all_old_new_renamed_files }}
2937
run: |
30-
renamed_redirects=()
38+
renamed_redirects=""
3139
for file in $RENAMED_FILES; do
3240
3341
# only run for .txt files
@@ -44,22 +52,26 @@ jobs:
4452
new="${new%.txt}"
4553
4654
# single quotes = ${var} rendered literally; double quotes = $var interpreted
47-
renamed_redirect='${prefix}/${version}'"$old"'/ -> ${base}/${version}'"$new"'/'
55+
redirect='${prefix}/${version}'"$old"'/ -> ${base}/${version}'"$new"'/'
4856
49-
# if redirect not already in file, add to array to add to PR description
50-
if ! grep -q "$renamed_redirect" $REDIRECTS_FILE; then
51-
renamed_redirects+=("$renamed_redirect")
57+
# if redirect not already in file, add to string to add to PR description
58+
if ! grep -q "$redirect" $REDIRECTS_FILE; then
59+
renamed_redirects+="$redirect,"
5260
fi
5361
done
54-
echo "redirects=${renamed_redirects}" >> "$GITHUB_OUTPUT"
62+
63+
# cut trailing comma
64+
renamed_redirects="${renamed_redirects%,}"
65+
66+
echo "redirects=${renamed_redirects}" >> "$GITHUB_OUTPUT"
5567
5668
- name: Find Missing Redirects for Deleted Files
5769
id: deleted-files
5870
if: steps.changed-files.outputs.deleted_files_count > 0
5971
env:
6072
DELETED_FILES: ${{ steps.changed-files.outputs.deleted_files }}
6173
run: |
62-
deleted_redirects=()
74+
deleted_redirects=""
6375
for file in $DELETED_FILES; do
6476
6577
# only run for .txt files
@@ -73,15 +85,18 @@ jobs:
7385
old="${old%.txt}"
7486
7587
# single quotes = ${var} rendered literally; double quotes = $var interpreted
76-
deleted_redirect='${prefix}/${version}'"$old"'/ -> ${base}/${version}/'
88+
redirect='${prefix}/${version}'"$old"'/ -> ${base}/${version}/'
7789
78-
# if redirect not already in file, add to array to add to PR description
79-
if ! grep -q "$deleted_redirect" $REDIRECTS_FILE; then
80-
deleted_redirects+=("$deleted_redirect")
90+
# if redirect not already in file, add to string to add to PR description
91+
if ! grep -q "$redirect" $REDIRECTS_FILE; then
92+
deleted_redirects+="$redirect,"
8193
fi
82-
8394
done
84-
echo "redirects=${deleted_redirects}" >> "$GITHUB_OUTPUT"
95+
96+
# cut trailing comma
97+
deleted_redirects="${deleted_redirects%,}"
98+
99+
echo "redirects=${deleted_redirects}" >> "$GITHUB_OUTPUT"
85100
86101
- name: Build Redirect HTML
87102
id: build-redirect-html
@@ -91,41 +106,38 @@ jobs:
91106
run: |
92107
# empty string that stays empty if there are no redirects to suggest
93108
redirect_html=""
94-
95-
for renamed_redirect in "${RENAMED_REDIRECTS[@]}"; do
96-
redirect_html+="<li>[&lt;v&gt;-*]: $renamed_redirect</li>"
109+
110+
combined_redirect_string="${RENAMED_REDIRECTS},${DELETED_REDIRECTS}"
111+
112+
for redirect in $combined_redirect_string; do
113+
if [[ ! -z "$redirect" ]]; then
114+
if [[ redirect_html="" ]]; then
115+
redirect_html="*Suggested redirects for moved/deleted files (replace &lt;v&gt; with earliest backport target version):*"
116+
fi
117+
redirect_html+="<li>[&lt;v&gt;-*]: $redirect</li>"
118+
fi
97119
done
98-
99-
for deleted_redirect in "${DELETED_REDIRECTS[@]}"; do
100-
redirect_html+="<li>[&lt;v&gt;-*]: $deleted_redirect</li>"
101-
done
102-
120+
103121
echo "redirect_html=${redirect_html}" >> "$GITHUB_OUTPUT"
104122
105123
- name: Update PR Description
106-
if: steps.build-redirect-html.outputs.redirect_html
107124
uses: MongoCaleb/pr-description-action@4bdfe35b98f64532b419ad20b350a92546cd3aa1
108-
env:
109-
REDIRECT_HTML: ${{ steps.build-redirect-html.outputs.redirect_html }}
110125
with:
111126
regex: "- \\[ \\] Did you add redirects\\?.*"
112127
appendContentOnMatchOnly: false
113128
regexFlags: is
114-
content: "- [ ] Did you add redirects?\n *Suggested redirects for moved/deleted files (replace &lt;v&gt; with earliest backport target version):*\n${{ steps.build-redirect-html.outputs.redirect_html }}\n"
129+
content: "- [ ] Did you add redirects?\n ${{ steps.build-redirect-html.outputs.redirect_html }}"
115130
token: ${{ secrets.GITHUB_TOKEN }}
116131

117132
- name: Check for duplicates in redirects file
118133
run: |
119-
# Specify the file to check
120-
file="$REDIRECTS_FILE"
121-
122-
if [[ -f "$file" ]]; then
123-
duplicates=$(sort "$file" | uniq -d)
134+
if [[ -f "$REDIRECTS_FILE" ]]; then
135+
duplicates=$(sort "$REDIRECTS_FILE" | uniq -d)
124136
if [[ -n "$duplicates" ]]; then
125-
echo "Duplicate lines found in $file:"
137+
echo "Duplicate lines found in $REDIRECTS_FILE:"
126138
echo "$duplicates"
127139
exit 1 # error
128140
fi
129141
else
130-
echo "File $file does not exist. Skipping duplicate check."
142+
echo "Redirects file doesn't exist. Skipping duplicate check."
131143
fi

0 commit comments

Comments
 (0)