Skip to content

Commit 6c697ee

Browse files
katiekleinkatiegoines
andauthored
add workflow to validate redirects (#7726)
* add workflow to validate redirects * change filename * removing testing errors * finding all invalid redirects * running validation to catch all issues * remove unneeded code --------- Co-authored-by: katiegoines <katiegoines@gmail.com>
1 parent 9a5e77f commit 6c697ee

File tree

5 files changed

+103
-2
lines changed

5 files changed

+103
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module.exports = {
2+
invalidRedirects: () => {
3+
const Ajv = require('ajv');
4+
const redirects = require('../../../redirects.json');
5+
const ajv = new Ajv();
6+
7+
const schema = {
8+
type: 'array',
9+
items: {
10+
type: 'object',
11+
required: ['source', 'target', 'status'],
12+
properties: {
13+
source: {
14+
description: 'The address the user requested.',
15+
type: 'string',
16+
pattern: '^/'
17+
},
18+
target: {
19+
description:
20+
'The address that actually serves the content that the user sees',
21+
type: 'string',
22+
pattern: '^[(https)(/)]'
23+
},
24+
status: {
25+
description:
26+
'Types include a permanent redirect (301), a temporary redirect (302), a rewrite (200), or not found (404).',
27+
type: 'string',
28+
pattern: '^[0-5-]+$'
29+
}
30+
}
31+
}
32+
};
33+
34+
const errors = [];
35+
const validate = ajv.compile(schema);
36+
37+
const validateEntries = (redirects) => {
38+
const valid = validate(redirects);
39+
40+
if (!valid) {
41+
const error = validate.errors[0];
42+
const invalidEntry =
43+
JSON.stringify(redirects[error.instancePath.slice(1, -7)]);
44+
const loc = error.schemaPath.slice(error.schemaPath.indexOf('properties') + 11, -8);
45+
const errorMessage = '\n\n' + 'INVALID ENTRY: Please correct the error in the "' + loc +'" property of the following entry: \n' + invalidEntry + '\n' + 'ERROR MESSAGE: ' + error.message;
46+
errors.push(errorMessage);
47+
48+
validateEntries(redirects.splice(parseInt(error.instancePath.slice(1, -7)) + 1));
49+
50+
}
51+
}
52+
validateEntries(redirects);
53+
54+
return errors;
55+
}
56+
}
57+
58+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Validate Redirects
2+
on:
3+
pull_request:
4+
branches: [main]
5+
types: [opened, synchronize]
6+
env:
7+
BUILD_DIR: 'client/www/next-build'
8+
permissions:
9+
contents: read
10+
jobs:
11+
ValidateRedirects:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
16+
- name: Setup Node.js 20.x
17+
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
18+
with:
19+
node-version: 20.x
20+
- name: Install Dependencies
21+
run: yarn
22+
- name: Validate redirects
23+
id: redirects
24+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
25+
with:
26+
result-encoding: string
27+
script: |
28+
const { invalidRedirects } = require('./.github/workflows/scripts/validate-redirects.js');
29+
return await invalidRedirects();
30+
- name: Fail if any invalid redirects have been found
31+
if: ${{ steps.redirects.outputs.result }}
32+
run: exit 1 && echo ${{ steps.redirects.outputs.result }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@aws-amplify/amplify-cli-core": "^4.3.8",
1313
"@aws-amplify/ui-react": "^6.1.12",
1414
"@docsearch/react": "3",
15+
"ajv": "^8.16.0",
1516
"aws-amplify": "^6.0.9",
1617
"next": "^14.2.3",
1718
"next-image-export-optimizer": "^1.8.3",

redirects.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3011,7 +3011,7 @@
30113011
},
30123012
{
30133013
"source": "/lib/client-configuration/configuring-amplify-categories/q/platform/react-native/",
3014-
"target": "react-native/tools/libraries/configure-categories/",
3014+
"target": "/react-native/tools/libraries/configure-categories/",
30153015
"status": "301"
30163016
},
30173017
{

yarn.lock

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3547,6 +3547,16 @@ ajv@^8.0.1:
35473547
require-from-string "^2.0.2"
35483548
uri-js "^4.2.2"
35493549

3550+
ajv@^8.16.0:
3551+
version "8.16.0"
3552+
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.16.0.tgz#22e2a92b94f005f7e0f9c9d39652ef0b8f6f0cb4"
3553+
integrity sha512-F0twR8U1ZU67JIEtekUcLkXkoO5mMMmgGD8sK/xUFzJ805jxHQl92hImFAqqXMyMYjSPOyUPAwHYhB72g5sTXw==
3554+
dependencies:
3555+
fast-deep-equal "^3.1.3"
3556+
json-schema-traverse "^1.0.0"
3557+
require-from-string "^2.0.2"
3558+
uri-js "^4.4.1"
3559+
35503560
algoliasearch@^4.19.1:
35513561
version "4.22.1"
35523562
resolved "https://registry.npmjs.org/algoliasearch/-/algoliasearch-4.22.1.tgz"
@@ -11493,7 +11503,7 @@ upper-case@^2.0.2:
1149311503
dependencies:
1149411504
tslib "^2.0.3"
1149511505

11496-
uri-js@^4.2.2:
11506+
uri-js@^4.2.2, uri-js@^4.4.1:
1149711507
version "4.4.1"
1149811508
resolved "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz"
1149911509
integrity sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==

0 commit comments

Comments
 (0)