From f63c29e4929a468b77820daeed6b6e38c5bb3c7b Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Sun, 11 Apr 2021 09:49:33 -0500 Subject: [PATCH] chore: add prettier config, format file, add lint workflow The prettier config was adapted from the official GitHub Actions repo, bent to fit the prevailing style (where possible) already in the project The intent is not to be controversial or argue about whitespace, it is just to have a consistent easy-to-verify style specifically to avoid all arguments about whitespace. If anything in here is objectionable, just name the setting to alter and I can edit / re-format / re-push chore: add husky and hook build/format/lint checks to pre-commit This enforces the same checks locally that will execute in CI With this, everyone should have a clean / consistent dev environment, and it will be clear to contributors if they submit code that is not valid typescript Additionally, after doing the build it adds the dist/index.js output to the commit list so contributors can't forget to commit it lint(prettier): trailingComma preferred as all vs none Co-authored-by: Steven --- .github/workflows/lint.yml | 18 +++++++ .husky/.gitignore | 1 + .husky/pre-commit | 3 ++ .prettierignore | 2 + .prettierrc.json | 11 +++++ dist/index.js | 15 +++--- package.json | 9 +++- src/index.ts | 99 +++++++++++++++++++++----------------- yarn.lock | 10 ++++ 9 files changed, 117 insertions(+), 51 deletions(-) create mode 100644 .github/workflows/lint.yml create mode 100644 .husky/.gitignore create mode 100755 .husky/pre-commit create mode 100644 .prettierignore create mode 100644 .prettierrc.json diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..be722041 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,18 @@ +name: Build and Analyze + +on: + pull_request: + push: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/setup-node@v2 + with: + node-version: v14 + - uses: actions/checkout@v2 + - run: yarn + - run: yarn build + - run: yarn format-check diff --git a/.husky/.gitignore b/.husky/.gitignore new file mode 100644 index 00000000..31354ec1 --- /dev/null +++ b/.husky/.gitignore @@ -0,0 +1 @@ +_ diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 00000000..33feda64 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,3 @@ +#!/bin/sh +yarn build && git add dist/index.js +yarn format-check diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 00000000..1eae0cf6 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +dist/ +node_modules/ diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 00000000..1e336995 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,11 @@ +{ + "printWidth": 100, + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": true, + "trailingComma": "all", + "bracketSpacing": true, + "arrowParens": "avoid", + "parser": "typescript" +} diff --git a/dist/index.js b/dist/index.js index e884532e..c6d81e52 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5848,7 +5848,7 @@ if (!core) { throw new Error('Module not found: core'); } async function main() { - const { eventName, sha, ref, repo: { owner, repo }, payload } = github.context; + const { eventName, sha, ref, repo: { owner, repo }, payload, } = github.context; const { GITHUB_RUN_ID } = process.env; let branch = ref.slice(11); let headSha = sha; @@ -5871,10 +5871,11 @@ async function main() { const { data: current_run } = await octokit.actions.getWorkflowRun({ owner, repo, - run_id: Number(GITHUB_RUN_ID) + run_id: Number(GITHUB_RUN_ID), }); if (workflow_id) { - workflow_id.replace(/\s/g, '') + workflow_id + .replace(/\s/g, '') .split(',') .forEach(n => workflow_ids.push(n)); } @@ -5884,7 +5885,7 @@ async function main() { console.log(`Found workflow_id: ${JSON.stringify(workflow_ids)}`); await Promise.all(workflow_ids.map(async (workflow_id) => { try { - const { data: { total_count, workflow_runs } } = await octokit.actions.listWorkflowRuns({ + const { data: { total_count, workflow_runs }, } = await octokit.actions.listWorkflowRuns({ per_page: 100, owner, repo, @@ -5894,7 +5895,9 @@ async function main() { console.log(`Found ${total_count} runs total.`); let cancelBefore = new Date(current_run.created_at); if (all_but_latest) { - const n = workflow_runs.map(run => new Date(run.created_at).getTime()).reduce((a, b) => Math.max(a, b), cancelBefore.getTime()); + const n = workflow_runs + .map(run => new Date(run.created_at).getTime()) + .reduce((a, b) => Math.max(a, b), cancelBefore.getTime()); cancelBefore = new Date(n); } const runningWorkflows = workflow_runs.filter(run => run.id !== current_run.id && @@ -5910,7 +5913,7 @@ async function main() { const res = await octokit.actions.cancelWorkflowRun({ owner, repo, - run_id: id + run_id: id, }); console.log(`Cancel run ${id} responded with status ${res.status}`); } diff --git a/package.json b/package.json index 2d516591..b83c82e1 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,10 @@ "main": "dist/index.js", "license": "MIT", "scripts": { - "build": "ncc build src/index.ts --license LICENSES.txt" + "build": "ncc build src/index.ts --license LICENSES.txt", + "format": "prettier --write '**/*.ts'", + "format-check": "prettier --check '**/*.ts'", + "prepare": "husky install" }, "dependencies": { "@actions/core": "1.2.6", @@ -12,6 +15,8 @@ }, "devDependencies": { "@vercel/ncc": "0.27.0", - "typescript": "4.1.5" + "prettier": "2.2.1", + "typescript": "4.1.5", + "husky": "6.0.0" } } diff --git a/src/index.ts b/src/index.ts index 578caaf1..bb6783cd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,13 @@ if (!core) { } async function main() { - const { eventName, sha, ref, repo: { owner, repo }, payload } = github.context; + const { + eventName, + sha, + ref, + repo: { owner, repo }, + payload, + } = github.context; const { GITHUB_RUN_ID } = process.env; let branch = ref.slice(11); @@ -35,12 +41,13 @@ async function main() { const { data: current_run } = await octokit.actions.getWorkflowRun({ owner, repo, - run_id: Number(GITHUB_RUN_ID) + run_id: Number(GITHUB_RUN_ID), }); if (workflow_id) { // The user provided one or more workflow id - workflow_id.replace(/\s/g, '') + workflow_id + .replace(/\s/g, '') .split(',') .forEach(n => workflow_ids.push(n)); } else { @@ -48,50 +55,56 @@ async function main() { workflow_ids.push(String(current_run.workflow_id)); } console.log(`Found workflow_id: ${JSON.stringify(workflow_ids)}`); - await Promise.all(workflow_ids.map(async (workflow_id) => { - try { - const { data: { total_count, workflow_runs } } = await octokit.actions.listWorkflowRuns({ - per_page: 100, - owner, - repo, - // @ts-ignore - workflow_id, - branch, - }); - console.log(`Found ${total_count} runs total.`); - let cancelBefore = new Date(current_run.created_at); - if (all_but_latest) { - const n = workflow_runs.map(run => new Date(run.created_at).getTime()).reduce((a, b) => Math.max(a, b), cancelBefore.getTime()); - cancelBefore = new Date(n); - } - const runningWorkflows = workflow_runs.filter(run => - run.id !== current_run.id && - (ignore_sha || run.head_sha !== headSha) && - run.status !== 'completed' && - new Date(run.created_at) < cancelBefore - ); - if (all_but_latest && new Date(current_run.created_at) < cancelBefore) { - // Make sure we cancel this run itself if it's out-of-date. - // We must cancel this run last so we can cancel the others first. - runningWorkflows.push(current_run); - } - console.log(`Found ${runningWorkflows.length} runs to cancel.`); - for (const {id, head_sha, status, html_url} of runningWorkflows) { - console.log('Canceling run: ', {id, head_sha, status, html_url}); - const res = await octokit.actions.cancelWorkflowRun({ + await Promise.all( + workflow_ids.map(async workflow_id => { + try { + const { + data: { total_count, workflow_runs }, + } = await octokit.actions.listWorkflowRuns({ + per_page: 100, owner, repo, - run_id: id + // @ts-ignore + workflow_id, + branch, }); - console.log(`Cancel run ${id} responded with status ${res.status}`); + console.log(`Found ${total_count} runs total.`); + let cancelBefore = new Date(current_run.created_at); + if (all_but_latest) { + const n = workflow_runs + .map(run => new Date(run.created_at).getTime()) + .reduce((a, b) => Math.max(a, b), cancelBefore.getTime()); + cancelBefore = new Date(n); + } + const runningWorkflows = workflow_runs.filter( + run => + run.id !== current_run.id && + (ignore_sha || run.head_sha !== headSha) && + run.status !== 'completed' && + new Date(run.created_at) < cancelBefore, + ); + if (all_but_latest && new Date(current_run.created_at) < cancelBefore) { + // Make sure we cancel this run itself if it's out-of-date. + // We must cancel this run last so we can cancel the others first. + runningWorkflows.push(current_run); + } + console.log(`Found ${runningWorkflows.length} runs to cancel.`); + for (const { id, head_sha, status, html_url } of runningWorkflows) { + console.log('Canceling run: ', { id, head_sha, status, html_url }); + const res = await octokit.actions.cancelWorkflowRun({ + owner, + repo, + run_id: id, + }); + console.log(`Cancel run ${id} responded with status ${res.status}`); + } + } catch (e) { + const msg = e.message || e; + console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`); } - - } catch (e) { - const msg = e.message || e; - console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`); - } - console.log('') - })); + console.log(''); + }), + ); } main() diff --git a/yarn.lock b/yarn.lock index 0da1ae85..e53b1b64 100644 --- a/yarn.lock +++ b/yarn.lock @@ -126,6 +126,11 @@ deprecation@^2.0.0, deprecation@^2.3.1: resolved "https://registry.yarnpkg.com/deprecation/-/deprecation-2.3.1.tgz#6368cbdb40abf3373b525ac87e4a260c3a700919" integrity sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ== +husky@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/husky/-/husky-6.0.0.tgz#810f11869adf51604c32ea577edbc377d7f9319e" + integrity sha512-SQS2gDTB7tBN486QSoKPKQItZw97BMOd+Kdb6ghfpBc0yXyzrddI0oDV5MkDAbuB4X2mO3/nj60TRMcYxwzZeQ== + is-plain-object@^4.0.0: version "4.1.1" resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-4.1.1.tgz#1a14d6452cbd50790edc7fdaa0aed5a40a35ebb5" @@ -143,6 +148,11 @@ once@^1.4.0: dependencies: wrappy "1" +prettier@2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5" + integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q== + tunnel@0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c"