-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-301224: Generate README with IPA rules #451
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2772054
CLOUDP-301224: Generate README with IPA rules
lovisaberggren 0d7352d
CLOUDP-301224: Fix
lovisaberggren 19ddd1a
CLOUDP-301224: Fix
lovisaberggren 69893be
CLOUDP-301224: Add readme
lovisaberggren 0f8b302
CLOUDP-301224: Add links to rule definitions
lovisaberggren 0e99a09
CLOUDP-301224: Refactor
lovisaberggren 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
| <!--- NOTE: This README file is generated, please see /scripts/generateRulesetReadme.js ---> | ||
|
|
||
| # IPA Validation Rules | ||
|
|
||
| All Spectral rules used in the IPA validation are defined in rulesets grouped by IPA number (`IPA-XXX.yaml`). These rulesets are imported into the main IPA ruleset [ipa-spectral.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/ipa-spectral.yaml) which is used for running the validation. | ||
|
|
||
| ## Rulesets | ||
|
|
||
| The tables below lists all available rules, their descriptions and severity level. | ||
|
|
||
| ### IPA-005 | ||
|
|
||
| For rule definitions, see [IPA-005.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-005.yaml). | ||
|
|
||
| | Rule Name | Description | Severity | | ||
| | --------------------------------------- | ------------------------------------------------------------------------ | -------- | | ||
| | xgen-IPA-005-exception-extension-format | IPA exception extensions must follow the correct format. http://go/ipa/5 | warn | | ||
|
|
||
| ### IPA-102 | ||
|
|
||
| For rule definitions, see [IPA-102.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-102.yaml). | ||
|
|
||
| | Rule Name | Description | Severity | | ||
| | ---------------------------------------------------- | -------------------------------------------------------------------------------- | -------- | | ||
| | xgen-IPA-102-path-alternate-resource-name-path-param | Paths should alternate between resource names and path params. http://go/ipa/102 | warn | | ||
|
|
||
| ### IPA-104 | ||
|
|
||
| For rule definitions, see [IPA-104.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-104.yaml). | ||
|
|
||
| | Rule Name | Description | Severity | | ||
| | ----------------------------- | --------------------------------------------------------------- | -------- | | ||
| | xgen-IPA-104-resource-has-GET | APIs must provide a get method for resources. http://go/ipa/104 | warn | | ||
|
|
||
| ### IPA-109 | ||
|
|
||
| For rule definitions, see [IPA-109.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-109.yaml). | ||
|
|
||
| | Rule Name | Description | Severity | | ||
| | ---------------------------------------------- | ------------------------------------------------------------------------- | -------- | | ||
| | xgen-IPA-109-custom-method-must-be-GET-or-POST | The HTTP method for custom methods must be GET or POST. http://go/ipa/109 | warn | | ||
| | xgen-IPA-109-custom-method-must-use-camel-case | The custom method must use camelCase format. http://go/ipa/109 | warn | | ||
|
|
||
| ### IPA-113 | ||
|
|
||
| For rule definitions, see [IPA-113.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-113.yaml). | ||
|
|
||
| | Rule Name | Description | Severity | | ||
| | --------------------------------------- | ------------------------------------------------------------------------------------------- | -------- | | ||
| | xgen-IPA-113-singleton-must-not-have-id | Singleton resources must not have a user-provided or system-generated ID. http://go/ipa/113 | warn | | ||
|
|
||
| ### IPA-123 | ||
|
|
||
| For rule definitions, see [IPA-123.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-123.yaml). | ||
|
|
||
| | Rule Name | Description | Severity | | ||
| | ------------------------------------------------- | ------------------------------------------------------- | -------- | | ||
| | xgen-IPA-123-enum-values-must-be-upper-snake-case | Enum values must be UPPER_SNAKE_CASE. http://go/ipa/123 | warn | | ||
|
|
||
|
|
||
This file contains hidden or 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,100 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
| import spectral from '@stoplight/spectral-core'; | ||
| import { markdownTable } from 'markdown-table'; | ||
| import { loadRuleset } from '../utils.js'; | ||
|
|
||
| const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const readmeFilePath = path.join(dirname, '../rulesets', 'README.md'); | ||
|
|
||
| const rulesetsSection = await getRulesetsSection(); | ||
|
|
||
| const fileContent = | ||
| '<!--- NOTE: This README file is generated, please see /scripts/generateRulesetReadme.js --->\n\n' + | ||
| '# IPA Validation Rules\n\n' + | ||
| 'All Spectral rules used in the IPA validation are defined in rulesets grouped by IPA number (`IPA-XXX.yaml`). These rulesets are imported into the main IPA ruleset [ipa-spectral.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/ipa-spectral.yaml) which is used for running the validation.\n\n' + | ||
| `${rulesetsSection}` + | ||
| '\n'; | ||
|
|
||
| fs.writeFile(readmeFilePath, fileContent, (error) => { | ||
| if (error) { | ||
| console.error('Error while generating the IPA rulesets README.md:', error); | ||
| process.exit(1); | ||
| } | ||
| }); | ||
|
|
||
| async function getRulesetsSection() { | ||
| let content = | ||
| '## Rulesets\n\n' + 'The tables below lists all available rules, their descriptions and severity level.\n\n'; | ||
|
|
||
| const rules = await getAllRules(); | ||
| const ruleNames = Object.keys(rules); | ||
| const ipaNumbers = getIpaNumbers(ruleNames); | ||
|
|
||
| ipaNumbers.forEach((ipaNumber) => { | ||
| const ipaRules = filterRulesByIpaNumber(ipaNumber, rules); | ||
| const table = generateRulesetTable(ipaRules); | ||
| content += | ||
| `### ${ipaNumber}\n\n` + `For rule definitions, see ${getIpaRulesetUrl(ipaNumber)}.\n\n` + `${table}\n\n`; | ||
| }); | ||
|
|
||
| return content; | ||
| } | ||
|
|
||
| function generateRulesetTable(rules) { | ||
| const table = [['Rule Name', 'Description', 'Severity']]; | ||
| const tableRows = []; | ||
|
|
||
| const ruleNames = Object.keys(rules); | ||
| ruleNames.forEach((ruleName) => { | ||
| const rule = rules[ruleName]; | ||
| tableRows.push([ruleName, rule.description, rule.definition.severity]); | ||
| }); | ||
|
|
||
| tableRows.sort(sortBySeverity); | ||
| tableRows.forEach((row) => table.push(row)); | ||
| return markdownTable(table); | ||
| } | ||
|
|
||
| async function getAllRules() { | ||
| const rulesetFilePath = path.join(dirname, '..', 'ipa-spectral.yaml'); | ||
| const { Spectral } = spectral; | ||
| const ruleset = await loadRuleset(rulesetFilePath, new Spectral()); | ||
| return ruleset.rules; | ||
| } | ||
|
|
||
| function getIpaNumbers(ruleNames) { | ||
| const ipaNumbers = []; | ||
| ruleNames.forEach((name) => { | ||
| const ipaName = name.substring(5, 12); | ||
| if (!ipaNumbers.includes(ipaName)) { | ||
| ipaNumbers.push(ipaName); | ||
| } | ||
| }); | ||
| return ipaNumbers.sort(); | ||
| } | ||
|
|
||
| function getIpaRulesetUrl(ipaNumber) { | ||
| return `[${ipaNumber}.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/${ipaNumber}.yaml)`; | ||
| } | ||
|
|
||
| function filterRulesByIpaNumber(ipaNumber, rules) { | ||
| return Object.keys(rules) | ||
| .filter((key) => key.includes(ipaNumber)) | ||
| .reduce((obj, key) => { | ||
| return { | ||
| ...obj, | ||
| [key]: rules[key], | ||
| }; | ||
| }, {}); | ||
| } | ||
|
|
||
| function sortBySeverity(a, b) { | ||
| if (a[2] < b[2]) { | ||
| return -1; | ||
| } else if (a[2] > b[2]) { | ||
| return 1; | ||
| } | ||
| return 0; | ||
| } |
This file contains hidden or 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,21 @@ | ||
| import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader'; | ||
| import fs from 'node:fs'; | ||
|
|
||
| export async function loadRuleset(rulesetPath, spectral) { | ||
| try { | ||
| const ruleset = await bundleAndLoadRuleset(rulesetPath, { fs, fetch }); | ||
| await spectral.setRuleset(ruleset); | ||
| return ruleset; | ||
| } catch (error) { | ||
| throw new Error(`Failed to load ruleset: ${error.message}`); | ||
| } | ||
| } | ||
|
|
||
| export function loadJsonFile(filePath) { | ||
| try { | ||
| const content = fs.readFileSync(filePath, 'utf8'); | ||
| return JSON.parse(content); | ||
| } catch (error) { | ||
| throw new Error(`Failed to load file: ${error.message}`); | ||
| } | ||
| } |
Oops, something went wrong.
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.
[nit] Golink to internal site. Might be better to have actions self explanatory. Then downstream can link upstream instead.
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.
IMO I think it's nice to have a link to the IPA itself for each rule, as it will show up when a validation fails. We do use golinks pretty widely in the openapi repo