diff --git a/.github/workflows/check_for_redirects.yml b/.github/workflows/check_for_redirects.yml new file mode 100644 index 00000000000..7eaf216108a --- /dev/null +++ b/.github/workflows/check_for_redirects.yml @@ -0,0 +1,56 @@ +name: Redirects Workflow +on: + pull_request: + branches: [main] + types: [opened, synchronize, labeled] +env: + DIFF_DIRECTORIES: 'src/pages src/fragments' +jobs: + onPrOpen: + name: Check if redirects are needed on PR opened + runs-on: ubuntu-latest + if: github.event.action == 'opened' + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + # Minimal depth 2 so we can checkout the commit before possible merge commit. + fetch-depth: 2 + - name: Get count of deleted files + env: + GITHUB_PULL_REQUEST_HEAD_SHA: ${{ github.pull_request.head.sha }} + run: | + git fetch origin main + 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 + echo "Deleted file count: ${{ env.DELETED_FILES_ON_OPENED }}" + - name: Fail status check if there are deleted files + if: ${{ env.DELETED_FILES_ON_OPENED > 0 }} + run: exit 1 + onPrSync: + name: Check if redirects are needed on PR sync + runs-on: ubuntu-latest + if: github.event.action == 'synchronize' + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + # Minimal depth 2 so we can checkout the commit before possible merge commit. + fetch-depth: 2 + - name: Get count of deleted files from last sync + env: + GITHUB_EVENT_BEFORE: ${{ github.event.before }} + GITHUB_EVENT_AFTER: ${{ github.event.after }} + run: | + git fetch origin ${{ github.event.before }} --depth=1 + 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 + echo "Deleted file count: ${{ env.DELETED_FILES_ON_SYNC }}" + - name: Fail status check if there are deleted files + if: ${{ env.DELETED_FILES_ON_SYNC > 0 }} + run: exit 1 + failStatusCheck: + name: Fail status check if redirects have not been added + runs-on: ubuntu-latest + if: contains(github.event.pull_request.labels.*.name, 'redirects-needed') + steps: + - name: Exit with error + run: exit 1 diff --git a/.mergify.yml b/.mergify.yml new file mode 100644 index 00000000000..1faf42441b0 --- /dev/null +++ b/.mergify.yml @@ -0,0 +1,26 @@ +pull_request_rules: + - name: Ask PR author what redirects are needed + conditions: + - label="redirects-needed" + actions: + comment: + message: | + @{{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: + + - What is the source address of the redirect? (Where are you trying to redirect from?) + + - What is the target address of the redirect? (Where are you trying to redirect to?) + + - 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) + label: + remove: + - 'redirects-added' + - name: Add redirects-needed label if status checks fail + conditions: + - or: + - check-failure="Check if redirects are needed on PR opened" + - check-failure="Check if redirects are needed on PR sync" + actions: + label: + add: + - 'redirects-needed'