-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add workflow to validate redirects #7726
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d85ad47
add workflow to validate redirects
1c02da7
change filename
6bf85ff
removing testing errors
9ece25e
finding all invalid redirects
685f0ca
running validation to catch all issues
0c3113b
remove unneeded code
3ac3238
Merge branch 'main' into redirects-validation
katiegoines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module.exports = { | ||
invalidRedirects: () => { | ||
const Ajv = require('ajv'); | ||
const redirects = require('../../../redirects.json'); | ||
const ajv = new Ajv(); | ||
|
||
const schema = { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: ['source', 'target', 'status'], | ||
properties: { | ||
source: { | ||
description: 'The address the user requested.', | ||
type: 'string', | ||
pattern: '^/' | ||
}, | ||
target: { | ||
description: | ||
'The address that actually serves the content that the user sees', | ||
type: 'string', | ||
pattern: '^[(https)(/)]' | ||
}, | ||
status: { | ||
description: | ||
'Types include a permanent redirect (301), a temporary redirect (302), a rewrite (200), or not found (404).', | ||
type: 'string', | ||
pattern: '^[0-5-]+$' | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const errors = []; | ||
const validate = ajv.compile(schema); | ||
|
||
const validateEntries = (redirects) => { | ||
const valid = validate(redirects); | ||
|
||
if (!valid) { | ||
const error = validate.errors[0]; | ||
const invalidEntry = | ||
JSON.stringify(redirects[error.instancePath.slice(1, -7)]); | ||
const loc = error.schemaPath.slice(error.schemaPath.indexOf('properties') + 11, -8); | ||
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; | ||
errors.push(errorMessage); | ||
|
||
validateEntries(redirects.splice(parseInt(error.instancePath.slice(1, -7)) + 1)); | ||
|
||
} | ||
} | ||
validateEntries(redirects); | ||
|
||
return errors; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Validate Redirects | ||
on: | ||
pull_request: | ||
branches: [main] | ||
types: [opened, synchronize] | ||
env: | ||
BUILD_DIR: 'client/www/next-build' | ||
permissions: | ||
contents: read | ||
jobs: | ||
ValidateRedirects: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 | ||
- name: Setup Node.js 20.x | ||
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 | ||
with: | ||
node-version: 20.x | ||
- name: Install Dependencies | ||
run: yarn | ||
- name: Validate redirects | ||
id: redirects | ||
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
with: | ||
result-encoding: string | ||
script: | | ||
const { invalidRedirects } = require('./.github/workflows/scripts/validate-redirects.js'); | ||
return await invalidRedirects(); | ||
- name: Fail if any invalid redirects have been found | ||
if: ${{ steps.redirects.outputs.result }} | ||
run: exit 1 && echo ${{ steps.redirects.outputs.result }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like the previous step will always return at least the empty error array, will this only fail if no errors are found or is this going to always fail because of the empty array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It only fails if no errors are found. Just double confirmed it on my forked branch.