Skip to content

Create Comment

Create Comment #6260

# This exists as a seperate workflow because pull_request workflows dont have permission to write comments
# How to use:
# 1. Add the name of the workflow that needs to leave a comment under on -> workflow_run -> workflows
# 2. In the main workflow always upload an artifact folder named comment_info
# 3. If a comment should be written then:
# 1. include issue_number.txt in the artifact folder containing the issue_number
# 2. include message.txt in the artifact folder containing the message of the comment
name: Create Comment
on:
workflow_run:
workflows: [ "Benchmarks" ]
# Cancel already running jobs
concurrency:
group: create_comment_${{ github.head_ref }}
cancel-in-progress: true
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
jobs:
upload:
runs-on: ubuntu-latest
if: >
${{ github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' }}
steps:
- name: 'Download results'
uses: actions/[email protected]
with:
# This script is copy pasted from https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
# Waiting on this issue to be able to just use download-artifacts https://github.com/actions/download-artifact/issues/60
script: |
var artifacts = await github.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 == "comment_info"
})[0];
if (!matchArtifact) {
throw 'comment_info artifact was not produced by the previous workflow';
}
var download = await github.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_info.zip', Buffer.from(download.data));
- run: unzip comment_info.zip
- name: 'Comment on PR'
uses: actions/github-script@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
var fs = require('fs');
if (fs.existsSync('./issue_number.txt')) {
var issue_number = Number(fs.readFileSync('./issue_number.txt'));
var message = fs.readFileSync('./message.txt', 'utf8');
await github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
body: message,
});
}