|
| 1 | +name: mypy_primer comment |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: read |
| 5 | + pull-requests: write |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_run: |
| 9 | + workflows: [mypy_primer run] |
| 10 | + types: [completed] |
| 11 | + |
| 12 | +jobs: |
| 13 | + comment: |
| 14 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 15 | + runs-on: ubuntu-latest |
| 16 | + timeout-minutes: 5 |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: download diffs |
| 20 | + |
| 21 | + with: |
| 22 | + script: | |
| 23 | + const fs = require('fs'); |
| 24 | + const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + run_id: ${{ github.event.workflow_run.id }}, |
| 28 | + }); |
| 29 | + const [matchArtifact] = artifacts.data.artifacts.filter((artifact) => { |
| 30 | + return artifact.name == 'primer_diffs' |
| 31 | + }); |
| 32 | +
|
| 33 | + const download = await github.rest.actions.downloadArtifact({ |
| 34 | + owner: context.repo.owner, |
| 35 | + repo: context.repo.repo, |
| 36 | + artifact_id: matchArtifact.id, |
| 37 | + archive_format: 'zip', |
| 38 | + }); |
| 39 | + fs.writeFileSync('diff.zip', Buffer.from(download.data)); |
| 40 | +
|
| 41 | + - run: unzip diff.zip |
| 42 | + |
| 43 | + - name: get pr number |
| 44 | + id: pr-number |
| 45 | + |
| 46 | + with: |
| 47 | + script: | |
| 48 | + const fs = require('fs'); |
| 49 | + return parseInt(fs.readFileSync('pr_number.txt', {encoding: 'utf8'})) |
| 50 | +
|
| 51 | + - name: hide previous comments |
| 52 | + |
| 53 | + with: |
| 54 | + issue-number: ${{ steps.pr-number.outputs.result }} |
| 55 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + |
| 57 | + - run: cat diff_*.txt | tee fulldiff.txt |
| 58 | + |
| 59 | + - name: post comment |
| 60 | + |
| 61 | + with: |
| 62 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + script: | |
| 64 | + const MAX_CHARS = 50000 |
| 65 | + const MAX_CHARS_PER_PROJECT = MAX_CHARS / 3 |
| 66 | +
|
| 67 | + const fs = require('fs') |
| 68 | + let data = fs.readFileSync('fulldiff.txt', {encoding: 'utf8'}) |
| 69 | +
|
| 70 | + function maybeTruncate(original, maxLength) { |
| 71 | + if (original.length <= maxLength) { |
| 72 | + return original |
| 73 | + } |
| 74 | + let truncated = original.substring(0, maxLength) |
| 75 | + // further, remove last line that might be truncated |
| 76 | + truncated = truncated.substring(0, truncated.lastIndexOf('\n')) |
| 77 | + let lines_truncated = original.split('\n').length - truncated.split('\n').length |
| 78 | + return `${truncated}\n\n... (truncated ${lines_truncated} lines) ...` |
| 79 | + } |
| 80 | +
|
| 81 | + const projects = data.split('\n\n') |
| 82 | + // don't let one project dominate |
| 83 | + data = projects.map(project => maybeTruncate(project, MAX_CHARS_PER_PROJECT)).join('\n\n') |
| 84 | + // posting comment fails if too long, so truncate |
| 85 | + data = maybeTruncate(data, MAX_CHARS) |
| 86 | +
|
| 87 | + console.log('mypy_primer diffs:') |
| 88 | + console.log(data) |
| 89 | +
|
| 90 | + let body |
| 91 | + if (data.trim()) { |
| 92 | + body = '[mypy_primer](https://github.com/hauntsaninja/mypy_primer) diffs:\n\n' |
| 93 | + body += '```diff\n' + data + '```' |
| 94 | + const prNumber = parseInt(fs.readFileSync('pr_number.txt', {encoding: 'utf8'})) |
| 95 | + await github.rest.issues.createComment({ |
| 96 | + issue_number: prNumber, |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + body |
| 100 | + }) |
| 101 | + } |
0 commit comments