From 5b19ea21254994ca64bb5f17b76e2bdcf8a12a10 Mon Sep 17 00:00:00 2001 From: hanbings Date: Sun, 23 Jun 2024 18:29:26 -0400 Subject: [PATCH] Fix CI code for `comment.yml` (#404) --- .github/workflows/comment.yml | 55 +++++++++++++++++------------------ .github/workflows/compat.yml | 55 +++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 30 deletions(-) diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml index de319e4..e0c20c8 100644 --- a/.github/workflows/comment.yml +++ b/.github/workflows/comment.yml @@ -1,15 +1,17 @@ on: - workflow_run: - workflows: [External testsuites] - types: [completed] + workflow_run: + workflows: [External-testsuites] + types: [completed] name: Comment Test results on the PR +permissions: {} jobs: upload-pr-comment: + if: ${{ github.event.workflow_run.event == 'pull_request' }} + name: Upload PR comment runs-on: ubuntu-latest - if: github.event_name == 'pull_request' permissions: actions: read pull-requests: write @@ -19,41 +21,37 @@ jobs: uses: actions/github-script@v7 with: script: | - let runs = await github.rest.checks.listForRef({ + let artifacts = await github.rest.actions.listWorkflowRunArtifacts({ owner: context.repo.owner, repo: context.repo.repo, - ref: '${{ github.event.pull_request.head.sha }}' + run_id: ${{ github.event.workflow_run.id }}, }); - let names = ['Run GNU findutils tests', 'Run BFS tests']; - let results = []; - runs.data.check_runs.filter(check => names.includes(check.name)).forEach(run => results.push(run)); + // List all artifacts + let matchArtifact = artifacts.data.artifacts.filter((artifact) => { + return artifact.name == "comment" + })[0]; - let annotations = { data: []}; - for (let result of results) { - let run = await github.rest.checks.listAnnotations({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: result.id - }); - - run.data.forEach(data => { - annotations.data.push({ - run: result.name, - annotation: data - }); - }); - } + // Download the artifact to github.workspace + let download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); let fs = require('fs'); - fs.writeFileSync('${{ github.workspace }}/annotations.json', JSON.stringify(annotations)); + fs.writeFileSync('${{ github.workspace }}/comment.zip', Buffer.from(download.data)); + + - run: unzip comment.zip + - name: Comment on PR uses: actions/github-script@v7 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | let fs = require('fs'); - let annotations = JSON.parse(fs.readFileSync('${{ github.workspace }}/annotations.json', 'utf8')); + let annotations = JSON.parse(fs.readFileSync('./annotations.json', 'utf8')); let annotationContent = annotations .data @@ -62,9 +60,10 @@ jobs: console.log(annotationContent); + // Comment on the PR github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: ${{ github.event.number }}, - body: 'GNU testsuite comparison:\n```\n' + annotationContent + '```' + issue_number: annotations.pull_request_number, + body: 'GNU testsuite comparison:\n```\n' + annotationContent + '\n```\n' }); \ No newline at end of file diff --git a/.github/workflows/compat.yml b/.github/workflows/compat.yml index cddef70..2eb035f 100644 --- a/.github/workflows/compat.yml +++ b/.github/workflows/compat.yml @@ -1,6 +1,6 @@ on: [push, pull_request] -name: External testsuites +name: External-testsuites jobs: gnu-tests: @@ -146,4 +146,55 @@ jobs: shell: bash run: | mv dl/bfs-result.json latest-bfs-result.json - python findutils/util/compare_bfs_result.py \ No newline at end of file + python findutils/util/compare_bfs_result.py + + upload-annotations: + name: Upload annotations + runs-on: ubuntu-latest + needs: [gnu-tests, bfs-tests] + if: ${{ github.event_name == 'pull_request' }} + + steps: + - name: List Annotations + uses: actions/github-script@v7 + + with: + script: | + let runs = await github.rest.checks.listForRef({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: '${{ github.event.pull_request.head.sha }}' + }); + + let names = ['Run GNU findutils tests', 'Run BFS tests']; + let results = []; + runs.data.check_runs.filter(check => names.includes(check.name)).forEach(run => results.push(run)); + + let annotations = { data: [], pull_request_number: '${{ github.event.number }}' }; + for (let result of results) { + let run = await github.rest.checks.listAnnotations({ + owner: context.repo.owner, + repo: context.repo.repo, + check_run_id: result.id + }); + + run.data.forEach(data => { + annotations.data.push({ + run: result.name, + annotation: data + }); + }); + } + + // Remove duplicate items. + annotations.data = annotations.data.filter((value, index, self) => + self.findIndex(v => v.annotation.message === value.annotation.message) === index); + + let fs = require('fs'); + fs.writeFileSync('${{ github.workspace }}/annotations.json', JSON.stringify(annotations)); + + - name: Upload annotations + uses: actions/upload-artifact@v4 + with: + name: comment + path: annotations.json \ No newline at end of file