Add Benchmark Results to PR #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Add Benchmark Results to PR | |
on: | |
workflow_run: | |
workflows: [ Run and Cache Benchmarks ] | |
types: [ completed ] | |
permissions: | |
pull-requests: write | |
jobs: | |
comment: | |
if: github.event.workflow_run.conclusion == 'success' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Download Benchmark Results | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
async function downloadArtifact(artifactName) { | |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: context.payload.workflow_run.id, | |
}); | |
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == artifactName | |
})[0]; | |
if (!matchArtifact) { | |
core.setFailed(`Failed to find artifact: ${artifactName}`); | |
} | |
let download = await github.rest.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
let fs = require('fs'); | |
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/${artifactName}.zip`, Buffer.from(download.data)); | |
} | |
await downloadArtifact("benchmark_results_target"); | |
await downloadArtifact("benchmark_results_pr"); | |
await downloadArtifact("event.json"); | |
- name: Unzip Benchmark Results | |
run: | | |
unzip benchmark_results_target.zip | |
unzip benchmark_results_pr.zip | |
unzip event.json.zip | |
- name: Export PR Event Data | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
let fs = require('fs'); | |
let prEvent = JSON.parse(fs.readFileSync(process.env.PR_EVENT, {encoding: 'utf8'})); | |
core.exportVariable("PR_NUMBER", prEvent.number); | |
core.exportVariable("HEAD_SHA", prEvent.pull_request.head.sha); | |
core.exportVariable("BASE_SHA", prEvent.pull_request.base.sha); | |
- name: Find Comment | |
uses: peter-evans/find-comment@v3 | |
with: | |
issue-number: ${{ env.PR_NUMBER }} | |
comment-author: 'github-actions[bot]' | |
body-includes: Benchmark Results - Solvers | |
- name: Create or update comment | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require('fs'); | |
const issue_number = ${{ env.PR_NUMBER }}; | |
const comment_id = '${{ steps.fc.outputs.comment-id }}'; | |
const head_sha = ${{ env.HEAD_SHA }}; | |
const base_sha = ${{ env.BASE_SHA }}; | |
let results_main = 'Benchmarks did not run successfully on main.'; | |
let results_pr = 'Benchmarks did not run successfully on PR.'; | |
if (fs.existsSync('results_main.txt')) { | |
results_main = fs.readFileSync('results_target.txt', 'utf8'); | |
} | |
if (fs.existsSync('results_pr.txt')) { | |
results_pr = fs.readFileSync('results_pr.txt', 'utf8'); | |
} | |
const body = `Benchmark Results - Solvers\n\n<details><summary>Target Branch - ${base_sha}</summary>\n\n\`\`\`\n${results_main}\n\`\`\`\n</details>\n<details><summary>PR - ${head_sha}</summary>\n\n\`\`\`\n${results_pr}\n\`\`\`\n</details>`; | |
if (comment_id) { | |
github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: comment_id, | |
body: body, | |
}); | |
} else { | |
github.rest.issues.createComment({ | |
issue_number: issue_number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body, | |
}); | |
} |