[DNS Firewall] Analytics #2821
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@v4 | |
- name: Potential redirects | |
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?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 }} | |
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?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:" | |
for path in $CHANGED_FILES; do | |
clean_path=$(echo "$path" | sed 's/"//g') # Remove quotation marks | |
comment_body="$comment_body | |
- [ ] \`$clean_path\`" | |
done | |
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/)." | |
for path in $PARTIAL_FILES; do | |
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 | |
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 |