From 372c2cdb19dfc054dc19411c7a9d21114449b356 Mon Sep 17 00:00:00 2001 From: Tim Nguyen Date: Tue, 31 Jan 2023 18:07:22 -0800 Subject: [PATCH 1/8] Add workflow for checking for redirects --- .github/workflows/check_for_redirects.yml | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/check_for_redirects.yml diff --git a/.github/workflows/check_for_redirects.yml b/.github/workflows/check_for_redirects.yml new file mode 100644 index 00000000000..1eec4a7d56f --- /dev/null +++ b/.github/workflows/check_for_redirects.yml @@ -0,0 +1,39 @@ +name: Redirects Workflow +on: + pull_request: + branches: [main] + types: [opened, synchronize, labeled, unlabeled] +jobs: + checkIfRedirectsAreNeeded: + permissions: + pull-requests: write # used to add label + contents: read + name: Check if redirects are needed + runs-on: ubuntu-latest + if: github.event.action == 'opened' || 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 + run: | + git fetch origin main + echo "DELETED_FILES=$(git diff --name-status --diff-filter=D origin/main -- src/pages src/fragments ${{ github.pull_request.head.sha }} -- src/pages src/fragments | wc -l)" >> $GITHUB_ENV + - name: Add redirects-needed label if there are deleted files + if: ${{ env.DELETED_FILES > 0 }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const { issue: { number: issue_number }, repo: { owner, repo } } = context; + const labels = ['redirects-needed']; + github.rest.issues.addLabels({ owner, repo, issue_number, labels}); + blockPrIfRedirectsNeeded: + name: Block PR if redirects are needed + runs-on: ubuntu-latest + if: contains(github.event.pull_request.labels.*.name, 'redirects-needed') + steps: + - name: Exit with error + run: exit 1 From 1baebd637d6073172c2f8413cd7106017cd678be Mon Sep 17 00:00:00 2001 From: Tim Nguyen Date: Tue, 31 Jan 2023 18:18:36 -0800 Subject: [PATCH 2/8] Add mergify pull request rules to help with handling redirect labels --- .mergify.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .mergify.yml diff --git a/.mergify.yml b/.mergify.yml new file mode 100644 index 00000000000..2838b53dd43 --- /dev/null +++ b/.mergify.yml @@ -0,0 +1,24 @@ +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 will need to be set up so these previous pages do not 404. Please answer these questions: + + - 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 action fails for forked PRs + conditions: + - check-failure="Check if redirects are needed" + actions: + label: + add: + - 'redirects-needed' From e94b443177aa2085299c608e7673054872af64bd Mon Sep 17 00:00:00 2001 From: Tim Nguyen <54393192+timngyn@users.noreply.github.com> Date: Sat, 11 Feb 2023 00:16:45 +0000 Subject: [PATCH 3/8] Split workflow to check for file deletions on PR opened and PR sync --- .github/workflows/check_for_redirects.yml | 50 ++++++++++++++--------- .mergify.yml | 8 ++-- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/.github/workflows/check_for_redirects.yml b/.github/workflows/check_for_redirects.yml index 1eec4a7d56f..fc220297a6c 100644 --- a/.github/workflows/check_for_redirects.yml +++ b/.github/workflows/check_for_redirects.yml @@ -2,15 +2,14 @@ name: Redirects Workflow on: pull_request: branches: [main] - types: [opened, synchronize, labeled, unlabeled] + types: [opened, synchronize, labeled] +env: + DIFF_DIRECTORIES: src/pages src/fragments jobs: - checkIfRedirectsAreNeeded: - permissions: - pull-requests: write # used to add label - contents: read - name: Check if redirects are needed + onPrOpen: + name: Check if redirects are needed on PR opened runs-on: ubuntu-latest - if: github.event.action == 'opened' || github.event.action == 'synchronize' + if: github.event.action == 'opened' steps: - name: Checkout repository uses: actions/checkout@v3 @@ -20,20 +19,33 @@ jobs: - name: Get count of deleted files run: | git fetch origin main - echo "DELETED_FILES=$(git diff --name-status --diff-filter=D origin/main -- src/pages src/fragments ${{ github.pull_request.head.sha }} -- src/pages src/fragments | wc -l)" >> $GITHUB_ENV - - name: Add redirects-needed label if there are deleted files - if: ${{ env.DELETED_FILES > 0 }} - uses: actions/github-script@v6 + echo "DELETED_FILES_ON_OPENED=$(git diff --name-status --diff-filter=D origin/main -- ${{ DIFF_DIRECTORIES }} ${{ github.pull_request.head.sha }} -- ${{ 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: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const { issue: { number: issue_number }, repo: { owner, repo } } = context; - const labels = ['redirects-needed']; - github.rest.issues.addLabels({ owner, repo, issue_number, labels}); - blockPrIfRedirectsNeeded: - name: Block PR if redirects are needed + # 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 + run: | + git fetch origin ${{ github.event.before }} --depth=1 + echo "DELETED_FILES_ON_SYNC=$(git diff --name-status --diff-filter=D ${{ github.event.before }} -- ${{ DIFF_DIRECTORIES }} ${{ github.event.after }} -- ${{ 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 + run: exit 1 \ No newline at end of file diff --git a/.mergify.yml b/.mergify.yml index 2838b53dd43..1faf42441b0 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -5,7 +5,7 @@ pull_request_rules: actions: comment: message: | - @{{author}}, since a file was deleted from the `src/pages` and/or `src/fragments` directories, redirects will need to be set up so these previous pages do not 404. Please answer these questions: + @{{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?) @@ -15,9 +15,11 @@ pull_request_rules: label: remove: - 'redirects-added' - - name: Add redirects-needed label if action fails for forked PRs + - name: Add redirects-needed label if status checks fail conditions: - - check-failure="Check if redirects are needed" + - or: + - check-failure="Check if redirects are needed on PR opened" + - check-failure="Check if redirects are needed on PR sync" actions: label: add: From e69a1ccaec7efa26dcc94fba0bfa3725b823c061 Mon Sep 17 00:00:00 2001 From: Tim Nguyen <54393192+timngyn@users.noreply.github.com> Date: Sat, 11 Feb 2023 00:17:50 +0000 Subject: [PATCH 4/8] Add newline at eof --- .github/workflows/check_for_redirects.yml | 3 ++- .mergify.yml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check_for_redirects.yml b/.github/workflows/check_for_redirects.yml index fc220297a6c..50486b69e60 100644 --- a/.github/workflows/check_for_redirects.yml +++ b/.github/workflows/check_for_redirects.yml @@ -48,4 +48,5 @@ jobs: if: contains(github.event.pull_request.labels.*.name, 'redirects-needed') steps: - name: Exit with error - run: exit 1 \ No newline at end of file + run: exit 1 + \ No newline at end of file diff --git a/.mergify.yml b/.mergify.yml index 1faf42441b0..ff935950fad 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -24,3 +24,4 @@ pull_request_rules: label: add: - 'redirects-needed' + From ba8dab137c4d3182d05502fba27d557957b6c586 Mon Sep 17 00:00:00 2001 From: Tim Nguyen <54393192+timngyn@users.noreply.github.com> Date: Sat, 11 Feb 2023 00:18:39 +0000 Subject: [PATCH 5/8] remove extra spaces --- .github/workflows/check_for_redirects.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/check_for_redirects.yml b/.github/workflows/check_for_redirects.yml index 50486b69e60..d6946fc87f8 100644 --- a/.github/workflows/check_for_redirects.yml +++ b/.github/workflows/check_for_redirects.yml @@ -49,4 +49,3 @@ jobs: steps: - name: Exit with error run: exit 1 - \ No newline at end of file From 97f5c2718343a37f765af2e5431bd7b76cd005d8 Mon Sep 17 00:00:00 2001 From: Tim Nguyen <54393192+timngyn@users.noreply.github.com> Date: Sat, 11 Feb 2023 00:19:25 +0000 Subject: [PATCH 6/8] add newline at eof --- .mergify.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.mergify.yml b/.mergify.yml index ff935950fad..1faf42441b0 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -24,4 +24,3 @@ pull_request_rules: label: add: - 'redirects-needed' - From fccda98a22d10cb07be3eb2a323fab639b61fca3 Mon Sep 17 00:00:00 2001 From: Tim Nguyen <54393192+timngyn@users.noreply.github.com> Date: Sat, 11 Feb 2023 00:41:54 +0000 Subject: [PATCH 7/8] fix typo --- .github/workflows/check_for_redirects.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check_for_redirects.yml b/.github/workflows/check_for_redirects.yml index d6946fc87f8..bf7c099b833 100644 --- a/.github/workflows/check_for_redirects.yml +++ b/.github/workflows/check_for_redirects.yml @@ -4,7 +4,7 @@ on: branches: [main] types: [opened, synchronize, labeled] env: - DIFF_DIRECTORIES: src/pages src/fragments + DIFF_DIRECTORIES: 'src/pages src/fragments' jobs: onPrOpen: name: Check if redirects are needed on PR opened @@ -19,7 +19,7 @@ jobs: - name: Get count of deleted files run: | git fetch origin main - echo "DELETED_FILES_ON_OPENED=$(git diff --name-status --diff-filter=D origin/main -- ${{ DIFF_DIRECTORIES }} ${{ github.pull_request.head.sha }} -- ${{ DIFF_DIRECTORIES }} | wc -l)" >> $GITHUB_ENV + echo "DELETED_FILES_ON_OPENED=$(git diff --name-status --diff-filter=D origin/main -- ${{ env.DIFF_DIRECTORIES }} ${{ 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 }} @@ -37,7 +37,7 @@ jobs: - name: Get count of deleted files from last sync run: | git fetch origin ${{ github.event.before }} --depth=1 - echo "DELETED_FILES_ON_SYNC=$(git diff --name-status --diff-filter=D ${{ github.event.before }} -- ${{ DIFF_DIRECTORIES }} ${{ github.event.after }} -- ${{ DIFF_DIRECTORIES }} | wc -l)" >> $GITHUB_ENV + echo "DELETED_FILES_ON_SYNC=$(git diff --name-status --diff-filter=D ${{ github.event.before }} -- ${{ env.DIFF_DIRECTORIES }} ${{ 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 }} From c41100d6f04ff0a82520f4403d11d7e73d6a5841 Mon Sep 17 00:00:00 2001 From: Tim Nguyen <54393192+timngyn@users.noreply.github.com> Date: Mon, 20 Feb 2023 20:34:45 +0000 Subject: [PATCH 8/8] Move github variables into environment variables --- .github/workflows/check_for_redirects.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check_for_redirects.yml b/.github/workflows/check_for_redirects.yml index bf7c099b833..7eaf216108a 100644 --- a/.github/workflows/check_for_redirects.yml +++ b/.github/workflows/check_for_redirects.yml @@ -17,9 +17,11 @@ jobs: # 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 }} ${{ github.pull_request.head.sha }} -- ${{ env.DIFF_DIRECTORIES }} | wc -l)" >> $GITHUB_ENV + 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 }} @@ -35,9 +37,12 @@ jobs: # 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 ${{ github.event.before }} -- ${{ env.DIFF_DIRECTORIES }} ${{ github.event.after }} -- ${{ env.DIFF_DIRECTORIES }} | wc -l)" >> $GITHUB_ENV + 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 }}