Post mypy_primer comment #7
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: Post mypy_primer comment | |
on: | |
workflow_run: | |
workflows: | |
- Run mypy_primer | |
types: | |
- completed | |
permissions: | |
contents: read | |
pull-requests: write | |
jobs: | |
comment: | |
name: Comment PR from mypy_primer | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
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 matchArtifacts = artifacts.data.artifacts.filter((artifact) => | |
artifact.name.startsWith("mypy_primer_diff")); | |
for (const matchArtifact of matchArtifacts) { | |
const download = await github.rest.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: "zip", | |
}); | |
fs.writeFileSync(`${matchArtifact.name}.zip`, Buffer.from(download.data)); | |
} | |
- run: for file in *.zip; do unzip $file; done | |
- run: | | |
cat diff_*.txt | tee fulldiff.txt | |
- name: Post comment | |
id: post-comment | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const fs = require('fs') | |
let data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' }) | |
// Maximum comment length is 65536 characters. We need much less than 236 for extra text. | |
const MAX_LENGTH = 65300 | |
if (data.length > MAX_LENGTH) { | |
let truncated_data = data.substring(0, MAX_LENGTH) | |
let lines_truncated = data.split('\n').length - truncated_data.split('\n').length | |
data = truncated_data + `\n\n... (truncated ${lines_truncated} lines) ...\n` | |
} | |
let body | |
if (data.trim()) { | |
body = 'Diff from [mypy_primer](https://github.com/hauntsaninja/mypy_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```' | |
} else { | |
body = 'According to [mypy_primer](https://github.com/hauntsaninja/mypy_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 | |
# v0.4.0 | |
uses: kanga333/comment-hider@c12bb20b48aeb8fc098e35967de8d4f8018fffdf | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
leave_visible: 1 | |
issue_number: ${{ steps.post-comment.outputs.result }} |