Comment on PR with Test Results #314
This file contains hidden or 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
| # SPDX-FileCopyrightText: Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved. | |
| # SPDX-License-Identifier: Apache-2.0 | |
| # Adapted from https://stackoverflow.com/a/71683208 | |
| # Commenting on a PR is a separate workflow from the others so that PRs from | |
| # forks can receive comments as well. Since this workflow is not triggered by | |
| # a pull request event, but instead by the completion of another workflow, we | |
| # have write permissions to the repository and can therefore comment on the PR. | |
| name: Comment on PR with Test Results | |
| on: | |
| workflow_run: | |
| workflows: [Small Benchmark/Test Suite, Test Example Applications] | |
| types: | |
| - completed | |
| jobs: | |
| download: | |
| runs-on: ubuntu-latest | |
| # Only run if the workflow completed (not cancelled) | |
| if: ${{ github.event.workflow_run.conclusion == 'success' || github.event.workflow_run.conclusion == 'failure' }} | |
| steps: | |
| - name: Download artifact | |
| id: download | |
| continue-on-error: true | |
| uses: actions/download-artifact@v5 | |
| with: | |
| github-token: ${{ github.token }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| merge-multiple: true | |
| - name: Comment results on PR | |
| if: steps.download.outcome == 'success' | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ github.token }} | |
| script: | | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const downloadPath = '${{ steps.download.outputs.download-path }}' || process.env.GITHUB_WORKSPACE; | |
| // Check if pr_number file exists | |
| const prNumberPath = path.join(downloadPath, 'pr_number'); | |
| if (!fs.existsSync(prNumberPath)) { | |
| console.log('No pr_number file found. Skipping comment.'); | |
| return; | |
| } | |
| const issue_number = Number(fs.readFileSync(prNumberPath, 'utf8').trim()); | |
| // Skip if not triggered by a PR (pr_number will be empty or invalid) | |
| if (!issue_number || isNaN(issue_number)) { | |
| console.log('Not triggered by a PR. Skipping comment.'); | |
| return; | |
| } | |
| const commitSha = fs.readFileSync(path.join(downloadPath, 'commit_sha'), 'utf8').trim(); | |
| const date = fs.readFileSync(path.join(downloadPath, 'date'), 'utf8').trim(); | |
| const server = '${{ github.server_url }}'; | |
| const repo = '${{ github.repository }}'; | |
| let prettyContent = ''; | |
| try { | |
| prettyContent = fs.readFileSync(path.join(downloadPath, 'readme.md'), 'utf8'); | |
| } catch (error) { | |
| prettyContent = 'Pretty output not available.'; | |
| } | |
| // Read the trends.md content | |
| let trendsContent = ''; | |
| try { | |
| trendsContent = fs.readFileSync(path.join(downloadPath, 'trends.md'), 'utf8'); | |
| } catch (error) { | |
| trendsContent = 'Trends not available.'; | |
| } | |
| const body = `<details> | |
| <summary>📊 Test Results for ${context.payload.workflow_run.name}</summary> | |
| [${commitSha}](${server}/${repo}/commit/${commitSha}) (${date}) | |
| ${prettyContent} | |
| </details> | |
| <details> | |
| <summary>📈 Trends (vs main branch) for ${context.payload.workflow_run.name}</summary> | |
| [${commitSha}](${server}/${repo}/commit/${commitSha}) (${date}) | |
| ${trendsContent} | |
| </details>`; | |
| github.rest.issues.createComment({ | |
| issue_number: issue_number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: body | |
| }); |