Skip to content

Commit

Permalink
Use nodejs like a pro.
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban committed Nov 5, 2022
1 parent 04d8448 commit a86b23b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
43 changes: 35 additions & 8 deletions .github/scripts/pr_comment.js
Expand Up @@ -7,7 +7,8 @@ Update the content of the existing comment.
https://octokit.github.io/rest.js/v19
*/
module.exports = async ({octokit_rest, context, process}) => {
module.exports = async ({github, context, process}) => {
var octokit_rest = github
if (context.eventName != "pull_request") {
// Only PR are supported.
return
Expand All @@ -24,23 +25,49 @@ module.exports = async ({octokit_rest, context, process}) => {
*/
var doAction = async function() {

console.log(context)

fs = require('fs');
var body = fs.readFile(
process.env.GITHUB_WORKSPACE + "/" + process.env.COMMENT_BODY)

var comments = await octokit.rest.issues.listComments({
const body = fs.readFileSync(
process.env.GITHUB_WORKSPACE + "/" + process.env.COMMENT_BODY, 'utf8');
var comment_id = null
var comment_marker = '\n' + process.env.COMMENT_MARKER
var comment_body = body + comment_marker

var comments = await octokit_rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.event.number,
issue_number: context.payload.number,
})

console.log(comments)

comments.data.forEach(comment => {
if (comment.body.endsWith(comment_marker)) {
comment_id = comment.id
}
})

if (comment_id) {
// We have an existing comment.
// update the content.
await octokit_rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment_id,
body: comment_body,
})
return
}

// Create a new comment.
await octokit_rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.event.number,
body: body,
})
issue_number: context.payload.number,
body: comment_body,
})

}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -291,7 +291,7 @@ jobs:
# for sending a comment to a PR.
- uses: actions/github-script@v3
env:
COMMENT_MARKER: coverage-report
COMMENT_MARKER: "<!--- automatic-coverage-report -->"
COMMENT_BODY: coverage-report.txt
with:
script: |
Expand Down

0 comments on commit a86b23b

Please sign in to comment.