Skip to content

Commit 3079606

Browse files
mikehardystyfle
andauthored
chore: add prettier config, format file, add lint workflow (#63)
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> Co-authored-by: Steven <steven@ceriously.com>
1 parent 8af639a commit 3079606

File tree

9 files changed

+117
-51
lines changed

9 files changed

+117
-51
lines changed

.github/workflows/lint.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Build and Analyze
2+
3+
on:
4+
pull_request:
5+
push:
6+
workflow_dispatch:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/setup-node@v2
13+
with:
14+
node-version: v14
15+
- uses: actions/checkout@v2
16+
- run: yarn
17+
- run: yarn build
18+
- run: yarn format-check

.husky/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/pre-commit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
yarn build && git add dist/index.js
3+
yarn format-check

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
node_modules/

.prettierrc.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"printWidth": 100,
3+
"tabWidth": 2,
4+
"useTabs": false,
5+
"semi": true,
6+
"singleQuote": true,
7+
"trailingComma": "all",
8+
"bracketSpacing": true,
9+
"arrowParens": "avoid",
10+
"parser": "typescript"
11+
}

dist/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5848,7 +5848,7 @@ if (!core) {
58485848
throw new Error('Module not found: core');
58495849
}
58505850
async function main() {
5851-
const { eventName, sha, ref, repo: { owner, repo }, payload } = github.context;
5851+
const { eventName, sha, ref, repo: { owner, repo }, payload, } = github.context;
58525852
const { GITHUB_RUN_ID } = process.env;
58535853
let branch = ref.slice(11);
58545854
let headSha = sha;
@@ -5871,10 +5871,11 @@ async function main() {
58715871
const { data: current_run } = await octokit.actions.getWorkflowRun({
58725872
owner,
58735873
repo,
5874-
run_id: Number(GITHUB_RUN_ID)
5874+
run_id: Number(GITHUB_RUN_ID),
58755875
});
58765876
if (workflow_id) {
5877-
workflow_id.replace(/\s/g, '')
5877+
workflow_id
5878+
.replace(/\s/g, '')
58785879
.split(',')
58795880
.forEach(n => workflow_ids.push(n));
58805881
}
@@ -5884,7 +5885,7 @@ async function main() {
58845885
console.log(`Found workflow_id: ${JSON.stringify(workflow_ids)}`);
58855886
await Promise.all(workflow_ids.map(async (workflow_id) => {
58865887
try {
5887-
const { data: { total_count, workflow_runs } } = await octokit.actions.listWorkflowRuns({
5888+
const { data: { total_count, workflow_runs }, } = await octokit.actions.listWorkflowRuns({
58885889
per_page: 100,
58895890
owner,
58905891
repo,
@@ -5894,7 +5895,9 @@ async function main() {
58945895
console.log(`Found ${total_count} runs total.`);
58955896
let cancelBefore = new Date(current_run.created_at);
58965897
if (all_but_latest) {
5897-
const n = workflow_runs.map(run => new Date(run.created_at).getTime()).reduce((a, b) => Math.max(a, b), cancelBefore.getTime());
5898+
const n = workflow_runs
5899+
.map(run => new Date(run.created_at).getTime())
5900+
.reduce((a, b) => Math.max(a, b), cancelBefore.getTime());
58985901
cancelBefore = new Date(n);
58995902
}
59005903
const runningWorkflows = workflow_runs.filter(run => run.id !== current_run.id &&
@@ -5910,7 +5913,7 @@ async function main() {
59105913
const res = await octokit.actions.cancelWorkflowRun({
59115914
owner,
59125915
repo,
5913-
run_id: id
5916+
run_id: id,
59145917
});
59155918
console.log(`Cancel run ${id} responded with status ${res.status}`);
59165919
}

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
"main": "dist/index.js",
55
"license": "MIT",
66
"scripts": {
7-
"build": "ncc build src/index.ts --license LICENSES.txt"
7+
"build": "ncc build src/index.ts --license LICENSES.txt",
8+
"format": "prettier --write '**/*.ts'",
9+
"format-check": "prettier --check '**/*.ts'",
10+
"prepare": "husky install"
811
},
912
"dependencies": {
1013
"@actions/core": "1.2.6",
1114
"@actions/github": "4.0.0"
1215
},
1316
"devDependencies": {
1417
"@vercel/ncc": "0.27.0",
15-
"typescript": "4.1.5"
18+
"prettier": "2.2.1",
19+
"typescript": "4.1.5",
20+
"husky": "6.0.0"
1621
}
1722
}

src/index.ts

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ if (!core) {
1010
}
1111

1212
async function main() {
13-
const { eventName, sha, ref, repo: { owner, repo }, payload } = github.context;
13+
const {
14+
eventName,
15+
sha,
16+
ref,
17+
repo: { owner, repo },
18+
payload,
19+
} = github.context;
1420
const { GITHUB_RUN_ID } = process.env;
1521

1622
let branch = ref.slice(11);
@@ -35,63 +41,70 @@ async function main() {
3541
const { data: current_run } = await octokit.actions.getWorkflowRun({
3642
owner,
3743
repo,
38-
run_id: Number(GITHUB_RUN_ID)
44+
run_id: Number(GITHUB_RUN_ID),
3945
});
4046

4147
if (workflow_id) {
4248
// The user provided one or more workflow id
43-
workflow_id.replace(/\s/g, '')
49+
workflow_id
50+
.replace(/\s/g, '')
4451
.split(',')
4552
.forEach(n => workflow_ids.push(n));
4653
} else {
4754
// The user did not provide workflow id so derive from current run
4855
workflow_ids.push(String(current_run.workflow_id));
4956
}
5057
console.log(`Found workflow_id: ${JSON.stringify(workflow_ids)}`);
51-
await Promise.all(workflow_ids.map(async (workflow_id) => {
52-
try {
53-
const { data: { total_count, workflow_runs } } = await octokit.actions.listWorkflowRuns({
54-
per_page: 100,
55-
owner,
56-
repo,
57-
// @ts-ignore
58-
workflow_id,
59-
branch,
60-
});
61-
console.log(`Found ${total_count} runs total.`);
62-
let cancelBefore = new Date(current_run.created_at);
63-
if (all_but_latest) {
64-
const n = workflow_runs.map(run => new Date(run.created_at).getTime()).reduce((a, b) => Math.max(a, b), cancelBefore.getTime());
65-
cancelBefore = new Date(n);
66-
}
67-
const runningWorkflows = workflow_runs.filter(run =>
68-
run.id !== current_run.id &&
69-
(ignore_sha || run.head_sha !== headSha) &&
70-
run.status !== 'completed' &&
71-
new Date(run.created_at) < cancelBefore
72-
);
73-
if (all_but_latest && new Date(current_run.created_at) < cancelBefore) {
74-
// Make sure we cancel this run itself if it's out-of-date.
75-
// We must cancel this run last so we can cancel the others first.
76-
runningWorkflows.push(current_run);
77-
}
78-
console.log(`Found ${runningWorkflows.length} runs to cancel.`);
79-
for (const {id, head_sha, status, html_url} of runningWorkflows) {
80-
console.log('Canceling run: ', {id, head_sha, status, html_url});
81-
const res = await octokit.actions.cancelWorkflowRun({
58+
await Promise.all(
59+
workflow_ids.map(async workflow_id => {
60+
try {
61+
const {
62+
data: { total_count, workflow_runs },
63+
} = await octokit.actions.listWorkflowRuns({
64+
per_page: 100,
8265
owner,
8366
repo,
84-
run_id: id
67+
// @ts-ignore
68+
workflow_id,
69+
branch,
8570
});
86-
console.log(`Cancel run ${id} responded with status ${res.status}`);
71+
console.log(`Found ${total_count} runs total.`);
72+
let cancelBefore = new Date(current_run.created_at);
73+
if (all_but_latest) {
74+
const n = workflow_runs
75+
.map(run => new Date(run.created_at).getTime())
76+
.reduce((a, b) => Math.max(a, b), cancelBefore.getTime());
77+
cancelBefore = new Date(n);
78+
}
79+
const runningWorkflows = workflow_runs.filter(
80+
run =>
81+
run.id !== current_run.id &&
82+
(ignore_sha || run.head_sha !== headSha) &&
83+
run.status !== 'completed' &&
84+
new Date(run.created_at) < cancelBefore,
85+
);
86+
if (all_but_latest && new Date(current_run.created_at) < cancelBefore) {
87+
// Make sure we cancel this run itself if it's out-of-date.
88+
// We must cancel this run last so we can cancel the others first.
89+
runningWorkflows.push(current_run);
90+
}
91+
console.log(`Found ${runningWorkflows.length} runs to cancel.`);
92+
for (const { id, head_sha, status, html_url } of runningWorkflows) {
93+
console.log('Canceling run: ', { id, head_sha, status, html_url });
94+
const res = await octokit.actions.cancelWorkflowRun({
95+
owner,
96+
repo,
97+
run_id: id,
98+
});
99+
console.log(`Cancel run ${id} responded with status ${res.status}`);
100+
}
101+
} catch (e) {
102+
const msg = e.message || e;
103+
console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`);
87104
}
88-
89-
} catch (e) {
90-
const msg = e.message || e;
91-
console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`);
92-
}
93-
console.log('')
94-
}));
105+
console.log('');
106+
}),
107+
);
95108
}
96109

97110
main()

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ deprecation@^2.0.0, deprecation@^2.3.1:
126126
resolved "https://registry.yarnpkg.com/deprecation/-/deprecation-2.3.1.tgz#6368cbdb40abf3373b525ac87e4a260c3a700919"
127127
integrity sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==
128128

129+
husky@^6.0.0:
130+
version "6.0.0"
131+
resolved "https://registry.yarnpkg.com/husky/-/husky-6.0.0.tgz#810f11869adf51604c32ea577edbc377d7f9319e"
132+
integrity sha512-SQS2gDTB7tBN486QSoKPKQItZw97BMOd+Kdb6ghfpBc0yXyzrddI0oDV5MkDAbuB4X2mO3/nj60TRMcYxwzZeQ==
133+
129134
is-plain-object@^4.0.0:
130135
version "4.1.1"
131136
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-4.1.1.tgz#1a14d6452cbd50790edc7fdaa0aed5a40a35ebb5"
@@ -143,6 +148,11 @@ once@^1.4.0:
143148
dependencies:
144149
wrappy "1"
145150

151+
prettier@2.2.1:
152+
version "2.2.1"
153+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
154+
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
155+
146156
tunnel@0.0.6:
147157
version "0.0.6"
148158
resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c"

0 commit comments

Comments
 (0)