5
0
Fork 0
mirror of https://github.com/pre-commit/action.git synced 2025-11-07 10:46:56 +00:00

simplifications

This commit is contained in:
Anthony Sottile 2020-06-13 12:40:24 -07:00
parent 4a08748ace
commit 5a37a08ca7

View file

@ -1,57 +1,31 @@
const os = require('os'); const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto'); const crypto = require('crypto');
const fs = require('fs');
const os = require('os');
const path = require('path');
const cache = require('@actions/cache');
const core = require('@actions/core'); const core = require('@actions/core');
const exec = require('@actions/exec'); const exec = require('@actions/exec');
const github = require('@actions/github'); const github = require('@actions/github');
const tr = require('@actions/exec/lib/toolrunner'); const tr = require('@actions/exec/lib/toolrunner');
const cache = require('@actions/cache');
const paths = [path.join(os.homedir(), '.cache', 'pre-commit')];
async function getPythonVersion() {
let myOutput = '';
const options = {
silent: true,
listeners: {
stdout: (data) => {
myOutput += data.toString();
}
}
};
await exec.exec('python', ['-VV'], options);
await exec.exec('which', ['python'], options);
return myOutput;
}
function hashFile(filePath) {
return hashString(fs.readFileSync(filePath).toString());
}
function hashString(content) { function hashString(content) {
const sha256 = crypto.createHash('sha256'); const sha256 = crypto.createHash('sha256');
return sha256.update(content).digest('hex'); return sha256.update(content).digest('hex');
} }
async function setupCache(cacheKey) { function getPythonVersion() {
try { const args = ['-c', 'import sys;print(sys.executable+"\\n"+sys.version)'];
await cache.saveCache(paths, cacheKey); const res = child_process.spawnSync('python', args);
} catch (e) { if (res.status !== 0) {
if (e.toString().includes('reserveCache failed')) { throw 'python version check failed';
// race condition
core.info(e.message);
return;
}
throw e;
} }
return res.stdout.toString();
} }
async function restoreCache(cacheKey) { function hashFile(filePath) {
return !!(await cache.restoreCache(paths, cacheKey)); return hashString(fs.readFileSync(filePath).toString());
} }
function addToken(url, token) { function addToken(url, token) {
@ -74,11 +48,12 @@ async function main() {
const pr = github.context.payload.pull_request; const pr = github.context.payload.pull_request;
const push = !!token && !!pr; const push = !!token && !!pr;
const pyVer = await getPythonVersion(); const cachePaths = [path.join(os.homedir(), '.cache', 'pre-commit')];
const cacheKey = `pre-commit-1-${hashString(pyVer)}-${hashFile('.pre-commit-config.yaml')}`; const py = getPythonVersion();
await restoreCache(cacheKey); const cacheKey = `pre-commit-2-${hashString(py)}-${hashFile('.pre-commit-config.yaml')}`;
await cache.restoreCache(cachePaths, cacheKey);
const ret = await exec.exec('pre-commit', args, {ignoreReturnCode: push}); const ret = await exec.exec('pre-commit', args, {ignoreReturnCode: push});
await setupCache(cacheKey); await cache.saveCache(cachePaths, cacheKey);
if (ret && push) { if (ret && push) {
// actions do not run on pushes made by actions. // actions do not run on pushes made by actions.