Comment on the pull request #92
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2025 Canonical Ltd. | |
# See LICENSE file for licensing details. | |
name: Comment on the pull request | |
on: | |
workflow_call: | |
workflow_run: | |
workflows: ["Workflow Unit tests"] | |
types: [completed] | |
env: | |
ARTIFACT_NAME: "report" | |
jobs: | |
download-artifacts: | |
name: Download Artifacts | |
runs-on: ubuntu-24.04 | |
if: github.event.workflow_run.event == 'pull_request' | |
outputs: | |
artifacts: ${{ fromJson(steps.download-artifact.outputs.result) }} | |
steps: | |
- uses: actions/checkout@v4.2.2 | |
- id: download-artifact | |
name: Download test report artifact | |
uses: actions/github-script@v7.0.1 | |
with: | |
script: | | |
const fs = require('fs'); | |
const owner = context.payload.workflow_run.repository.owner.login; | |
const repo = context.payload.workflow_run.repository.name; | |
const run_id = ${{ github.event.workflow_run.id }}; | |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: owner, | |
repo: repo, | |
run_id: run_id, | |
}); | |
const report_artifacts = artifacts.data.artifacts.filter(artifact => | |
artifact.name.startsWith('${{ env.ARTIFACT_NAME }}-') | |
) | |
const zips = await Promise.all( | |
report_artifacts.map(async artifact => { | |
const download = await github.rest.actions.downloadArtifact({ | |
owner: owner, | |
repo: repo, | |
artifact_id: artifact.id, | |
archive_format: 'zip', | |
}); | |
const zipPath = `${artifact}.zip`; | |
fs.writeFileSync(zipPath, Buffer.from(download.data)); | |
return zipPath; | |
}) | |
); | |
return JSON.stringify(zips); | |
comment-on-prs: | |
name: Comment on PRs | |
runs-on: ubuntu-24.04 | |
needs: download-artifacts | |
if: github.event.workflow_run.event == 'pull_request' | |
strategy: | |
matrix: | |
artifact: ${{fromJson(needs.download-artifacts.outputs.artifacts)}} | |
steps: | |
- name: Unzip | |
shell: bash | |
run: unzip ${{ matrix.artifact }} | |
- name: Comment on PR | |
uses: actions/github-script@v7.0.1 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
const comments = JSON.parse(fs.readFileSync('${{ env.ARTIFACT_NAME }}.json')); | |
const owner = context.payload.workflow_run.repository.owner.login; | |
const repo = context.payload.workflow_run.repository.name; | |
const pull = (await github.rest.pulls.list({ | |
owner: owner, | |
repo: repo, | |
})).data.filter(pr => pr.head.sha == context.payload.workflow_run.head_sha)[0]; | |
const issue_number = pull.number; | |
const createComment = async (body) => { | |
await github.rest.issues.createComment({ | |
owner: owner, | |
repo: repo, | |
issue_number: issue_number, | |
body, | |
}); | |
} | |
const deleteGithubActionsComments = async () => { | |
const existingComments = await github.rest.issues.listComments({ | |
owner: owner, | |
repo: repo, | |
issue_number: issue_number, | |
}); | |
const githubActionsComments = existingComments.data.filter( | |
comment => comment.user.login == 'github-actions[bot]' | |
); | |
for (const comment of githubActionsComments) { | |
await github.rest.issues.deleteComment({ | |
owner: owner, | |
repo: repo, | |
comment_id: comment.id, | |
}); | |
} | |
} | |
await deleteGithubActionsComments(); | |
for (const comment of comments) { | |
await createComment(comment); | |
} |