Skip to content

Commit

Permalink
Prune YAML files before sending them to vale.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Oct 21, 2024
1 parent e53d0f0 commit 47b5d81
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,3 @@ jobs:

- name: Lint
run: npm run lint

- name: Check Style
uses: errata-ai/[email protected]
with:
version: 3.7.1
files: '["spec","tests"]'
28 changes: 28 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Lint

on: [pull_request,push]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Install Dependencies
run: npm ci

- name: Lint
run: npm run vale -- --source spec
run: npm run vale -- --source tests

- name: Check Style
uses: errata-ai/[email protected]
with:
version: 3.7.1
files: '["spec","tests"]'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"lint": "eslint . --report-unused-disable-directives",
"lint--fix": "eslint . --fix --report-unused-disable-directives",
"merge": "ts-node tools/src/merger/merge.ts",
"vale": "ts-node tools/src/vale/vale.ts",
"test": "npm run test:unit && npm run test:integ",
"jest": "jest",
"test:unit": "jest --testMatch='**/*.test.ts' --testPathIgnorePatterns=/integ/",
Expand Down
49 changes: 49 additions & 0 deletions tools/src/vale/OpenApiValer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import fs from 'fs'
import globby from 'globby'
import _ from 'lodash'

Check failure on line 12 in tools/src/vale/OpenApiValer.ts

View workflow job for this annotation

GitHub Actions / lint

'_' is defined but never used
import { Logger } from '../Logger'

export default class OpenApiValer {

Check failure on line 15 in tools/src/vale/OpenApiValer.ts

View workflow job for this annotation

GitHub Actions / lint

Unknown word: "Valer"
root_folder: string
logger: Logger

constructor (root_folder: string, logger: Logger = new Logger()) {
this.logger = logger
this.root_folder = fs.realpathSync(root_folder)
}

async vale(): Promise<undefined> {
const files = await globby([this.root_folder])
files.forEach((path) => {
this.process(path)
})
}

process(filename: string): void {
const contents = fs.readFileSync(filename, 'utf-8')
var writer = fs.createWriteStream(filename, { flags: 'w+' })

var inside_description = false
contents.split(/\r?\n/).forEach((line) => {
if (line.includes('description: |')) {
inside_description = true
} else if (line.includes('description: ')) {
writer.write(line.replace("description: ", " "))
} else if (inside_description && line.includes(": ")) {
inside_description = false
} else if (inside_description) {
writer.write(line)
}
writer.write("\n")
})
}
}
27 changes: 27 additions & 0 deletions tools/src/vale/vale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import { Command, Option } from '@commander-js/extra-typings'
import { Logger, LogLevel } from '../Logger'
import { resolve } from 'path'
import OpenApiValer from './OpenApiValer'

Check failure on line 13 in tools/src/vale/vale.ts

View workflow job for this annotation

GitHub Actions / lint

Unknown word: "Valer"

const command = new Command()
.description('Prepares YAML files for Vale.')
.addOption(new Option('-s, --source <path>', 'path to the root folder of the multi-file spec').default(resolve(__dirname, '../../../spec')))
.addOption(new Option('--verbose', 'show merge details').default(false))
.allowExcessArguments(false)
.parse()

const opts = command.opts()
const logger = new Logger(opts.verbose ? LogLevel.info : LogLevel.warn)
const valer = new OpenApiValer(opts.source, logger)

Check failure on line 24 in tools/src/vale/vale.ts

View workflow job for this annotation

GitHub Actions / lint

Unknown word: "valer"

Check failure on line 24 in tools/src/vale/vale.ts

View workflow job for this annotation

GitHub Actions / lint

Unknown word: "Valer"
logger.log(`Pruning ${opts.source} ...`)
valer.vale()

Check failure on line 26 in tools/src/vale/vale.ts

View workflow job for this annotation

GitHub Actions / lint

Unknown word: "valer"

Check failure on line 26 in tools/src/vale/vale.ts

View workflow job for this annotation

GitHub Actions / lint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
logger.log('Done.')

0 comments on commit 47b5d81

Please sign in to comment.