From 3c93129b89d9c7defdc68b2363f04b19909e83b4 Mon Sep 17 00:00:00 2001 From: tristanlatr Date: Fri, 3 Nov 2023 12:25:46 -0400 Subject: [PATCH] Put everything in the same file --- .github/workflows/pydoctor_primer.yaml | 85 ++++++++++++++++ .../workflows/pydoctor_primer_comment.yaml | 97 ------------------- 2 files changed, 85 insertions(+), 97 deletions(-) delete mode 100644 .github/workflows/pydoctor_primer_comment.yaml diff --git a/.github/workflows/pydoctor_primer.yaml b/.github/workflows/pydoctor_primer.yaml index 0e255968e..53e9bdcf0 100644 --- a/.github/workflows/pydoctor_primer.yaml +++ b/.github/workflows/pydoctor_primer.yaml @@ -81,3 +81,88 @@ jobs: with: name: pydoctor_primer_diffs path: pr_number.txt + + comment: + needs: [pydoctor_primer] + name: Comment PR from pydoctor_primer + runs-on: ubuntu-latest + + steps: + - name: Download diffs + uses: actions/github-script@v6 + 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 [matchArtifact] = artifacts.data.artifacts.filter((artifact) => + artifact.name == "pydoctor_primer_diffs"); + + const download = await github.rest.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 + - run: | + cat diff_*.txt | tee fulldiff.txt + + - name: Post comment + id: post-comment + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const MAX_CHARACTERS = 30000 + const MAX_CHARACTERS_PER_PROJECT = MAX_CHARACTERS / 3 + + const fs = require('fs') + let data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' }) + + function truncateIfNeeded(original, maxLength) { + if (original.length <= maxLength) { + return original + } + let truncated = original.substring(0, maxLength) + // further, remove last line that might be truncated + truncated = truncated.substring(0, truncated.lastIndexOf('\n')) + let lines_truncated = original.split('\n').length - truncated.split('\n').length + return `${truncated}\n\n... (truncated ${lines_truncated} lines) ...` + } + + const projects = data.split('\n\n') + // don't let one project dominate + data = projects.map(project => truncateIfNeeded(project, MAX_CHARACTERS_PER_PROJECT)).join('\n\n') + // posting comment fails if too long, so truncate + data = truncateIfNeeded(data, MAX_CHARACTERS) + + console.log("Diff from pydoctor_primer:") + console.log(data) + + let body + if (data.trim()) { + body = 'Diff from [pydoctor_primer](https://github.com/tristanlatr/pydoctor_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```' + } else { + body = "According to [pydoctor_primer](https://github.com/tristanlatr/pydoctor_primer), this change doesn't affect type check results on a corpus of 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 + uses: kanga333/comment-hider@v0.4.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + leave_visible: 1 + issue_number: ${{ steps.post-comment.outputs.result }} \ No newline at end of file diff --git a/.github/workflows/pydoctor_primer_comment.yaml b/.github/workflows/pydoctor_primer_comment.yaml deleted file mode 100644 index 1b2d3f1dc..000000000 --- a/.github/workflows/pydoctor_primer_comment.yaml +++ /dev/null @@ -1,97 +0,0 @@ -name: Comment with pydoctor_primer diff - -on: - workflow_run: - workflows: - - Run pydoctor_primer - types: - - completed - -permissions: - contents: read - pull-requests: write - -jobs: - comment: - name: Comment PR from pydoctor_primer - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'success' }} - steps: - - name: Download diffs - uses: actions/github-script@v6 - 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 [matchArtifact] = artifacts.data.artifacts.filter((artifact) => - artifact.name == "pydoctor_primer_diffs"); - - const download = await github.rest.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 - - run: | - cat diff_*.txt | tee fulldiff.txt - - - name: Post comment - id: post-comment - uses: actions/github-script@v6 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const MAX_CHARACTERS = 30000 - const MAX_CHARACTERS_PER_PROJECT = MAX_CHARACTERS / 3 - - const fs = require('fs') - let data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' }) - - function truncateIfNeeded(original, maxLength) { - if (original.length <= maxLength) { - return original - } - let truncated = original.substring(0, maxLength) - // further, remove last line that might be truncated - truncated = truncated.substring(0, truncated.lastIndexOf('\n')) - let lines_truncated = original.split('\n').length - truncated.split('\n').length - return `${truncated}\n\n... (truncated ${lines_truncated} lines) ...` - } - - const projects = data.split('\n\n') - // don't let one project dominate - data = projects.map(project => truncateIfNeeded(project, MAX_CHARACTERS_PER_PROJECT)).join('\n\n') - // posting comment fails if too long, so truncate - data = truncateIfNeeded(data, MAX_CHARACTERS) - - console.log("Diff from pydoctor_primer:") - console.log(data) - - let body - if (data.trim()) { - body = 'Diff from [pydoctor_primer](https://github.com/tristanlatr/pydoctor_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```' - } else { - body = "According to [pydoctor_primer](https://github.com/tristanlatr/pydoctor_primer), this change doesn't affect type check results on a corpus of 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 - uses: kanga333/comment-hider@v0.4.0 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - leave_visible: 1 - issue_number: ${{ steps.post-comment.outputs.result }}