Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update existing comments if they exist #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ As the **very last job** in your workflow, add
- name: Time reporter
uses: DeviesDevelopment/workflow-timer@v0.0.2
```

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:

```
```yaml
on:
push:
branches: master
Expand All @@ -27,4 +28,5 @@ on:
```

## How to contribute

Feel free to open a pull request! All contributions, no matter how small, are more than welcome. Happy hacking!
41 changes: 35 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ runs:
if (context.eventName != 'pull_request') {
return
}

const currentTime = new Date().getTime();
const currentRun = await github.rest.actions.getWorkflowRun({
owner: context.repo.owner,
Expand All @@ -23,7 +24,7 @@ runs:
});
const startedAt = currentRun.data.run_started_at
const currentRunDurationInMillis = currentTime - new Date(startedAt).getTime();

const workflowId = currentRun.data.workflow_id
const historical_runs = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
Expand All @@ -37,7 +38,7 @@ runs:
x.conclusion == 'success'
);

var outputMessage = ''
let outputMessage = ''
if (latestRunsOnMaster.length === 0) {
outputMessage = "No data for historical runs on master/main branch found. Can't compare."
} else {
Expand All @@ -46,13 +47,41 @@ runs:
const diffInSeconds = (currentRunDurationInMillis - latestMasterRunDurationInMillis) / 1000
const percentageDiff = ((1 - (currentRunDurationInMillis/latestMasterRunDurationInMillis)) * 100).toFixed(2)
const outcome = (diffInSeconds > 0) ? "an increase" : "a decrease"

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.'
}

github.rest.issues.createComment({
issue_number: context.issue.number,
const commentData = {
owner: context.repo.owner,
repo: context.repo.repo,
body: outputMessage
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.reverse().find(comment => {
return (
comment?.user?.login === 'github-actions[bot]' &&
comment?.user?.type === "Bot" &&
comment?.body?.startsWith('🕒 Workflow \"')
);
})

// If the comment exists then update instead of creating a new one.
const action = existingComment ? "updateComment" : "createComment";

if (existingComment) {
commentData.comment_id = existingComment.id;
}

await github.rest.issues[action](commentData);