Skip to content

End-to-end PR Comment #3

End-to-end PR Comment

End-to-end PR Comment #3

Workflow file for this run

# Add end to end test summary as a PR comment. This needs to be run in a separate
# workflow to allow permissions to add comments to PRs without elevating permissions
# of the build. This is a modified version of recommendations found here:
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
#
# IMPORTANT: Nothing should be added to this workflow that can potentially run
# untrusted code from unmerged forked repo PRs. The `workflow_run` event will
# only run on our main branch vs other events such as `pull_request_target` that
# runs unmerged PR code with elevated permissions.
name: End-to-end PR Comment
# read-write repo token
# access to secrets
on:
workflow_run:
workflows: ['End-to-end Tests']
types:
- completed
jobs:
upload:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: 'Download artifact'
uses: actions/github-script@v7
with:
script: |
const artifacts = await github.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }},
});
const matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr-comment"
})[0];
const download = await github.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/pr-comment.zip', Buffer.from(download.data));
- run: unzip pr-comment.zip
- name: 'Comment on PR'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const prNumber = Number(fs.readFileSync('./PR-number.txt'));
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
})
// Append a marker to the summary content to identify the comment for later updates
const marker = '<!-- DH GH Action Comment -->';
const prComment = String(fs.readFileSync('./pr-comment.html'));
const contentWithMarker = `${marker}\n${prComment}`;
const existingComment = comments.data.find(comment => comment.body.startsWith(marker));
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: contentWithMarker,
});
}
else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: contentWithMarker,
});
}