Skip to content

Commit ed0fa87

Browse files
feat: update existing comments if they exist (#9)
This update relates to issue #7 to update existing comments in a pull request on subsequent runs. In larger repos, this will reduce the amount of comments being added to a PR during its review cycle. --------- Co-authored-by: castastrophe <[email protected]>
1 parent 9571eec commit ed0fa87

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ As the **very last job** in your workflow, add
1616
- name: Time reporter
1717
uses: DeviesDevelopment/[email protected]
1818
```
19+
1920
Workflow-timer compares the current workflow run with the latest run on the master/main branch. Therefore, the same workflow needs to run when merging with the master as well, otherwise, there will be no data to compare. We suggest having the following definition in the workflow:
2021
21-
```
22+
```yaml
2223
on:
2324
push:
2425
branches: master
@@ -27,4 +28,5 @@ on:
2728
```
2829
2930
## How to contribute
31+
3032
Feel free to open a pull request! All contributions, no matter how small, are more than welcome. Happy hacking!

action.yml

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
name: "Workflow Timer"
22
description: "Measures the duration of the workflow and compares it to the duration of historical runs."
33
branding:
4-
icon: 'clock'
5-
color: 'green'
6-
4+
icon: "clock"
5+
color: "green"
76

87
runs:
98
using: "composite"
@@ -15,6 +14,7 @@ runs:
1514
if (context.eventName != 'pull_request') {
1615
return
1716
}
17+
1818
const currentTime = new Date().getTime();
1919
const currentRun = await github.rest.actions.getWorkflowRun({
2020
owner: context.repo.owner,
@@ -23,7 +23,7 @@ runs:
2323
});
2424
const startedAt = currentRun.data.run_started_at
2525
const currentRunDurationInMillis = currentTime - new Date(startedAt).getTime();
26-
26+
2727
const workflowId = currentRun.data.workflow_id
2828
const historical_runs = await github.rest.actions.listWorkflowRuns({
2929
owner: context.repo.owner,
@@ -37,7 +37,7 @@ runs:
3737
x.conclusion == 'success'
3838
);
3939
40-
var outputMessage = ''
40+
let outputMessage = ''
4141
if (latestRunsOnMaster.length === 0) {
4242
outputMessage = "No data for historical runs on master/main branch found. Can't compare."
4343
} else {
@@ -46,13 +46,41 @@ runs:
4646
const diffInSeconds = (currentRunDurationInMillis - latestMasterRunDurationInMillis) / 1000
4747
const percentageDiff = ((1 - (currentRunDurationInMillis/latestMasterRunDurationInMillis)) * 100).toFixed(2)
4848
const outcome = (diffInSeconds > 0) ? "an increase" : "a decrease"
49-
49+
5050
outputMessage = '🕒 Workflow \"' + context.workflow + '\" took ' + (currentRunDurationInMillis / 1000) + 's which is ' + outcome + ' with ' + Math.abs(diffInSeconds) + 's (' + Math.abs(percentageDiff) + '%) compared to latest run on master/main.'
5151
}
5252
53-
github.rest.issues.createComment({
54-
issue_number: context.issue.number,
53+
const commentData = {
5554
owner: context.repo.owner,
5655
repo: context.repo.repo,
57-
body: outputMessage
56+
issue_number: context.issue.number,
57+
};
58+
59+
const comments = await github.rest.issues.listComments(commentData);
60+
61+
/**
62+
* Add the body content after comments are fetched so that it's
63+
* not included in the search for existing comments.
64+
*/
65+
commentData.body = outputMessage;
66+
67+
/**
68+
* Search comments from the bottom-up to find the most recent comment
69+
* from the GitHub Actions bot that matches our criteria.
70+
*/
71+
const existingComment = comments.data.reverse().find(comment => {
72+
return (
73+
comment?.user?.login === 'github-actions[bot]' &&
74+
comment?.user?.type === "Bot" &&
75+
comment?.body?.startsWith(`🕒 Workflow "${context.workflow}" took `)
76+
);
5877
})
78+
79+
// If the comment exists then update instead of creating a new one.
80+
const action = existingComment ? "updateComment" : "createComment";
81+
82+
if (existingComment) {
83+
commentData.comment_id = existingComment.id;
84+
}
85+
86+
await github.rest.issues[action](commentData);

0 commit comments

Comments
 (0)