Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifies the download of report to use job syntax #568

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 51 additions & 34 deletions .github/workflows/comment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,76 @@ on:
workflows: ["Workflow Unit tests"]
types: [completed]

permissions:
# Needed to add and delete comments on a PRs
pull-requests: write

env:
ARTIFACT_NAME: "report"

jobs:
filter-artifacts:
name: Filter Artifacts
setup-env:
name: Setup environment
runs-on: ubuntu-24.04
if: github.event.workflow_run.event == 'pull_request'
outputs:
artifacts: ${{ fromJson(steps.filter-artifacts.outputs.result) }}
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:
- uses: actions/checkout@v4.2.2
- id: filter-artifacts
name: Filter test report artifacts
uses: actions/github-script@v7.0.1
with:
script: |
const fs = require('fs');
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.payload.workflow_run.repository.owner.login,
repo: context.payload.workflow_run.repository.name,
run_id: ${{ github.event.workflow_run.id }},
});
const zips = artifacts.data.artifacts.flatMap(artifact =>
artifact.name.startsWith('${{ env.ARTIFACT_NAME }}-')? [artifact.name] : []
);
return JSON.stringify(zips);
- 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: filter-artifacts
if: github.event.workflow_run.event == 'pull_request' && needs.filter-artifacts.outputs.artifacts != '[]'
strategy:
matrix:
artifact: ${{fromJson(needs.filter-artifacts.outputs.artifacts)}}
Comment on lines -42 to -46
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat! we don't need this!!

needs: setup-env
if: ${{ needs.setup-env.outputs.IS_PR }}
steps:
- name: Download artifact
- name: Download artifacts
uses: actions/download-artifact@v4.1.8
with:
repository: ${{ github.event.workflow_run.repository.name }}
run-id: ${{ github.event.workflow_run.id }}
name: ${{ matrix.artifact }}
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 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 sha = context.payload.workflow_run.head_sha
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}-`))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find any directory starting with report-

.map(dir => `${dir}/${artifact}.json`)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

within that directory find a report.json

.filter(fs.existsSync)
.flatMap(filePath => JSON.parse(fs.readFileSync(filePath, 'utf8')));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the report contains a list of strings, so we need to use .flatMap to flatten the contents into a string[] rather than a string[][]


const pull = (await github.rest.pulls.list({
owner: owner,
Expand All @@ -74,7 +91,7 @@ jobs:
owner: owner,
repo: repo,
issue_number: issue_number,
header + body,
body: (header + body),
});
}

Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/workflow_simple_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ jobs:
[ '${{ needs.simple.result }}' = 'success' ] || (echo simple failed && false)
[ '${{ needs.simple-uv.result }}' = 'success' ] || (echo simple-uv failed && false)
[ '${{ needs.simple-self-hosted.result }}' = 'success' ] || (echo simple-self-hosted failed && false)
test-comment:
name: Comment on the pull request
uses: ./.github/workflows/comment.yaml
if: always() && !cancelled()
needs:
- simple
- simple-uv
- simple-self-hosted
Loading