Skip to content

Commit

Permalink
Update deleted assets workflow (#5591)
Browse files Browse the repository at this point in the history
* Update deleted assets workflow

* Refactor redirects label workflow to use add_label_for_deleted_files.js

* Update mergify.yml to remove adding the deleted-assets label

* Update job name and some fixes
  • Loading branch information
timngyn authored Jul 5, 2023
1 parent 12d6277 commit a31782e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 67 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/add_deleted_assets_label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Add deleted-assets label
on:
workflow_run:
workflows: ['Deleted Assets Workflow']
types: [completed]
env:
ARTIFACT_NAME: deletedAssetsArtifact
LABEL_TO_ADD: deleted-assets
jobs:
addRedirectsNeededLabel:
name: Add deleted-assets label
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
permissions:
pull-requests: write # used to add label
steps:
- name: Checkout repository to get the workflow scripts
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 https://github.com/actions/checkout/commit/c85c95e3d7251135ab7dc9ce3241c5835cc595a9
- name: Download artifact
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 https://github.com/actions/github-script/commit/d7906e4ad0b1822421a7e6a35d5ca353c962f410
env:
WORKSPACE: ${{ github.workspace }}
with:
script: |
const { getArtifact } = require('./.github/workflows/scripts/utilities.js');
const fs = require('fs');
const artifactName = process.env.ARTIFACT_NAME;
const workspace = process.env.WORKSPACE
getArtifact({github, context, fs, artifactName, workspace});
- name: Unzip artifact
run: unzip '${{ env.ARTIFACT_NAME }}.zip'
- name: Add redirects-needed label to PR
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 https://github.com/actions/github-script/commit/d7906e4ad0b1822421a7e6a35d5ca353c962f410
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { addLabelForDeletedFiles } = require('./.github/workflows/scripts/add_label_for_deleted_files.js');
const fs = require('fs');
const artifactName = process.env.ARTIFACT_NAME;
const label = process.env.LABEL_TO_ADD;
addLabelForDeletedFiles({github, context, fs, core, artifactName, label});
6 changes: 4 additions & 2 deletions .github/workflows/add_redirects_label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
types: [completed]
env:
ARTIFACT_NAME: redirectsArtifact
LABEL_TO_ADD: redirects-needed
jobs:
addRedirectsNeededLabel:
name: Add redirects-needed label
Expand Down Expand Up @@ -33,7 +34,8 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { addRedirectsNeededLabel } = require('./.github/workflows/scripts/check_for_redirects.js');
const { addLabelForDeletedFiles } = require('./.github/workflows/scripts/add_label_for_deleted_files.js');
const fs = require('fs');
const artifactName = process.env.ARTIFACT_NAME;
addRedirectsNeededLabel({github, context, fs, core, artifactName});
const label = process.env.LABEL_TO_ADD;
addLabelForDeletedFiles({github, context, fs, core, artifactName, label});
71 changes: 27 additions & 44 deletions .github/workflows/check_for_deleted_assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,38 @@ name: Deleted Assets Workflow
on:
pull_request:
branches: [main]
types: [opened, synchronize, labeled]
env:
DIFF_DIRECTORIES: 'public'
ARTIFACT_NAME: 'deletedAssetsArtifact'
PATHS_TO_CHECK: 'public'
jobs:
onPrOpen:
name: Check if assets were deleted on PR opened
checkIfAssetsWereDeleted:
name: Check if assets were deleted
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: Checkout repository to get the workflow scripts
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 https://github.com/actions/checkout/commit/c85c95e3d7251135ab7dc9ce3241c5835cc595a9
- 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
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 https://github.com/actions/github-script/commit/d7906e4ad0b1822421a7e6a35d5ca353c962f410
id: set-deleted-files-count
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
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: |
const { PATHS_TO_CHECK } = process.env;
const paths = PATHS_TO_CHECK.split(',');
const { getDeletedFilesFromPR } = require('./.github/workflows/scripts/utilities.js');
return getDeletedFilesFromPR({github, context, paths});
- name: Create artifact containing the PR number and deleted file count
env:
GITHUB_EVENT_BEFORE: ${{ github.event.before }}
GITHUB_EVENT_AFTER: ${{ github.event.after }}
PR_NUMBER: ${{ github.event.pull_request.number }}
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
artifactName="${{ env.ARTIFACT_NAME }}.txt"
echo ${{ env.PR_NUMBER }} >> $artifactName
echo ${{ steps.set-deleted-files-count.outputs.result }} >> $artifactName
- name: Upload the deleted assets file to artifacts
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2 https://github.com/actions/upload-artifact/commit/0b7f8abb1508181956e8e162db84b466c27e18ce
with:
name: ${{ env.ARTIFACT_NAME }}
path: '${{ env.ARTIFACT_NAME }}.txt'
retention-days: 1
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module.exports = {
/**
* Add redirects-needed label if count of deleted files is greater than 0
* Add deleted-assets label if count of deleted files is greater than 0
*
* @param {Object} obj.artifactName - Name of artifiact file to check
* @param {Object} obj - Object parameters
* @param {String} obj.artifactName - Name of artifiact file to check
* @param {String} obj.label - Label to add to PR when deleted files are found from the specified paths
*/
addRedirectsNeededLabel: async ({
addLabelForDeletedFiles: async ({
github,
context,
fs,
core,
artifactName
artifactName,
label
}) => {
const {
payload: {
Expand Down Expand Up @@ -43,7 +46,7 @@ module.exports = {
owner: ownerLogin,
repo: repoName,
issue_number: prNumber,
labels: ['redirects-needed']
labels: [label]
});
}
} else {
Expand Down
16 changes: 0 additions & 16 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,3 @@ pull_request_rules:
- 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)
- 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'

0 comments on commit a31782e

Please sign in to comment.