Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jun 21, 2020
1 parent de680a8 commit 2c4a0b9
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 77 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

<p align="center">
<img width="495" alt="demo" src="https://user-images.githubusercontent.com/8785025/67726924-bf3e6180-f9a4-11e9-9c32-2233756731b9.png">
<img width="50%" alt="A demo screenshot." src="https://user-images.githubusercontent.com/8785025/85236358-272d3680-b3d2-11ea-8793-0f45cb70189a.png">
</p>

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:
Expand All @@ -26,8 +22,20 @@ jobs:
uses: actions/checkout@master

- name: Vale
uses: errata-ai/[email protected]
uses: errata-ai/[email protected]
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}}
```
Expand All @@ -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
Expand Down
34 changes: 23 additions & 11 deletions lib/check.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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})`;
/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 20 additions & 7 deletions lib/input.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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"));
Expand Down
36 changes: 27 additions & 9 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -8,38 +27,37 @@ 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.
*
* See https://bit.ly/2WlFUD7 for more information.
*/
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 }
});
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
29 changes: 14 additions & 15 deletions src/check.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -43,7 +44,7 @@ interface CheckOptions {
* See https://developer.github.com/v3/checks.
*/
export class CheckRunner {
private annotations: Array<octokit.ChecksCreateParamsOutputAnnotations>;
private annotations: Array<ChecksCreateParamsOutputAnnotations>;
private stats: Stats;

constructor() {
Expand Down Expand Up @@ -91,7 +92,7 @@ export class CheckRunner {
public async executeCheck(options: CheckOptions): Promise<void> {
core.info(`Vale: ${this.getSummary()}`);

const client = new github.GitHub(options.token, {
const client = github.getOctokit(options.token, {
userAgent: USER_AGENT,
});

Expand Down Expand Up @@ -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<number> {
const response = await client.checks.create({
owner: options.owner,
Expand All @@ -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<void> {
let annotations = this.getBucket();

Expand Down Expand Up @@ -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<void> {
let req: any = {
owner: options.owner,
Expand Down Expand Up @@ -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<void> {
let req: any = {
owner: options.owner,
Expand Down Expand Up @@ -259,18 +260,16 @@ 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)));
}

/**
* Create buckets of at most 50 annotations for the API.
*
* See https://developer.github.com/v3/checks/runs/#output-object.
*/
private getBucket(): Array<octokit.ChecksCreateParamsOutputAnnotations> {
let annotations: Array<octokit.ChecksCreateParamsOutputAnnotations> = [];
private getBucket(): Array<ChecksCreateParamsOutputAnnotations> {
let annotations: Array<ChecksCreateParamsOutputAnnotations> = [];
while (annotations.length < 50) {
const annotation = this.annotations.pop();
if (annotation) {
Expand Down Expand Up @@ -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':
Expand All @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -19,14 +19,19 @@ export async function run(actionInput: input.Input): Promise<void> {
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}
});
Expand Down
Loading

0 comments on commit 2c4a0b9

Please sign in to comment.