Skip to content

Commit

Permalink
chore: add prettier config, format file, add lint workflow
Browse files Browse the repository at this point in the history
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 <steven@ceriously.com>
  • Loading branch information
mikehardy and styfle committed Apr 11, 2021
1 parent 8af639a commit f63c29e
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 51 deletions.
18 changes: 18 additions & 0 deletions .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
1 change: 1 addition & 0 deletions .husky/.gitignore
@@ -0,0 +1 @@
_
3 changes: 3 additions & 0 deletions .husky/pre-commit
@@ -0,0 +1,3 @@
#!/bin/sh
yarn build && git add dist/index.js
yarn format-check
2 changes: 2 additions & 0 deletions .prettierignore
@@ -0,0 +1,2 @@
dist/
node_modules/
11 changes: 11 additions & 0 deletions .prettierrc.json
@@ -0,0 +1,11 @@
{
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"arrowParens": "avoid",
"parser": "typescript"
}
15 changes: 9 additions & 6 deletions dist/index.js
Expand Up @@ -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;
Expand All @@ -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));
}
Expand All @@ -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,
Expand All @@ -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 &&
Expand All @@ -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}`);
}
Expand Down
9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -4,14 +4,19 @@
"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",
"@actions/github": "4.0.0"
},
"devDependencies": {
"@vercel/ncc": "0.27.0",
"typescript": "4.1.5"
"prettier": "2.2.1",
"typescript": "4.1.5",
"husky": "6.0.0"
}
}
99 changes: 56 additions & 43 deletions src/index.ts
Expand Up @@ -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);
Expand All @@ -35,63 +41,70 @@ 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 {
// The user did not provide workflow id so derive from current run
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()
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit f63c29e

Please sign in to comment.