1
- name : Add Redirects
1
+ name : Verify Redirects
2
+
2
3
on :
3
4
pull_request_target :
5
+
4
6
jobs :
5
- add -redirects :
6
- name : Get Redirects for Modified Files
7
+ verify -redirects :
8
+ name : Verifying Redirects
7
9
runs-on : ubuntu-latest
10
+ env :
11
+ REDIRECTS_FILE : " pr/config/redirects"
8
12
permissions :
9
13
pull-requests : write
10
14
steps :
11
- - uses : actions/checkout@v4
15
+ - name : Check Out Base Branch
16
+ uses : actions/checkout@v4
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
+
12
26
- name : Get Changed Files
13
27
id : changed-files
14
28
uses : tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c
15
29
with :
16
30
include_all_old_new_renamed_files : true
17
- - name : Make Redirects for Renamed Files
31
+
32
+ - name : Find Missing Redirects for Renamed Files
18
33
id : renamed-files
19
34
if : steps.changed-files.outputs.renamed_files_count > 0
20
35
env :
21
36
RENAMED_FILES : ${{ steps.changed-files.outputs.all_old_new_renamed_files }}
22
37
run : |
23
38
renamed_redirects=""
24
39
for file in $RENAMED_FILES; do
40
+
41
+ # only run for .txt files
25
42
if [[ ! "$file" == *.txt ]]; then
26
43
continue
27
44
fi
45
+
46
+ # format old and new URLs
28
47
old=$(echo "$file" | cut -d',' -f1)
29
48
old="${old#source}"
30
49
old="${old%.txt}"
31
50
new=$(echo "$file" | cut -d',' -f2)
32
51
new="${new#source}"
33
52
new="${new%.txt}"
34
- #single quotes = ${var} rendered literally; double quotes = $var interpreted
35
- renamed_redirects+='<li>[<v>-*]: ${prefix}/${version}'"$old"'/ -> ${base}/${version}'"$new"'/</li>'
53
+
54
+ # single quotes = ${var} rendered literally; double quotes = $var interpreted
55
+ redirect='${prefix}/${version}'"$old"'/ -> ${base}/${version}'"$new"'/'
56
+
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,"
60
+ fi
36
61
done
37
- echo "redirects=${renamed_redirects}" >> "$GITHUB_OUTPUT"
38
- - name : Make Redirects for Deleted Files
62
+
63
+ # cut trailing comma
64
+ renamed_redirects="${renamed_redirects%,}"
65
+
66
+ echo "redirects=${renamed_redirects}" >> "$GITHUB_OUTPUT"
67
+
68
+ - name : Find Missing Redirects for Deleted Files
39
69
id : deleted-files
40
70
if : steps.changed-files.outputs.deleted_files_count > 0
41
71
env :
42
72
DELETED_FILES : ${{ steps.changed-files.outputs.deleted_files }}
43
73
run : |
44
74
deleted_redirects=""
45
75
for file in $DELETED_FILES; do
76
+
77
+ # only run for .txt files
46
78
if [[ ! "$file" == *.txt ]]; then
47
79
continue
48
80
fi
81
+
82
+ # format old URL
49
83
old=$(echo "$file" | cut -d',' -f1)
50
84
old="${old#source}"
51
85
old="${old%.txt}"
52
- deleted_redirects+='<li>[<v>-*]: ${prefix}/${version}'"$old"'/ -> ${base}/${version}/</li>'
86
+
87
+ # single quotes = ${var} rendered literally; double quotes = $var interpreted
88
+ redirect='${prefix}/${version}'"$old"'/ -> ${base}/${version}/'
89
+
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,"
93
+ fi
53
94
done
54
- echo "redirects=${deleted_redirects}" >> "$GITHUB_OUTPUT"
95
+
96
+ # cut trailing comma
97
+ deleted_redirects="${deleted_redirects%,}"
98
+
99
+ echo "redirects=${deleted_redirects}" >> "$GITHUB_OUTPUT"
100
+
101
+ - name : Build Redirect HTML
102
+ id : build-redirect-html
103
+ env :
104
+ RENAMED_REDIRECTS : ${{ steps.renamed-files.outputs.redirects }}
105
+ DELETED_REDIRECTS : ${{ steps.deleted-files.outputs.redirects }}
106
+ run : |
107
+ # empty string that stays empty if there are no redirects to suggest
108
+ redirect_html=""
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 <v> with earliest backport target version):*"
116
+ fi
117
+ redirect_html+="<li>[<v>-*]: $redirect</li>"
118
+ fi
119
+ done
120
+
121
+ echo "redirect_html=${redirect_html}" >> "$GITHUB_OUTPUT"
122
+
55
123
- name : Update PR Description
56
- if : steps.changed-files.outputs.renamed_files_count > 0 || steps.changed-files.outputs.deleted_files_count > 0
57
124
uses : MongoCaleb/pr-description-action@4bdfe35b98f64532b419ad20b350a92546cd3aa1
58
125
with :
59
126
regex : " - \\ [ \\ ] Did you add redirects\\ ?.*"
60
127
appendContentOnMatchOnly : false
61
128
regexFlags : is
62
- content : " - [ ] Did you add redirects?\n *Suggested redirects for moved/deleted files (replace <v> with earliest backport target version):*\n ${{ steps.renamed-files.outputs.redirects }}${{ steps.deleted-files.outputs.redirects }}\n "
63
- token : ${{ secrets.GITHUB_TOKEN }}
129
+ content : " - [ ] Did you add redirects?\n ${{ steps.build-redirect-html.outputs.redirect_html }}"
130
+ token : ${{ secrets.GITHUB_TOKEN }}
131
+
132
+ - name : Check for duplicates in redirects file
133
+ run : |
134
+ if [[ -f "$REDIRECTS_FILE" ]]; then
135
+ duplicates=$(sort "$REDIRECTS_FILE" | uniq -d)
136
+ if [[ -n "$duplicates" ]]; then
137
+ echo "Duplicate lines found in $REDIRECTS_FILE:"
138
+ echo "$duplicates"
139
+ exit 1 # error
140
+ fi
141
+ else
142
+ echo "Redirects file doesn't exist. Skipping duplicate check."
143
+ fi
0 commit comments