From 5a59b68454f365d657e0eefad1e4b6a7cff8a883 Mon Sep 17 00:00:00 2001 From: Tim Nguyen <54393192+timngyn@users.noreply.github.com> Date: Tue, 21 Feb 2023 16:21:17 -0800 Subject: [PATCH] Add workflow to check for deleted assets (#5142) --- .../workflows/check_for_deleted_assets.yml | 56 +++++++++++++++++++ .mergify.yml | 16 ++++++ 2 files changed, 72 insertions(+) create mode 100644 .github/workflows/check_for_deleted_assets.yml diff --git a/.github/workflows/check_for_deleted_assets.yml b/.github/workflows/check_for_deleted_assets.yml new file mode 100644 index 00000000000..7ded5024eae --- /dev/null +++ b/.github/workflows/check_for_deleted_assets.yml @@ -0,0 +1,56 @@ +name: Deleted Assets Workflow +on: + pull_request: + branches: [main] + types: [opened, synchronize, labeled] +env: + DIFF_DIRECTORIES: 'public' +jobs: + onPrOpen: + name: Check if assets were deleted 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 assets were deleted 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 assets have not been verified + runs-on: ubuntu-latest + if: contains(github.event.pull_request.labels.*.name, 'deleted-assets') + steps: + - name: Exit with error + run: exit 1 \ No newline at end of file diff --git a/.mergify.yml b/.mergify.yml index 1faf42441b0..befbae40c2f 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -24,3 +24,19 @@ pull_request_rules: label: add: - 'redirects-needed' + - name: Remove verified-assets label if deleted-assets label is added + conditions: + - label="deleted-assets" + actions: + label: + remove: + - 'verified-assets' + - name: Add deleted-assets label if status checks fail + conditions: + - or: + - check-failure="Check if assets were deleted on PR opened" + - check-failure="Check if assets were deleted on PR sync" + actions: + label: + add: + - 'deleted-assets' \ No newline at end of file