fix: migrating to yaml validation action

This commit is contained in:
Britton Hayes 2021-10-01 00:17:49 -07:00
parent ddb023fb9d
commit a3414cb702
No known key found for this signature in database
GPG key ID: 922B4F3652980643
20 changed files with 3995 additions and 6467 deletions

View file

@ -1,54 +1,76 @@
{
"plugins": ["jest", "@typescript-eslint"],
"extends": ["plugin:github/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-comment": "error",
"camelcase": "off",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": ["error", {"allowExpressions": true}],
"@typescript-eslint/func-call-spacing": ["error", "never"],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-for-in-array": "error",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"semi": "off",
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error"
},
"env": {
"node": true,
"es6": true,
"jest/globals": true
}
"plugins": [
"jest",
"@typescript-eslint"
],
"extends": [
"plugin:github/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": [
"error",
{
"accessibility": "no-public"
}
],
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-comment": "error",
"camelcase": "off",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowExpressions": true
}
],
"@typescript-eslint/func-call-spacing": [
"error",
"never"
],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-for-in-array": "error",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"semi": "off",
"@typescript-eslint/semi": [
"error",
"never"
],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error",
"i18n-text/no-en": "off"
},
"env": {
"node": true,
"es6": true,
"jest/globals": true
}
}

1
.gitattributes vendored
View file

@ -1 +1,2 @@
dist/** -diff linguist-generated=true
*.js linguist-vendored=true

View file

@ -1,9 +1,9 @@
version: 2
updates:
# Enable version updates for npm
- package-ecosystem: 'npm'
- package-ecosystem: "npm"
# Look for `package.json` and `lock` files in the `root` directory
directory: '/'
directory: "/"
# Check the npm registry for updates every day (weekdays)
schedule:
interval: 'daily'
interval: "weekly"

View file

@ -1,10 +1,10 @@
name: 'build-test'
name: "build-test"
on: # rebuild any PRs and main branch changes
pull_request:
push:
branches:
- main
- 'releases/*'
- "releases/*"
jobs:
build: # make sure build/ci work properly
@ -12,13 +12,11 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: |
npm install
yarn install
- run: |
npm run all
yarn run all
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
milliseconds: 1000

View file

@ -1 +1 @@
* @actions/actions-runtime
* @brittonhayes

View file

@ -1,29 +1,9 @@
import {wait} from '../src/wait'
import * as process from 'process'
import * as cp from 'child_process'
import * as path from 'path'
import {validateYAML} from '../src/validate'
import {expect, test} from '@jest/globals'
test('throws invalid number', async () => {
const input = parseInt('foo', 10)
await expect(wait(input)).rejects.toThrow('milliseconds not a number')
})
test('valid yaml files', async () => {
const files: string[] = ['__tests__/valid.yaml']
const schemaPath: string = '__tests__/schema.json'
test('wait 500 ms', async () => {
const start = new Date()
await wait(500)
const end = new Date()
var delta = Math.abs(end.getTime() - start.getTime())
expect(delta).toBeGreaterThan(450)
})
// shows how the runner will run a javascript action with env / stdout protocol
test('test runs', () => {
process.env['INPUT_MILLISECONDS'] = '500'
const np = process.execPath
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecFileSyncOptions = {
env: process.env
}
console.log(cp.execFileSync(np, [ip], options).toString())
await expect(validateYAML(files, schemaPath)).resolves.toBe(0)
})

8
__tests__/schema.json Normal file
View file

@ -0,0 +1,8 @@
{
"test": {
"structure":{
"myValue": "string",
"myValue2": "string"
}
}
}

5
__tests__/valid.yaml Normal file
View file

@ -0,0 +1,5 @@
---
test:
structure:
myValue: "1"
myValue2: "2"

View file

@ -1,11 +1,13 @@
name: 'Your name here'
description: 'Provide a description here'
author: 'Your name or organization here'
name: "Validate YAML"
description: "Validate YAML files against a strict schema"
author: "Britton Hayes"
inputs:
milliseconds: # change this
files:
description: "Files to validate"
required: true
schemaPath:
description: "Path to schema to validate against"
required: true
description: 'input description here'
default: 'default value if applicable'
runs:
using: 'node12'
main: 'dist/index.js'
using: "node12"
main: "dist/index.js"

BIN
dist/index.js generated vendored

Binary file not shown.

BIN
dist/index.js.map generated vendored

Binary file not shown.

BIN
dist/licenses.txt generated vendored

Binary file not shown.

BIN
dist/sourcemap-register.js generated vendored

Binary file not shown.

6334
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,9 @@
{
"name": "typescript-action",
"name": "validate-yaml",
"version": "0.0.0",
"private": true,
"description": "TypeScript template action",
"main": "lib/main.js",
"description": "Validate YAML against a schema using github actions",
"main": "lib/index.js",
"scripts": {
"build": "tsc",
"format": "prettier --write **/*.ts",
@ -11,21 +11,24 @@
"lint": "eslint src/**/*.ts",
"package": "ncc build --source-map --license licenses.txt",
"test": "jest",
"all": "npm run build && npm run format && npm run lint && npm run package && npm test"
"all": "yarn run build && yarn run format && yarn run lint && yarn run package && yarn test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/actions/typescript-action.git"
"url": "git+https://github.com/brittonhayes/wails-build-action.git"
},
"keywords": [
"actions",
"node",
"setup"
"go",
"wails"
],
"author": "",
"author": "Britton Hayes",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.4.0"
"@actions/core": "^1.4.0",
"@actions/exec": "^1.1.0",
"yaml-validator": "^3.0.1"
},
"devDependencies": {
"@types/node": "^16.3.3",
@ -34,6 +37,7 @@
"eslint": "^7.31.0",
"eslint-plugin-github": "^4.1.1",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.0.6",
"js-yaml": "^4.1.0",
"prettier": "2.2.1",

15
src/index.ts Normal file
View file

@ -0,0 +1,15 @@
import * as core from '@actions/core'
import {validateYAML} from './validate'
async function run(): Promise<void> {
try {
const files: string[] = core.getMultilineInput('files')
const schemaPath: string = core.getInput('schemaPath')
await validateYAML(files, schemaPath)
} catch (error) {
core.setFailed((error as Error).message)
}
}
run()

View file

@ -1,19 +0,0 @@
import * as core from '@actions/core'
import {wait} from './wait'
async function run(): Promise<void> {
try {
const ms: string = core.getInput('milliseconds')
core.debug(`Waiting ${ms} milliseconds ...`) // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true
core.debug(new Date().toTimeString())
await wait(parseInt(ms, 10))
core.debug(new Date().toTimeString())
core.setOutput('time', new Date().toTimeString())
} catch (error) {
core.setFailed(error.message)
}
}
run()

23
src/validate.ts Normal file
View file

@ -0,0 +1,23 @@
import YamlValidator, {IYamlValidatorOptions} from 'yaml-validator'
import fs from 'fs'
export async function validateYAML(
files: string[],
schemaPath: string
): Promise<number> {
return new Promise((resolve, reject) => {
const structure = fs.readFileSync(schemaPath, {encoding: 'utf-8'})
const options: IYamlValidatorOptions = {
log: 'validator.log',
onWarning: (err, file) => {
reject(new Error(`File: ${file} - ${err.message}`))
},
structure: JSON.parse(structure),
writeJson: false
}
const validator = new YamlValidator(options)
validator.validate(files)
resolve(validator.report())
})
}

View file

@ -1,9 +0,0 @@
export async function wait(milliseconds: number): Promise<string> {
return new Promise(resolve => {
if (isNaN(milliseconds)) {
throw new Error('milliseconds not a number')
}
setTimeout(() => resolve('done!'), milliseconds)
})
}

3832
yarn.lock Normal file

File diff suppressed because it is too large Load diff