Skip to content

Commit a86b23b

Browse files
committed
Use nodejs like a pro.
1 parent 04d8448 commit a86b23b

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

.github/scripts/pr_comment.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Update the content of the existing comment.
77
88
https://octokit.github.io/rest.js/v19
99
*/
10-
module.exports = async ({octokit_rest, context, process}) => {
10+
module.exports = async ({github, context, process}) => {
11+
var octokit_rest = github
1112
if (context.eventName != "pull_request") {
1213
// Only PR are supported.
1314
return
@@ -24,23 +25,49 @@ module.exports = async ({octokit_rest, context, process}) => {
2425
*/
2526
var doAction = async function() {
2627

28+
console.log(context)
29+
2730
fs = require('fs');
28-
var body = fs.readFile(
29-
process.env.GITHUB_WORKSPACE + "/" + process.env.COMMENT_BODY)
3031

31-
var comments = await octokit.rest.issues.listComments({
32+
const body = fs.readFileSync(
33+
process.env.GITHUB_WORKSPACE + "/" + process.env.COMMENT_BODY, 'utf8');
34+
var comment_id = null
35+
var comment_marker = '\n' + process.env.COMMENT_MARKER
36+
var comment_body = body + comment_marker
37+
38+
var comments = await octokit_rest.issues.listComments({
3239
owner: context.repo.owner,
3340
repo: context.repo.repo,
34-
issue_number: context.event.number,
41+
issue_number: context.payload.number,
3542
})
43+
3644
console.log(comments)
3745

46+
comments.data.forEach(comment => {
47+
if (comment.body.endsWith(comment_marker)) {
48+
comment_id = comment.id
49+
}
50+
})
51+
52+
if (comment_id) {
53+
// We have an existing comment.
54+
// update the content.
55+
await octokit_rest.issues.updateComment({
56+
owner: context.repo.owner,
57+
repo: context.repo.repo,
58+
comment_id: comment_id,
59+
body: comment_body,
60+
})
61+
return
62+
}
63+
64+
// Create a new comment.
3865
await octokit_rest.issues.createComment({
3966
owner: context.repo.owner,
4067
repo: context.repo.repo,
41-
issue_number: context.event.number,
42-
body: body,
43-
})
68+
issue_number: context.payload.number,
69+
body: comment_body,
70+
})
4471

4572
}
4673

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ jobs:
291291
# for sending a comment to a PR.
292292
- uses: actions/github-script@v3
293293
env:
294-
COMMENT_MARKER: coverage-report
294+
COMMENT_MARKER: "<!--- automatic-coverage-report -->"
295295
COMMENT_BODY: coverage-report.txt
296296
with:
297297
script: |

0 commit comments

Comments
 (0)