Skip to content

Commit 71182eb

Browse files
authored
Fix typescript errors (#11)
In addition, I've added a github actions workflow which performs typecheck on PR. Could later be expanded to running tests and linting as well.
1 parent 7940d16 commit 71182eb

File tree

6 files changed

+82
-47
lines changed

6 files changed

+82
-47
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: ci
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
verify:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- uses: actions/setup-node@v4
18+
with:
19+
node-version-file: .node-version
20+
cache: npm
21+
22+
- name: Install Dependencies
23+
id: npm-ci
24+
run: npm ci
25+
26+
- name: Typecheck
27+
run: npm run tsc

.node-version

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

dist/index.js

Lines changed: 25 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
},
1414
"scripts": {
1515
"package": "npx rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
16-
"package:watch": "npm run package -- --watch"
16+
"package:watch": "npm run package -- --watch",
17+
"tsc": "tsc --noEmit",
18+
"all": "npm run tsc && npm run package"
1719
},
1820
"dependencies": {
1921
"@actions/core": "^1.11.1",

src/main.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export async function run(): Promise<void> {
1515
run_id: context.runId,
1616
});
1717
const startedAt = currentRun.data.run_started_at;
18+
if (!startedAt) {
19+
throw new Error("Missing run_started_at for current workflow run");
20+
}
21+
1822
const currentRunDurationInMillis =
1923
currentTime - new Date(startedAt).getTime();
2024

@@ -38,15 +42,17 @@ export async function run(): Promise<void> {
3842
"No data for historical runs on master/main branch found. Can't compare.";
3943
} else {
4044
const latestRunOnMaster = latestRunsOnMaster[0];
45+
if (!latestRunOnMaster.run_started_at) {
46+
throw new Error("Missing run_started_at for latest run on master");
47+
}
4148
const latestMasterRunDurationInMillis =
4249
new Date(latestRunOnMaster.updated_at).getTime() -
4350
new Date(latestRunOnMaster.run_started_at).getTime();
4451
const diffInSeconds =
4552
(currentRunDurationInMillis - latestMasterRunDurationInMillis) / 1000;
46-
const percentageDiff = (
53+
const percentageDiff =
4754
(1 - currentRunDurationInMillis / latestMasterRunDurationInMillis) *
48-
100
49-
).toFixed(2);
55+
100;
5056
const outcome = diffInSeconds > 0 ? "an increase" : "a decrease";
5157

5258
outputMessage =
@@ -59,44 +65,38 @@ export async function run(): Promise<void> {
5965
" with " +
6066
Math.abs(diffInSeconds) +
6167
"s (" +
62-
Math.abs(percentageDiff) +
68+
Math.abs(percentageDiff).toFixed(2) +
6369
"%) compared to latest run on master/main.";
6470
}
6571

66-
const commentData = {
72+
const existingComments = await github.rest.issues.listComments({
6773
owner: context.repo.owner,
6874
repo: context.repo.repo,
6975
issue_number: context.issue.number,
70-
};
71-
72-
const comments = await github.rest.issues.listComments(commentData);
73-
74-
/**
75-
* Add the body content after comments are fetched so that it's
76-
* not included in the search for existing comments.
77-
*/
78-
commentData.body = outputMessage;
79-
80-
/**
81-
* Search comments from the bottom-up to find the most recent comment
82-
* from the GitHub Actions bot that matches our criteria.
83-
*/
84-
const existingComment = comments.data.reverse().find((comment) => {
76+
});
77+
const existingComment = existingComments.data.reverse().find((comment) => {
8578
return (
8679
comment?.user?.login === "github-actions[bot]" &&
8780
comment?.user?.type === "Bot" &&
8881
comment?.body?.startsWith(`🕒 Workflow "${context.workflow}" took `)
8982
);
9083
});
9184

92-
// If the comment exists then update instead of creating a new one.
93-
const action = existingComment ? "updateComment" : "createComment";
85+
const commentInput = {
86+
owner: context.repo.owner,
87+
repo: context.repo.repo,
88+
issue_number: context.issue.number,
89+
body: outputMessage,
90+
};
9491

9592
if (existingComment) {
96-
commentData.comment_id = existingComment.id;
93+
await github.rest.issues["updateComment"]({
94+
...commentInput,
95+
comment_id: existingComment.id,
96+
});
97+
} else {
98+
await github.rest.issues["createComment"](commentInput);
9799
}
98-
99-
await github.rest.issues[action](commentData);
100100
} catch (error) {
101101
if (error instanceof Error) core.setFailed(error.message);
102102
}

0 commit comments

Comments
 (0)