Debian Codegraph @

.github/workflows/mypy_primer_comment.yml

raw
1 name: Comment with mypy_primer diff
2
3 on:
4 workflow_run:
5 workflows:
6 - Run mypy_primer
7 types:
8 - completed
9
10 permissions:
11 contents: read
12 pull-requests: write
13
14 jobs:
15 comment:
16 name: Comment PR from mypy_primer
17 runs-on: ubuntu-latest
18 if: ${{ github.event.workflow_run.conclusion == 'success' }}
19 steps:
20 - name: Download diffs
21 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
22 with:
23 script: |
24 const fs = require('fs');
25 const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
26 owner: context.repo.owner,
27 repo: context.repo.repo,
28 run_id: ${{ github.event.workflow_run.id }},
29 });
30 const [matchArtifact] = artifacts.data.artifacts.filter((artifact) =>
31 artifact.name == "mypy_primer_diffs");
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: get-pr-number
45 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
46 with:
47 script: |
48 const fs = require('fs');
49 return parseInt(fs.readFileSync("pr_number.txt", { encoding: "utf8" }))
50
51 - name: Hide old comments
52 uses: int128/hide-comment-action@3580fff2b9b7c0e16466686530622f0eed93132a # v1.47.0
53 with:
54 token: ${{ secrets.GITHUB_TOKEN }}
55 issue-number: ${{ steps.get-pr-number.outputs.result }}
56
57 - run: cat diff_*.txt | tee fulldiff.txt
58
59 - name: Post comment
60 id: post-comment
61 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
62 with:
63 github-token: ${{ secrets.GITHUB_TOKEN }}
64 script: |
65 const MAX_CHARACTERS = 50000
66 const MAX_CHARACTERS_PER_PROJECT = MAX_CHARACTERS / 3
67
68 const fs = require('fs')
69 let data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' })
70
71 function truncateIfNeeded(original, maxLength) {
72 if (original.length <= maxLength) {
73 return original
74 }
75 let truncated = original.substring(0, maxLength)
76 // further, remove last line that might be truncated
77 truncated = truncated.substring(0, truncated.lastIndexOf('\n'))
78 let lines_truncated = original.split('\n').length - truncated.split('\n').length
79 return `${truncated}\n\n... (truncated ${lines_truncated} lines) ...`
80 }
81
82 const projects = data.split('\n\n')
83 // don't let one project dominate
84 data = projects.map(project => truncateIfNeeded(project, MAX_CHARACTERS_PER_PROJECT)).join('\n\n')
85 // posting comment fails if too long, so truncate
86 data = truncateIfNeeded(data, MAX_CHARACTERS)
87
88 console.log("Diff from mypy_primer:")
89 console.log(data)
90
91 let body
92 if (data.trim()) {
93 body = 'Diff from [mypy_primer](https://github.com/hauntsaninja/mypy_primer), '
94 body += 'showing the effect of this PR on type check results on a corpus of open source code:\n```diff\n'
95 body += data + '```'
96 const prNumber = parseInt(fs.readFileSync("pr_number.txt", { encoding: "utf8" }))
97 await github.rest.issues.createComment({
98 issue_number: prNumber,
99 owner: context.repo.owner,
100 repo: context.repo.repo,
101 body
102 })
103 }