Skip to content

Instantly share code, notes, and snippets.

@t04glovern
Last active October 20, 2022 03:06
Show Gist options
  • Save t04glovern/3d7a7ff67bf4d38fa51c22bb05065e05 to your computer and use it in GitHub Desktop.
Save t04glovern/3d7a7ff67bf4d38fa51c22bb05065e05 to your computer and use it in GitHub Desktop.
GitHub Bot User - Comment/Update Comment on PR
name: Workflow
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
ci:
runs-on: ubuntu-22.04
steps:
- name: Update Comment from Bot
uses: actions/github-script@v6
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Do The Thing')
})
// 2. Prepare format of the comment
const output = `## Do The Thing
* COOL THING 1!
* COOL THING 2`
// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment