-
Notifications
You must be signed in to change notification settings - Fork 4.7k
86 lines (76 loc) · 4.39 KB
/
comment-changed-filenames.yml
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
name: Flag changed filenames
# **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:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Get Renamed or Removed Files from Pull Request
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
files=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" | \
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: 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" ]; 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 changes current filenames or deletes current files")) | .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 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:"
for path in $CHANGED_FILES; do
clean_path=$(echo $path | sed 's/"//g') # Remove quotation marks
comment_body="$comment_body
- [ ] \`$clean_path\`"
done
# 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 changes current filenames or deletes current files")) | .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