Skip to content

Commit 2a9aa6f

Browse files
timngynRachel Lee Nabors
authored andcommitted
Add redirects label workflow (#5079)
* Add workflow for checking for redirects * Add mergify pull request rules to help with handling redirect labels * Split workflow to check for file deletions on PR opened and PR sync * Add newline at eof * remove extra spaces * add newline at eof * fix typo * Move github variables into environment variables
1 parent 9594be9 commit 2a9aa6f

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Redirects Workflow
2+
on:
3+
pull_request:
4+
branches: [main]
5+
types: [opened, synchronize, labeled]
6+
env:
7+
DIFF_DIRECTORIES: 'src/pages src/fragments'
8+
jobs:
9+
onPrOpen:
10+
name: Check if redirects are needed on PR opened
11+
runs-on: ubuntu-latest
12+
if: github.event.action == 'opened'
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v3
16+
with:
17+
# Minimal depth 2 so we can checkout the commit before possible merge commit.
18+
fetch-depth: 2
19+
- name: Get count of deleted files
20+
env:
21+
GITHUB_PULL_REQUEST_HEAD_SHA: ${{ github.pull_request.head.sha }}
22+
run: |
23+
git fetch origin main
24+
echo "DELETED_FILES_ON_OPENED=$(git diff --name-status --diff-filter=D origin/main -- ${{ env.DIFF_DIRECTORIES }} ${{ env.GITHUB_PULL_REQUEST_HEAD_SHA }} -- ${{ env.DIFF_DIRECTORIES }} | wc -l)" >> $GITHUB_ENV
25+
echo "Deleted file count: ${{ env.DELETED_FILES_ON_OPENED }}"
26+
- name: Fail status check if there are deleted files
27+
if: ${{ env.DELETED_FILES_ON_OPENED > 0 }}
28+
run: exit 1
29+
onPrSync:
30+
name: Check if redirects are needed on PR sync
31+
runs-on: ubuntu-latest
32+
if: github.event.action == 'synchronize'
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v3
36+
with:
37+
# Minimal depth 2 so we can checkout the commit before possible merge commit.
38+
fetch-depth: 2
39+
- name: Get count of deleted files from last sync
40+
env:
41+
GITHUB_EVENT_BEFORE: ${{ github.event.before }}
42+
GITHUB_EVENT_AFTER: ${{ github.event.after }}
43+
run: |
44+
git fetch origin ${{ github.event.before }} --depth=1
45+
echo "DELETED_FILES_ON_SYNC=$(git diff --name-status --diff-filter=D ${{ env.GITHUB_EVENT_BEFORE}} -- ${{ env.DIFF_DIRECTORIES }} ${{ env.GITHUB_EVENT_AFTER }} -- ${{ env.DIFF_DIRECTORIES }} | wc -l)" >> $GITHUB_ENV
46+
echo "Deleted file count: ${{ env.DELETED_FILES_ON_SYNC }}"
47+
- name: Fail status check if there are deleted files
48+
if: ${{ env.DELETED_FILES_ON_SYNC > 0 }}
49+
run: exit 1
50+
failStatusCheck:
51+
name: Fail status check if redirects have not been added
52+
runs-on: ubuntu-latest
53+
if: contains(github.event.pull_request.labels.*.name, 'redirects-needed')
54+
steps:
55+
- name: Exit with error
56+
run: exit 1

.mergify.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pull_request_rules:
2+
- name: Ask PR author what redirects are needed
3+
conditions:
4+
- label="redirects-needed"
5+
actions:
6+
comment:
7+
message: |
8+
@{{author}}, since a file was deleted from the `src/pages` and/or `src/fragments` directories, redirects might need to be set up so these previous pages do not 404. If redirects are needed, please answer these questions for each redirect that is needed:
9+
10+
- What is the source address of the redirect? (Where are you trying to redirect from?)
11+
12+
- What is the target address of the redirect? (Where are you trying to redirect to?)
13+
14+
- Type of redirect? 301 - permanent redirect or 302 - temporary redirect? (More info on Amplify Hosting redirects here: https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html)
15+
label:
16+
remove:
17+
- 'redirects-added'
18+
- name: Add redirects-needed label if status checks fail
19+
conditions:
20+
- or:
21+
- check-failure="Check if redirects are needed on PR opened"
22+
- check-failure="Check if redirects are needed on PR sync"
23+
actions:
24+
label:
25+
add:
26+
- 'redirects-needed'

0 commit comments

Comments
 (0)