From c3a600b4c827c4ba1749c4042c2f63da05f9ee5f Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Wed, 19 May 2021 18:53:28 -0700 Subject: [PATCH] Port over latest GA mypy_primer changes (#10505) This is mostly the same as typeshed. The biggest difference is that we don't post the "no effect" message. As a side effect, we also have to run the comment hider first. Co-authored-by: hauntsaninja <> Co-authored-by: Akuli --- .github/workflows/mypy_primer.yml | 45 ++--------- .github/workflows/mypy_primer_comment.yml | 99 +++++++++++++++++++++++ 2 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 .github/workflows/mypy_primer_comment.yml diff --git a/.github/workflows/mypy_primer.yml b/.github/workflows/mypy_primer.yml index ee542c32ccd6..b2166e12330d 100644 --- a/.github/workflows/mypy_primer.yml +++ b/.github/workflows/mypy_primer.yml @@ -57,42 +57,13 @@ jobs: with: name: mypy_primer_diffs path: diff_${{ matrix.shard-index }}.txt - - comment: - name: Comment - runs-on: ubuntu-latest - needs: mypy_primer - permissions: - contents: read - pull-requests: write - steps: - - name: Download diffs - uses: actions/download-artifact@v2 + - if: ${{ matrix.shard-index }} == 0 + name: Save PR number + run: | + echo ${{ github.event.pull_request.number }} | tee pr_number.txt + - if: ${{ matrix.shard-index }} == 0 + name: Upload PR number + uses: actions/upload-artifact@v2 with: name: mypy_primer_diffs - - - name: Post comment - uses: actions/github-script@v3 - with: - github-token: ${{secrets.GITHUB_TOKEN}} - script: | - const fs = require('fs') - const data = ( - ['diff_0.txt', 'diff_1.txt'] - .map(fileName => fs.readFileSync(fileName, { encoding: 'utf8' })) - .join('') - .substr(0, 30000) // About 300 lines - ) - - console.log("Diff from mypy_primer:") - console.log(data) - - if (data.trim()) { - const 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 + '```' - await github.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body - }) - } + path: pr_number.txt diff --git a/.github/workflows/mypy_primer_comment.yml b/.github/workflows/mypy_primer_comment.yml new file mode 100644 index 000000000000..edc91b190e29 --- /dev/null +++ b/.github/workflows/mypy_primer_comment.yml @@ -0,0 +1,99 @@ +name: Comment with mypy_primer diff + +on: + workflow_run: + workflows: + - Run mypy_primer + types: + - completed + +permissions: + contents: read + pull-requests: write + +jobs: + comment: + name: Comment PR from mypy_primer + runs-on: ubuntu-latest + steps: + - name: Download diffs + uses: actions/github-script@v3 + with: + script: | + const fs = require('fs'); + const artifacts = await github.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 == "mypy_primer_diffs"); + + const download = await github.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 + + # Based on https://github.com/kanga333/comment-hider + - name: Hide old comments + uses: actions/github-script@v3 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const fs = require('fs') + + const response = await github.issues.listComments({ + issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }), + owner: context.repo.owner, + repo: context.repo.repo, + }) + const botCommentIds = response.data + .filter(comment => comment.user.login === 'github-actions[bot]') + .map(comment => comment.node_id) + + for (const id of botCommentIds) { + const resp = await github.graphql(` + mutation { + minimizeComment(input: {classifier: OUTDATED, subjectId: "${id}"}) { + minimizedComment { + isMinimized + } + } + } + `) + if (resp.errors) { + throw new Error(resp.errors) + } + } + + - name: Post comment + uses: actions/github-script@v3 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const fs = require('fs') + // Keep in sync with shards produced by mypy_primer workflow + const data = ( + ['diff_0.txt', 'diff_1.txt', 'diff_2.txt'] + .map(fileName => fs.readFileSync(fileName, { encoding: 'utf8' })) + .join('') + .substr(0, 30000) // About 300 lines + ) + + console.log("Diff from mypy_primer:") + console.log(data) + + if (data.trim()) { + const 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 + '```' + await github.issues.createComment({ + issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }), + owner: context.repo.owner, + repo: context.repo.repo, + body + }) + }