Comment with primer diff #545
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
# Most of this is inspired by the mypy primer | |
name: Comment with primer diff | |
on: | |
workflow_run: | |
workflows: [Primer] | |
types: | |
- completed | |
permissions: | |
contents: read | |
pull-requests: write | |
jobs: | |
primer-comment: | |
name: Run | |
runs-on: ubuntu-latest | |
steps: | |
- name: Download diffs | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require('fs'); | |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{ github.event.workflow_run.id }}, | |
}); | |
const [matchArtifact] = artifacts.data.artifacts.filter((artifact) => | |
artifact.name == "primer_diffs"); | |
const download = await github.rest.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: "zip", | |
}); | |
fs.writeFileSync("diff.zip", Buffer.from(download.data)); | |
- run: unzip diff.zip | |
- name: Post comment | |
id: post-comment | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs') | |
const data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' }) | |
console.log("Diff from primer:") | |
console.log(data) | |
let body | |
if (data.trim()) { | |
body = 'Diff from the primer, showing the effect of this PR on open source code:\n' + data | |
} else { | |
body = 'According to the primer, this change has no effect on the checked open source code. 🤖🎉' | |
} | |
const prNumber = parseInt(fs.readFileSync("pr_number.txt", { encoding: "utf8" })) | |
await github.rest.issues.createComment({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body | |
}) | |
return prNumber | |
- name: Hide old comments | |
# Taken from mypy primer | |
# v0.3.0 | |
uses: kanga333/comment-hider@c12bb20b48aeb8fc098e35967de8d4f8018fffdf | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
leave_visible: 1 | |
issue_number: ${{ steps.post-comment.outputs.result }} |