Comment on the pull request #169
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] | |
permissions: | |
# Needed to add and delete comments on a PRs | |
pull-requests: write | |
env: | |
ARTIFACT_NAME: "report" | |
jobs: | |
setup-env: | |
name: Setup environment | |
runs-on: ubuntu-24.04 | |
outputs: | |
IS_PR: ${{ steps.call.outputs.IS_PR || steps.run.outputs.IS_PR }} | |
ORG: ${{ steps.call.outputs.ORG || steps.run.outputs.ORG }} | |
REPO: ${{ steps.call.outputs.REPO || steps.run.outputs.REPO }} | |
RUN_ID: ${{ steps.call.outputs.RUN_ID || steps.run.outputs.RUN_ID }} | |
REPO_FULL_NAME: ${{ steps.call.outputs.REPO_FULL_NAME || steps.run.outputs.REPO_FULL_NAME }} | |
SHA: ${{ steps.call.outputs.SHA || steps.run.outputs.SHA }} | |
steps: | |
- name: Workflow call setup from a Pull Request | |
id: call | |
if: github.event_name == 'pull_request' | |
run: | | |
echo "Setting up from a workflow call" | |
echo "RUN_ID=${{ github.run_id }}" >> $GITHUB_OUTPUT | |
REPO_FULL_NAME="${{ github.repository }}" | |
parts=(${REPO_FULL_NAME//\// }) | |
echo "ORG=${parts[0]}" >> $GITHUB_OUTPUT | |
echo "REPO=${parts[1]}" >> $GITHUB_OUTPUT | |
echo "SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT | |
echo "IS_PR=${{ github.event_name == 'pull_request' }}" >> $GITHUB_OUTPUT | |
- name: Workflow run setup | |
id: run | |
if: github.event_name == 'workflow_run' | |
run: | | |
echo "RUN_ID=${{ github.event.workflow_run.id }}" >> $GITHUB_OUTPUT | |
echo "ORG=${{ github.event.workflow_run.organization.login }}" >> $GITHUB_OUTPUT | |
echo "REPO=${{ github.event.workflow_run.repository.name }}" >> $GITHUB_OUTPUT | |
echo "REPO_FULL_NAME=${{ github.event.workflow_run.repository.full_name }}" >> $GITHUB_OUTPUT | |
echo "SHA=${{ github.event.workflow_run.head_sha }}" >> $GITHUB_OUTPUT | |
echo "IS_PR=${{ github.event.workflow_run.event == 'pull_request' }}" >> $GITHUB_OUTPUT | |
comment-on-prs: | |
name: Comment on PRs | |
runs-on: ubuntu-24.04 | |
needs: setup-env | |
if: ${{ needs.setup-env.outputs.IS_PR }} | |
steps: | |
- name: Download artifacts | |
uses: actions/download-artifact@v4.1.8 | |
with: | |
repository: ${{ needs.setup-env.outputs.REPO_FULL_NAME }} | |
run-id: ${{ needs.setup-env.outputs.RUN_ID }} | |
pattern: "${{ env.ARTIFACT_NAME }}-*" | |
- name: Comment on PR | |
uses: actions/github-script@v7.0.1 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
const artifact = '${{ env.ARTIFACT_NAME }}'; | |
const owner = '${{ needs.setup-env.outputs.ORG }}'; | |
const repo = '${{ needs.setup-env.outputs.REPO }}'; | |
const sha = '${{ needs.setup-env.outputs.SHA }}'; | |
const header = `## Test results for commit ${sha}\n\n`; | |
const comments = fs.readdirSync('.') | |
.filter(d => fs.statSync(d).isDirectory() && d.startsWith(`${artifact}-`)) | |
.map(dir => `${dir}/${artifact}.json`) | |
.filter(fs.existsSync) | |
.flatMap(filePath => JSON.parse(fs.readFileSync(filePath, 'utf8'))); | |
const pull = (await github.rest.pulls.list({ | |
owner: owner, | |
repo: repo, | |
})).data.filter(pr => pr.head.sha == 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: (header + 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]' && | |
!comment.body.startsWith(header), | |
); | |
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); | |
} |