From 2c4a0b9b306eaa9998efdbd330fda1e9a9c76d88 Mon Sep 17 00:00:00 2001 From: Joseph Kato Date: Sun, 21 Jun 2020 14:31:44 -0700 Subject: [PATCH] v1.2.0 --- README.md | 22 ++++++++++++++-------- lib/check.js | 34 +++++++++++++++++++++++----------- lib/input.js | 27 ++++++++++++++++++++------- lib/main.js | 36 +++++++++++++++++++++++++++--------- package.json | 9 +++++---- src/check.ts | 29 ++++++++++++++--------------- src/main.ts | 13 +++++++++---- vale-matcher.json | 19 ------------------- 8 files changed, 112 insertions(+), 77 deletions(-) delete mode 100644 vale-matcher.json diff --git a/README.md b/README.md index f67daa15..6a23091d 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,10 @@ > :octocat: The official GitHub Action for Vale -- install, manage, and run Vale with ease. -This GitHub Action executes [Vale](https://github.com/errata-ai/vale) and displays its alerts as annotations in GitHub's "Files changed" UI: -

- demo + A demo screenshot.

-You can see a [live example here](https://github.com/errata-ai/vale-action/pull/1/commits/2e3b1e76e3b6412ea9577701218aba19470f87bb). - ## Usage Add the following (or similar) to one of your [`.github/workflow`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/configuring-a-workflow) files: @@ -26,8 +22,20 @@ jobs: uses: actions/checkout@master - name: Vale - uses: errata-ai/vale-action@v1.0.4 + uses: errata-ai/vale-action@v1.2.0 + with: + # Optional + styles: | + https://github.com/errata-ai/Microsoft/releases/latest/download/Microsoft.zip + https://github.com/errata-ai/write-good/releases/latest/download/write-good.zip + + # Optional + config: https://raw.githubusercontent.com/errata-ai/vale/master/.vale.ini + + # Optional + files: path/to/lint env: + # Required GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} ``` @@ -37,8 +45,6 @@ The recommended repository structure makes use of the existing `.github` directo ```text .github -├── CONTRIBUTING.md -├── ISSUE_TEMPLATE.md ├── styles │ └── vocab.txt └── workflows diff --git a/lib/check.js b/lib/check.js index b5c877d3..6a7490d1 100644 --- a/lib/check.js +++ b/lib/check.js @@ -1,4 +1,23 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -8,16 +27,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; Object.defineProperty(exports, "__esModule", { value: true }); +exports.CheckRunner = void 0; const core = __importStar(require("@actions/core")); const github = __importStar(require("@actions/github")); +const colorize = require('json-colorizer'); const pkg = require('../package.json'); const USER_AGENT = `${pkg.name}/${pkg.version} (${pkg.bugs.url})`; /** @@ -69,7 +83,7 @@ class CheckRunner { executeCheck(options) { return __awaiter(this, void 0, void 0, function* () { core.info(`Vale: ${this.getSummary()}`); - const client = new github.GitHub(options.token, { + const client = github.getOctokit(options.token, { userAgent: USER_AGENT, }); let checkRunId; @@ -227,9 +241,7 @@ class CheckRunner { * TODO: Nicer formatting? */ dumpToStdout() { - for (const annotation of this.annotations) { - core.info(annotation.message); - } + console.log(colorize(JSON.stringify(this.annotations))); } /** * Create buckets of at most 50 annotations for the API. diff --git a/lib/input.js b/lib/input.js index 7094cbb4..4a87c997 100644 --- a/lib/input.js +++ b/lib/input.js @@ -1,4 +1,23 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -8,14 +27,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; Object.defineProperty(exports, "__esModule", { value: true }); +exports.get = void 0; const core = __importStar(require("@actions/core")); const exec = __importStar(require("@actions/exec")); const fs = __importStar(require("fs")); diff --git a/lib/main.js b/lib/main.js index c9cb05dd..b26f8e8d 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,4 +1,23 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -8,20 +27,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; Object.defineProperty(exports, "__esModule", { value: true }); +exports.run = void 0; const core = __importStar(require("@actions/core")); const github = __importStar(require("@actions/github")); const tmp = __importStar(require("tmp")); -const execa = require("execa"); const check_1 = require("./check"); const input = __importStar(require("./input")); +const execa = require('execa'); /** * These environment variables are exposed for GitHub Actions. * @@ -29,17 +42,22 @@ const input = __importStar(require("./input")); */ const { GITHUB_TOKEN, GITHUB_WORKSPACE } = process.env; function run(actionInput) { + var _a, _b; return __awaiter(this, void 0, void 0, function* () { const startedAt = new Date().toISOString(); const alertResp = yield execa('vale', actionInput.args); let runner = new check_1.CheckRunner(); + let sha = github.context.sha; + if ((_b = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.head) === null || _b === void 0 ? void 0 : _b.sha) { + sha = github.context.payload.pull_request.head.sha; + } runner.makeAnnotations(alertResp.stdout); yield runner.executeCheck({ token: actionInput.token, name: 'Vale', owner: github.context.repo.owner, repo: github.context.repo.repo, - head_sha: github.context.sha, + head_sha: sha, started_at: startedAt, context: { vale: actionInput.version } }); diff --git a/package.json b/package.json index 49ba0628..aed209d0 100644 --- a/package.json +++ b/package.json @@ -28,22 +28,23 @@ "dependencies": { "@actions/core": "^1.0.0", "@actions/exec": "^1.0.0", - "@actions/github": "^1.0.0", + "@actions/github": "^3.0.0", "@types/request-promise-native": "^1.0.17", "@types/tmp": "^0.1.0", "execa": "^3.2.0", + "json-colorizer": "^2.2.2", "request": "^2.88.0", "request-promise-native": "^1.0.7", "tmp": "^0.1.0" }, "devDependencies": { "@types/jest": "^24.0.13", - "@types/node": "^12.0.4", - "@zeit/ncc": "^0.20.5", + "@types/node": "^14.0.13", + "@zeit/ncc": "^0.22.3", "jest": "^24.8.0", "jest-circus": "^24.7.1", "prettier": "^1.17.1", "ts-jest": "^24.0.2", - "typescript": "^3.5.1" + "typescript": "^3.9.5" } } diff --git a/src/check.ts b/src/check.ts index 05164dc0..cb880462 100644 --- a/src/check.ts +++ b/src/check.ts @@ -1,10 +1,11 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; -import * as octokit from '@octokit/rest'; +const colorize = require('json-colorizer'); const pkg = require('../package.json'); const USER_AGENT = `${pkg.name}/${pkg.version} (${pkg.bugs.url})`; +type ChecksCreateParamsOutputAnnotations = any; type Severity = 'suggestion'|'warning'|'error'; interface Alert { @@ -43,7 +44,7 @@ interface CheckOptions { * See https://developer.github.com/v3/checks. */ export class CheckRunner { - private annotations: Array; + private annotations: Array; private stats: Stats; constructor() { @@ -91,7 +92,7 @@ export class CheckRunner { public async executeCheck(options: CheckOptions): Promise { core.info(`Vale: ${this.getSummary()}`); - const client = new github.GitHub(options.token, { + const client = github.getOctokit(options.token, { userAgent: USER_AGENT, }); @@ -131,7 +132,7 @@ export class CheckRunner { * * See https://developer.github.com/v3/checks/runs/#create-a-check-run. */ - private async createCheck(client: github.GitHub, options: CheckOptions): + private async createCheck(client: any, options: CheckOptions): Promise { const response = await client.checks.create({ owner: options.owner, @@ -153,7 +154,7 @@ export class CheckRunner { * multiple "buckets" if we have more than 50. */ private async runUpdateCheck( - client: github.GitHub, checkRunId: number, + client: any, checkRunId: number, options: CheckOptions): Promise { let annotations = this.getBucket(); @@ -195,7 +196,7 @@ export class CheckRunner { * Indicate that no alerts were found. */ private async successCheck( - client: github.GitHub, checkRunId: number, + client: any, checkRunId: number, options: CheckOptions): Promise { let req: any = { owner: options.owner, @@ -224,7 +225,7 @@ export class CheckRunner { * Something went wrong; cancel the check run and report the exception. */ private async cancelCheck( - client: github.GitHub, checkRunId: number, + client: any, checkRunId: number, options: CheckOptions): Promise { let req: any = { owner: options.owner, @@ -259,9 +260,7 @@ export class CheckRunner { * TODO: Nicer formatting? */ private dumpToStdout() { - for (const annotation of this.annotations) { - core.info(annotation.message); - } + console.log(colorize(JSON.stringify(this.annotations))); } /** @@ -269,8 +268,8 @@ export class CheckRunner { * * See https://developer.github.com/v3/checks/runs/#output-object. */ - private getBucket(): Array { - let annotations: Array = []; + private getBucket(): Array { + let annotations: Array = []; while (annotations.length < 50) { const annotation = this.annotations.pop(); if (annotation) { @@ -343,9 +342,9 @@ export class CheckRunner { * See https://developer.github.com/v3/checks/runs/#annotations-object. */ static makeAnnotation(name: string, alert: Alert): - octokit.ChecksCreateParamsOutputAnnotations { + ChecksCreateParamsOutputAnnotations { let annotation_level: - octokit.ChecksCreateParamsOutputAnnotations['annotation_level']; + ChecksCreateParamsOutputAnnotations['annotation_level']; switch (alert.Severity) { case 'suggestion': @@ -359,7 +358,7 @@ export class CheckRunner { break; } - let annotation: octokit.ChecksCreateParamsOutputAnnotations = { + let annotation: ChecksCreateParamsOutputAnnotations = { path: name, start_line: alert.Line, end_line: alert.Line, diff --git a/src/main.ts b/src/main.ts index 75e19114..c14f81b3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,12 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; - import * as tmp from 'tmp'; -import execa = require('execa'); import {CheckRunner} from './check'; import * as input from './input'; +const execa = require('execa'); + /** * These environment variables are exposed for GitHub Actions. * @@ -19,14 +19,19 @@ export async function run(actionInput: input.Input): Promise { const alertResp = await execa('vale', actionInput.args); let runner = new CheckRunner(); - runner.makeAnnotations(alertResp.stdout); + let sha = github.context.sha; + if (github.context.payload.pull_request?.head?.sha) { + sha = github.context.payload.pull_request.head.sha; + } + + runner.makeAnnotations(alertResp.stdout); await runner.executeCheck({ token: actionInput.token, name: 'Vale', owner: github.context.repo.owner, repo: github.context.repo.repo, - head_sha: github.context.sha, + head_sha: sha, started_at: startedAt, context: {vale: actionInput.version} }); diff --git a/vale-matcher.json b/vale-matcher.json deleted file mode 100644 index 76017f1b..00000000 --- a/vale-matcher.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "problemMatcher": [{ - "owner": "vale", - "pattern": [{ - "regexp": "^([^\\s]+\\.[^\\s]+)$", - "file": 1 - }, - { - "regexp": "^\\s*(\\d+):(\\d+)\\s+(error|warning|suggestion)\\s+(.*?)\\s\\s+(.+)$", - "line": 1, - "column": 2, - "severity": 3, - "message": 4, - "code": 5, - "loop": true - } - ] - }] -}