Skip to content

Commit a31782e

Browse files
authored
Update deleted assets workflow (aws-amplify#5591)
* 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
1 parent 12d6277 commit a31782e

File tree

5 files changed

+80
-67
lines changed

5 files changed

+80
-67
lines changed

Diff for: .github/workflows/add_deleted_assets_label.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Add deleted-assets label
2+
on:
3+
workflow_run:
4+
workflows: ['Deleted Assets Workflow']
5+
types: [completed]
6+
env:
7+
ARTIFACT_NAME: deletedAssetsArtifact
8+
LABEL_TO_ADD: deleted-assets
9+
jobs:
10+
addRedirectsNeededLabel:
11+
name: Add deleted-assets label
12+
runs-on: ubuntu-latest
13+
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
14+
permissions:
15+
pull-requests: write # used to add label
16+
steps:
17+
- name: Checkout repository to get the workflow scripts
18+
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 https://github.com/actions/checkout/commit/c85c95e3d7251135ab7dc9ce3241c5835cc595a9
19+
- name: Download artifact
20+
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 https://github.com/actions/github-script/commit/d7906e4ad0b1822421a7e6a35d5ca353c962f410
21+
env:
22+
WORKSPACE: ${{ github.workspace }}
23+
with:
24+
script: |
25+
const { getArtifact } = require('./.github/workflows/scripts/utilities.js');
26+
const fs = require('fs');
27+
const artifactName = process.env.ARTIFACT_NAME;
28+
const workspace = process.env.WORKSPACE
29+
getArtifact({github, context, fs, artifactName, workspace});
30+
- name: Unzip artifact
31+
run: unzip '${{ env.ARTIFACT_NAME }}.zip'
32+
- name: Add redirects-needed label to PR
33+
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 https://github.com/actions/github-script/commit/d7906e4ad0b1822421a7e6a35d5ca353c962f410
34+
with:
35+
github-token: ${{ secrets.GITHUB_TOKEN }}
36+
script: |
37+
const { addLabelForDeletedFiles } = require('./.github/workflows/scripts/add_label_for_deleted_files.js');
38+
const fs = require('fs');
39+
const artifactName = process.env.ARTIFACT_NAME;
40+
const label = process.env.LABEL_TO_ADD;
41+
addLabelForDeletedFiles({github, context, fs, core, artifactName, label});

Diff for: .github/workflows/add_redirects_label.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
types: [completed]
66
env:
77
ARTIFACT_NAME: redirectsArtifact
8+
LABEL_TO_ADD: redirects-needed
89
jobs:
910
addRedirectsNeededLabel:
1011
name: Add redirects-needed label
@@ -33,7 +34,8 @@ jobs:
3334
with:
3435
github-token: ${{ secrets.GITHUB_TOKEN }}
3536
script: |
36-
const { addRedirectsNeededLabel } = require('./.github/workflows/scripts/check_for_redirects.js');
37+
const { addLabelForDeletedFiles } = require('./.github/workflows/scripts/add_label_for_deleted_files.js');
3738
const fs = require('fs');
3839
const artifactName = process.env.ARTIFACT_NAME;
39-
addRedirectsNeededLabel({github, context, fs, core, artifactName});
40+
const label = process.env.LABEL_TO_ADD;
41+
addLabelForDeletedFiles({github, context, fs, core, artifactName, label});

Diff for: .github/workflows/check_for_deleted_assets.yml

+27-44
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,38 @@ name: Deleted Assets Workflow
22
on:
33
pull_request:
44
branches: [main]
5-
types: [opened, synchronize, labeled]
65
env:
7-
DIFF_DIRECTORIES: 'public'
6+
ARTIFACT_NAME: 'deletedAssetsArtifact'
7+
PATHS_TO_CHECK: 'public'
88
jobs:
9-
onPrOpen:
10-
name: Check if assets were deleted on PR opened
9+
checkIfAssetsWereDeleted:
10+
name: Check if assets were deleted
1111
runs-on: ubuntu-latest
12-
if: github.event.action == 'opened'
1312
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
13+
- name: Checkout repository to get the workflow scripts
14+
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 https://github.com/actions/checkout/commit/c85c95e3d7251135ab7dc9ce3241c5835cc595a9
1915
- 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 assets were deleted 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
16+
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 https://github.com/actions/github-script/commit/d7906e4ad0b1822421a7e6a35d5ca353c962f410
17+
id: set-deleted-files-count
3618
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
19+
github-token: ${{ secrets.GITHUB_TOKEN }}
20+
result-encoding: string
21+
script: |
22+
const { PATHS_TO_CHECK } = process.env;
23+
const paths = PATHS_TO_CHECK.split(',');
24+
25+
const { getDeletedFilesFromPR } = require('./.github/workflows/scripts/utilities.js');
26+
return getDeletedFilesFromPR({github, context, paths});
27+
- name: Create artifact containing the PR number and deleted file count
4028
env:
41-
GITHUB_EVENT_BEFORE: ${{ github.event.before }}
42-
GITHUB_EVENT_AFTER: ${{ github.event.after }}
29+
PR_NUMBER: ${{ github.event.pull_request.number }}
4330
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 assets have not been verified
52-
runs-on: ubuntu-latest
53-
if: contains(github.event.pull_request.labels.*.name, 'deleted-assets')
54-
steps:
55-
- name: Exit with error
56-
run: exit 1
31+
artifactName="${{ env.ARTIFACT_NAME }}.txt"
32+
echo ${{ env.PR_NUMBER }} >> $artifactName
33+
echo ${{ steps.set-deleted-files-count.outputs.result }} >> $artifactName
34+
- name: Upload the deleted assets file to artifacts
35+
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2 https://github.com/actions/upload-artifact/commit/0b7f8abb1508181956e8e162db84b466c27e18ce
36+
with:
37+
name: ${{ env.ARTIFACT_NAME }}
38+
path: '${{ env.ARTIFACT_NAME }}.txt'
39+
retention-days: 1

Diff for: .github/workflows/scripts/check_for_redirects.js renamed to .github/workflows/scripts/add_label_for_deleted_files.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
module.exports = {
22
/**
3-
* Add redirects-needed label if count of deleted files is greater than 0
3+
* Add deleted-assets label if count of deleted files is greater than 0
44
*
5-
* @param {Object} obj.artifactName - Name of artifiact file to check
5+
* @param {Object} obj - Object parameters
6+
* @param {String} obj.artifactName - Name of artifiact file to check
7+
* @param {String} obj.label - Label to add to PR when deleted files are found from the specified paths
68
*/
7-
addRedirectsNeededLabel: async ({
9+
addLabelForDeletedFiles: async ({
810
github,
911
context,
1012
fs,
1113
core,
12-
artifactName
14+
artifactName,
15+
label
1316
}) => {
1417
const {
1518
payload: {
@@ -43,7 +46,7 @@ module.exports = {
4346
owner: ownerLogin,
4447
repo: repoName,
4548
issue_number: prNumber,
46-
labels: ['redirects-needed']
49+
labels: [label]
4750
});
4851
}
4952
} else {

Diff for: .mergify.yml

-16
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,3 @@ pull_request_rules:
1212
- What is the target address of the redirect? (Where are you trying to redirect to?)
1313
1414
- 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-
- name: Remove verified-assets label if deleted-assets label is added
16-
conditions:
17-
- label="deleted-assets"
18-
actions:
19-
label:
20-
remove:
21-
- 'verified-assets'
22-
- name: Add deleted-assets label if status checks fail
23-
conditions:
24-
- or:
25-
- check-failure="Check if assets were deleted on PR opened"
26-
- check-failure="Check if assets were deleted on PR sync"
27-
actions:
28-
label:
29-
add:
30-
- 'deleted-assets'

0 commit comments

Comments
 (0)