Skip to content

Commit

Permalink
Expose version as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed May 10, 2022
1 parent ff94479 commit 42e2539
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 6,142 deletions.
12 changes: 4 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ branding:
color: green

inputs:
styles:
description: A list of styles to install prior to running Vale.
required: false

config:
description: An externally-hosted configuration file to use.
version:
description: 'The Vale CLI version to install.'
required: false
default: 'latest'

files:
description: 'The files to lint: "all" or "<some_folder>".'
Expand All @@ -33,8 +30,7 @@ runs:
using: docker
image: Dockerfile
args:
- ${{ inputs.styles }}
- ${{ inputs.config }}
- ${{ inputs.version }}
- ${{ inputs.files }}
- ${{ inputs.onlyAnnotateModifiedLines }}
- ${{ inputs.debug }}
14 changes: 13 additions & 1 deletion lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,19 @@ function getCommits() {
});
break;
default:
core.warning(`Unrecognized event: ${CTX.eventName}`);
if (process.env.OVERRIDE_PR_NUMBER) {
const resp = yield API.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/commits', {
owner: CTX.repo.owner,
repo: CTX.repo.repo,
pull_number: process.env.OVERRIDE_PR_NUMBER
});
resp.data.forEach((commit) => {
commits.push(commit.sha);
});
}
else {
core.warning(`Unrecognized event: ${CTX.eventName}`);
}
}
return commits;
});
Expand Down
54 changes: 1 addition & 53 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const request = __importStar(require("request-promise-native"));
const git_1 = require("./git");
/**
* Log debugging information to `stdout`.
Expand All @@ -49,7 +48,7 @@ function logIfDebug(msg) {
/**
* Parse our user input and set up our Vale environment.
*/
function get(tmp, tok, dir) {
function get(tok, dir) {
return __awaiter(this, void 0, void 0, function* () {
let modified = {};
// Get the current version of Vale:
Expand All @@ -63,57 +62,6 @@ function get(tmp, tok, dir) {
version = version.split(' ').slice(-1)[0];
logIfDebug(`Using Vale ${version}`);
let args = ['--no-exit', '--output=JSON'];
// Check if we were given an external config file.
//
// NOTE: We need to do this first because we may not have a local config file
// to read the `StylesPath` from.
const config = core.getInput('config');
if (config !== '') {
logIfDebug(`Downloading external config '${config}' ...`);
yield request
.get(config)
.catch(error => {
core.warning(`Failed to fetch remote config: ${error}.`);
})
.then(body => {
try {
fs.writeFileSync(tmp.name, body);
logIfDebug(`Successfully fetched remote config.`);
args.push('--mode-rev-compat');
args.push(`--config=${tmp.name}`);
}
catch (e) {
core.warning(`Failed to write config: ${e}.`);
}
});
}
// Install our user-specified styles:
const styles = core.getInput('styles').split('\n');
for (const style of styles) {
if (style !== '') {
const name = style
.split('/')
.slice(-1)[0]
.split('.zip')[0];
logIfDebug(`Installing style '${name}' ...`);
let cmd = ['install', name, style];
if (args.length > 2) {
cmd = args.concat(cmd);
}
let stderr = '';
let resp = yield exec.exec('vale', cmd, {
cwd: dir,
listeners: {
stderr: (data) => {
stderr += data.toString();
}
}
});
if (resp == 2) {
core.setFailed(stderr);
}
}
}
// Figure out what we're supposed to lint:
const files = core.getInput('files');
if (core.getInput('onlyAnnotateModifiedLines') != 'false' ||
Expand Down
59 changes: 59 additions & 0 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"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) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.installLint = void 0;
const core = __importStar(require("@actions/core"));
const tc = __importStar(require("@actions/tool-cache"));
const path_1 = __importDefault(require("path"));
const releases = 'https://github.com/errata-ai/vale/releases/download';
function installLint(version) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing Vale ${version} ...`);
const url = releases + `/v${version}/vale_${version}_Linux_64-bit.tar.gz`;
const startedAt = Date.now();
const archivePath = yield tc.downloadTool(url);
let extractedDir = '';
let repl = /\.tar\.gz$/;
const args = ['xz'];
if (process.platform.toString() != 'darwin') {
args.push('--overwrite');
}
extractedDir = yield tc.extractTar(archivePath, process.env.HOME, args);
const urlParts = url.split(`/`);
const dirName = urlParts[urlParts.length - 1].replace(repl, ``);
const lintPath = path_1.default.join(extractedDir, dirName, `vale`);
core.info(`Installed Vale into ${lintPath} in ${Date.now() - startedAt}ms.`);
return lintPath;
});
}
exports.installLint = installLint;
46 changes: 17 additions & 29 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
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 check_1 = require("./check");
const input = __importStar(require("./input"));
const install_1 = require("./install");
const execa_1 = __importDefault(require("execa"));
/**
* These environment variables are exposed for GitHub Actions.
Expand All @@ -48,30 +46,17 @@ function run(actionInput) {
return __awaiter(this, void 0, void 0, function* () {
try {
const startedAt = new Date().toISOString();
const alertResp = yield execa_1.default('vale', actionInput.args);
let runner = new check_1.CheckRunner(actionInput.files);
let sha = github.context.sha;
if (github.context.payload.pull_request) {
sha = github.context.payload.pull_request.head.sha;
}
// Allow to customize the SHA to use for the check
// useful when using the action with a workflow_run/completed event
if (process.env.OVERRIDE_GITHUB_SHA) {
sha = process.env.OVERRIDE_GITHUB_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: sha,
started_at: startedAt,
context: { vale: actionInput.version }
});
const localVale = yield install_1.installLint(actionInput.version);
const alertResp = yield execa_1.default(localVale, actionInput.args);
core.info(alertResp.stdout);
}
catch (error) {
core.setFailed(error.stderr);
if (typeof error === 'string') {
core.setFailed(error.toUpperCase());
}
else if (error instanceof Error) {
core.setFailed(error.message);
}
}
});
}
Expand All @@ -81,13 +66,16 @@ function main() {
try {
const userToken = GITHUB_TOKEN;
const workspace = GITHUB_WORKSPACE;
const tmpobj = tmp.fileSync({ postfix: '.ini', dir: workspace });
const actionInput = yield input.get(tmpobj, userToken, workspace);
const actionInput = yield input.get(userToken, workspace);
yield run(actionInput);
tmpobj.removeCallback();
}
catch (error) {
core.setFailed(error.message);
if (typeof error === 'string') {
core.setFailed(error.toUpperCase());
}
else if (error instanceof Error) {
core.setFailed(error.message);
}
}
});
}
Expand Down
Loading

0 comments on commit 42e2539

Please sign in to comment.