Skip to content

Commit 74029f4

Browse files
jorenhamCopilot
andauthored
👷 mypy_primer (#824)
Co-authored-by: Copilot <[email protected]>
1 parent a09cafe commit 74029f4

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: mypy_primer run
2+
permissions: read-all
3+
4+
on:
5+
# only run on pr, since we diff between branches
6+
pull_request:
7+
paths:
8+
- "scipy-stubs/**/*.pyi"
9+
- ".github/workflows/mypy_primer.yml"
10+
- ".github/workflows/mypy_primer_comment.yml"
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
mypy_primer:
18+
runs-on: ubuntu-latest
19+
timeout-minutes: 20
20+
21+
strategy:
22+
fail-fast: false
23+
matrix:
24+
# e.g. change this to `[0, 1, 2]` with `--num-shards=3` below
25+
shard-index: [0]
26+
27+
steps:
28+
- uses: actions/[email protected]
29+
with:
30+
path: new
31+
fetch-depth: 0
32+
33+
- uses: astral-sh/[email protected]
34+
with:
35+
activate-environment: true
36+
python-version: "3.13"
37+
38+
- name: install
39+
run: uv pip install git+https://github.com/hauntsaninja/mypy_primer.git
40+
41+
- name: run
42+
shell: bash
43+
run: |
44+
cd new
45+
MYPY_VERSION=$(grep /mypy- uv.lock | sed -n "s/.*\/mypy-\([^;]*\)-py3.*/\1/p")
46+
47+
echo "new commit"
48+
git checkout $GITHUB_SHA
49+
git rev-list --format=%s --max-count=1 HEAD
50+
51+
MERGE_BASE=$(git merge-base $GITHUB_SHA origin/$GITHUB_BASE_REF)
52+
git worktree add ../old $MERGE_BASE
53+
cd ../old
54+
55+
echo "old commit"
56+
git rev-list --format=%s --max-count=1 HEAD
57+
58+
echo ""
59+
cd ..
60+
61+
# fail action if exit code isn't zero or one
62+
(
63+
mypy_primer \
64+
--new v${MYPY_VERSION} \
65+
--old v${MYPY_VERSION} \
66+
--known-dependency-selector scipy-stubs \
67+
--old-prepend-path old \
68+
--new-prepend-path new \
69+
--num-shards 1 \
70+
--shard-index ${{ matrix.shard-index }} \
71+
--debug \
72+
--output concise \
73+
| tee diff_${{ matrix.shard-index }}.txt
74+
) || [ $? -eq 1 ]
75+
76+
- name: save pr number
77+
if: ${{ matrix.shard-index == 0 }}
78+
run: |
79+
echo ${{ github.event.pull_request.number }} | tee pr_number.txt
80+
81+
- name: upload diff and pr number
82+
if: ${{ matrix.shard-index == 0 }}
83+
uses: actions/[email protected]
84+
with:
85+
name: primer_diffs-${{ matrix.shard-index }}
86+
path: |
87+
diff_${{ matrix.shard-index }}.txt
88+
pr_number.txt
89+
90+
- name: upload diff
91+
if: ${{ matrix.shard-index != 0 }}
92+
uses: actions/[email protected]
93+
with:
94+
name: primer_diffs-${{ matrix.shard-index }}
95+
path: diff_${{ matrix.shard-index }}.txt
96+
97+
join_artifacts:
98+
needs: [mypy_primer]
99+
runs-on: ubuntu-latest
100+
timeout-minutes: 5
101+
102+
steps:
103+
- uses: actions/upload-artifact/[email protected]
104+
with:
105+
name: primer_diffs
106+
pattern: primer_diffs-*
107+
delete-merged: true
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
uses: actions/[email protected]
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+
uses: actions/[email protected]
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+
uses: int128/[email protected]
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+
uses: actions/[email protected]
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

Comments
 (0)