Skip to content

Comment on the pull request #9

Comment on the pull request

Comment on the pull request #9

name: Comment on the pull request
on:
# Trigger this workflow after the Health workflow completes. This workflow will have permissions to
# do things like create comments on the PR, even if the original workflow couldn't.
workflow_call:
workflow_run:
workflows:
- Publish
- Health
types:
- completed
jobs:
upload:
permissions:
pull-requests: write
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
continue-on-error: true
steps:
# Download the output of the health workflow, consisting of the comment markdown and either
# the issue number or an existing comment ID.
- name: 'Download artifact'
uses: actions/github-script@6f00a0b667f9463337970371ccda9072ee86fb27
with:
script: |
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "output"
})[0];
var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{ github.workspace }}/comment.zip', Buffer.from(download.data));
- run: unzip comment.zip
# Create the comment, or update the existing one, with the markdown
# generated in the Health workflow.
- name: 'Comment on PR'
uses: actions/github-script@6f00a0b667f9463337970371ccda9072ee86fb27
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
var fs = require('fs');
if (fs.existsSync('./comment.md')) {
var markdown = fs.readFileSync('./comment.md', 'utf8');
if (fs.existsSync('./commentId')) {
var comment_number = Number(fs.readFileSync('./commentId', 'utf8'));
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment_number,
body: markdown
});
} else {
var issue_number = Number(fs.readFileSync('./issueNumber', 'utf8'));
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
body: markdown
});
}
} else if (fs.existsSync('./commentId')) {
var comment_number = Number(fs.readFileSync('./commentId', 'utf8'));
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment_number,
});
}