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
  • Loading branch information
mikehardy committed Apr 5, 2021
1 parent 3d86a7c commit 55cf49e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 37 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
node_modules/
11 changes: 11 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"arrowParens": "avoid",
"parser": "typescript"
}
5 changes: 3 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5873,7 +5873,8 @@ async function main() {
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 @@ -5887,7 +5888,7 @@ async function main() {
owner,
repo,
workflow_id,
branch,
branch
});
const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch);
console.log(`Found ${branchWorkflows.length} runs for workflow ${workflow_id} on branch ${branch}`);
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"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'"
},
"dependencies": {
"@actions/core": "1.2.6",
"@actions/github": "4.0.0"
},
"devDependencies": {
"@vercel/ncc": "0.27.0",
"prettier": "2.2.1",
"typescript": "4.1.5"
}
}
80 changes: 46 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -39,7 +45,8 @@ async function main() {

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 {
Expand All @@ -49,42 +56,47 @@ async function main() {

console.log(`Found workflow_id: ${JSON.stringify(workflow_ids)}`);

await Promise.all(workflow_ids.map(async (workflow_id) => {
try {
const { data } = await octokit.actions.listWorkflowRuns({
owner,
repo,
// @ts-ignore
workflow_id,
branch,
});

const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch);
console.log(`Found ${branchWorkflows.length} runs for workflow ${workflow_id} on branch ${branch}`);
console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n'));

const runningWorkflows = branchWorkflows.filter(run =>
(ignore_sha || run.head_sha !== headSha) &&
run.status !== 'completed' &&
new Date(run.created_at) < new Date(current_run.created_at)
);
console.log(`with ${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 } = await octokit.actions.listWorkflowRuns({
owner,
repo,
run_id: id
// @ts-ignore
workflow_id,
branch
});
console.log(`Cancel run ${id} responded with status ${res.status}`);

const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch);
console.log(
`Found ${branchWorkflows.length} runs for workflow ${workflow_id} on branch ${branch}`
);
console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n'));

const runningWorkflows = branchWorkflows.filter(
run =>
(ignore_sha || run.head_sha !== headSha) &&
run.status !== 'completed' &&
new Date(run.created_at) < new Date(current_run.created_at)
);
console.log(`with ${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
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,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 55cf49e

Please sign in to comment.