Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: ci

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
verify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version-file: .node-version
cache: npm

- name: Install Dependencies
id: npm-ci
run: npm ci

- name: Typecheck
run: npm run tsc
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.9.0
45 changes: 25 additions & 20 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
},
"scripts": {
"package": "npx rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
"package:watch": "npm run package -- --watch"
"package:watch": "npm run package -- --watch",
"tsc": "tsc --noEmit",
"all": "npm run tsc && npm run package"
},
"dependencies": {
"@actions/core": "^1.11.1",
Expand Down
50 changes: 25 additions & 25 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export async function run(): Promise<void> {
run_id: context.runId,
});
const startedAt = currentRun.data.run_started_at;
if (!startedAt) {
throw new Error("Missing run_started_at for current workflow run");
}

const currentRunDurationInMillis =
currentTime - new Date(startedAt).getTime();

Expand All @@ -38,15 +42,17 @@ export async function run(): Promise<void> {
"No data for historical runs on master/main branch found. Can't compare.";
} else {
const latestRunOnMaster = latestRunsOnMaster[0];
if (!latestRunOnMaster.run_started_at) {
throw new Error("Missing run_started_at for latest run on master");
}
const latestMasterRunDurationInMillis =
new Date(latestRunOnMaster.updated_at).getTime() -
new Date(latestRunOnMaster.run_started_at).getTime();
const diffInSeconds =
(currentRunDurationInMillis - latestMasterRunDurationInMillis) / 1000;
const percentageDiff = (
const percentageDiff =
(1 - currentRunDurationInMillis / latestMasterRunDurationInMillis) *
100
).toFixed(2);
100;
const outcome = diffInSeconds > 0 ? "an increase" : "a decrease";

outputMessage =
Expand All @@ -59,44 +65,38 @@ export async function run(): Promise<void> {
" with " +
Math.abs(diffInSeconds) +
"s (" +
Math.abs(percentageDiff) +
Math.abs(percentageDiff).toFixed(2) +
"%) compared to latest run on master/main.";
}

const commentData = {
const existingComments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
};

const comments = await github.rest.issues.listComments(commentData);

/**
* Add the body content after comments are fetched so that it's
* not included in the search for existing comments.
*/
commentData.body = outputMessage;

/**
* Search comments from the bottom-up to find the most recent comment
* from the GitHub Actions bot that matches our criteria.
*/
const existingComment = comments.data.reverse().find((comment) => {
});
const existingComment = existingComments.data.reverse().find((comment) => {
return (
comment?.user?.login === "github-actions[bot]" &&
comment?.user?.type === "Bot" &&
comment?.body?.startsWith(`🕒 Workflow "${context.workflow}" took `)
);
});

// If the comment exists then update instead of creating a new one.
const action = existingComment ? "updateComment" : "createComment";
const commentInput = {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: outputMessage,
};

if (existingComment) {
commentData.comment_id = existingComment.id;
await github.rest.issues["updateComment"]({
...commentInput,
comment_id: existingComment.id,
});
} else {
await github.rest.issues["createComment"](commentInput);
}

await github.rest.issues[action](commentData);
} catch (error) {
if (error instanceof Error) core.setFailed(error.message);
}
Expand Down