Skip to content

Publish Test Results #53

Publish Test Results

Publish Test Results #53

name: Publish Test Results
on:
workflow_run:
workflows: ["tests 1", "tests 2", "tests others"]
types:
- completed
jobs:
merge-reports:
permissions:
pull-requests: write
checks: write
id-token: write # This is required for OIDC login (azure/login) to succeed
contents: read # This is required for actions/checkout to succeed
if: ${{ github.event.workflow_run.event == 'pull_request' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm ci
env:
DEBUG: pw:install
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
ELECTRON_SKIP_BINARY_DOWNLOAD: 1
- run: npm run build
- name: Download blob report artifact
uses: ./.github/actions/download-artifact
with:
namePrefix: 'blob-report'
path: 'all-blob-reports'
- name: Merge reports
run: |
npx playwright merge-reports --config .github/workflows/merge.config.ts ./all-blob-reports
env:
NODE_OPTIONS: --max-old-space-size=4096
- name: Azure Login
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_BLOB_REPORTS_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_BLOB_REPORTS_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_BLOB_REPORTS_SUBSCRIPTION_ID }}
- name: Upload HTML report to Azure
run: |
REPORT_DIR='run-${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }}-${{ github.sha }}'
azcopy cp --recursive "./playwright-report/*" "https://mspwblobreport.blob.core.windows.net/\$web/$REPORT_DIR"
echo "Report url: https://mspwblobreport.z1.web.core.windows.net/$REPORT_DIR/index.html"
env:
AZCOPY_AUTO_LOGIN_TYPE: AZCLI
- name: Read pull request number
uses: ./.github/actions/download-artifact
with:
namePrefix: 'pull-request'
path: '.'
- name: Comment on PR
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
let prNumber;
if (context.payload.workflow_run.event === 'pull_request') {
const prs = context.payload.workflow_run.pull_requests;
if (prs.length) {
prNumber = prs[0].number;
} else {
prNumber = parseInt(fs.readFileSync('pull_request_number.txt').toString());
console.log('Read pull request number from file: ' + prNumber);
}
} else {
core.error('Unsupported workflow trigger event: ' + context.payload.workflow_run.event);
return;
}
if (!prNumber) {
core.error('No pull request found for commit ' + context.sha + ' and workflow triggered by: ' + context.payload.workflow_run.event);
return;
}
const reportDir = 'run-${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }}-${{ github.sha }}';
const reportUrl = `https://mspwblobreport.z1.web.core.windows.net/${reportDir}/index.html#?q=s%3Afailed%20s%3Aflaky`;
const mergeWorkflowUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const reportMd = await fs.promises.readFile('report.md', 'utf8');
const commitSha = '${{ github.sha }}vs${{ github.event.workflow_run.head_commit }}';
const newTableRow = `| [${{ github.event.workflow_run.name }}](${mergeWorkflowUrl}) | ${commitSha.substring(0, 7)} ${reportMd} [Open](${reportUrl}) |`;
const { data: pullRequest } = await github.rest.pulls.get({
...context.repo,
pull_number: prNumber,
});
let body = (pullRequest.body || '').trimEnd();
const mergeReportsHeader = '### Merge Reports';
const tableHeader = '| Bot | Commit | Failing | Flaky | Passed | HTML Report |';
if (!body.includes(mergeReportsHeader)) {
body += `\n\n---\n\n${mergeReportsHeader}\n\n${tableHeader}\n|---|---|---|---|---|---|\n`;
}
const lines = body.split('\n');
const headerIndex = lines.findIndex(line => line.includes(mergeReportsHeader));
const tableStartIndex = lines.findIndex((line, index) => index > headerIndex && line.includes(tableHeader));
if (tableStartIndex !== -1) {
// If the table already exists in the lines array, insert the new row right after the table start index.
lines.splice(tableStartIndex + 1, 0, newTableRow);
} else if (headerIndex !== -1) {
// If the header exists but the table does not, insert the table header and the new row right after the header index.
lines.splice(headerIndex + 1, 0, tableHeader, '|---|---|---|---|---|---|', newTableRow);
} else {
// If neither the header nor the table exists, append the header, table header, and new row to the end of the lines array.
lines.push(`\n\n---\n\n${mergeReportsHeader}\n\n${tableHeader}\n|---|---|---|---|---|---|\n${newTableRow}`);
}
const updatedBody = lines.join('\n');
await github.rest.pulls.update({
...context.repo,
pull_number: prNumber,
body: updatedBody,
});
core.info('Updated PR description with merge report results.');