-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Expand file tree
/
Copy pathpotential-redirects-or-partials.yml
More file actions
130 lines (115 loc) · 6.58 KB
/
potential-redirects-or-partials.yml
File metadata and controls
130 lines (115 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
name: Potential redirects or partials
# **What it does**: Adds or removes a comment if a PR renames or removes a file.
# **Why we have it**: Highlights when we need redirects covering certain file paths.
# **Who does it impact**: PCX team, other Cloudflare contributors
on:
pull_request:
branches:
- production
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
flag_changed_filenames:
permissions:
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: Potential redirects
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
files=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/files?per_page=100" | \
jq -r '.[] | select(.status=="renamed" or .status=="removed") | select (.filename | startswith("src/content/docs")) | select(.filename | endswith(".mdx")) | if .status == "renamed" then .previous_filename else .filename end' | \
sed -e 's|^src/content/docs||' -e 's|/index\.mdx$|/|' -e 's|\.mdx$|/|')
# Use random delimiter for security reasons
delimiter="$(openssl rand -hex 8)"
echo "CHANGED_FILES<<${delimiter}" >> "$GITHUB_ENV"
echo "${files}" >> "$GITHUB_ENV"
echo "${delimiter}" >> "$GITHUB_ENV"
- name: Updated partial files
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
files=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/files?per_page=100" | \
jq -r '.[] | select(.status=="modified") | select (.filename | startswith("src/content/partials")) | select(.filename | endswith(".mdx")) | .filename' | \
sed -e 's|^src/content/partials||' -e 's|\.mdx$|/|')
# Use random delimiter for security reasons
delimiter="$(openssl rand -hex 8)"
echo "PARTIAL_FILES<<${delimiter}" >> "$GITHUB_ENV"
echo "${files}" >> "$GITHUB_ENV"
echo "${delimiter}" >> "$GITHUB_ENV"
- name: Comment or Update Comment on PR based on changed files
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# If there are no changed files
if [ -z "$CHANGED_FILES" ] && [ -z "$PARTIAL_FILES" ]; then
# Fetch the ID of the existing comment, if it exists
existing_comment_id=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" | \
jq '.[] | select(.user.id == 41898282) | select(.body | contains("This PR requires additional review attention because it affects the following areas")) | .id')
# If the comment exists, delete it
if [ ! -z "$existing_comment_id" ]; then
curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X DELETE \
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$existing_comment_id"
fi
else
# Construct the comment body for changed files
comment_body="This PR requires additional review attention because it affects the following areas:"
# Add CHANGED_FILES logic
if [ -n "$CHANGED_FILES" ]; then
comment_body="$comment_body
### Redirects
This PR changes current filenames or deletes current files. Make sure you have [redirects](https://developers.cloudflare.com/pages/configuration/redirects/) set up to cover the following paths:"
while IFS= read -r path; do
[ -z "$path" ] && continue
clean_path=$(echo "$path" | sed 's/"//g') # Remove quotation marks
comment_body="$comment_body
- [ ] \`$clean_path\`"
done <<< "$CHANGED_FILES"
fi
# Conditional bit for PARTIAL_FILES
if [ -n "$PARTIAL_FILES" ]; then
comment_body="$comment_body
### Partials
This PR updates partial files, which are pieces of content used across multiple files in our [Render component](https://developers.cloudflare.com/style-guide/components/render/)."
while IFS= read -r path; do
[ -z "$path" ] && continue
updated_path=$(echo "$path" | sed -e 's/"//g' -e 's/^\///' -e 's/\/$//')
comment_body="$comment_body
- [ ] \`$updated_path\` - [view affected files](https://developers.cloudflare.com/style-guide/components/render/?partial=$updated_path)"
done <<< "$PARTIAL_FILES"
fi
# Check if a comment already exists
existing_comment_id=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" | \
jq '.[] | select(.user.id == 41898282) | select(.body | contains("This PR requires additional review attention because it affects the following areas")) | .id')
comment_payload=$(echo '{}' | jq --arg body "$comment_body" '.body = $body')
# If a comment exists, update it. Otherwise, post a new comment.
if [ ! -z "$existing_comment_id" ]; then
curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X PATCH -d "$comment_payload" \
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$existing_comment_id"
else
curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST -d "$comment_payload" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
fi
fi