1+ name : Metrics Comparison and Post Comment
2+ on :
3+ workflow_run :
4+ workflows : ["E2E Tests"]
5+ types : [completed]
6+ permissions :
7+ contents : read
8+ pull-requests : write
9+ jobs :
10+ metrics-comparison :
11+ name : Compare Metrics
12+ runs-on : ubuntu-latest
13+ steps :
14+ - name : Checkout code
15+ uses : actions/checkout@v4
16+ with :
17+ ref : ${{ github.event.repository.default_branch }}
18+
19+ - name : Install adm-zip
20+ run : npm install adm-zip
21+
22+ - name : Download all metrics artifacts from triggering workflow
23+ id : download-artifacts
24+ uses : actions/github-script@v7
25+ with :
26+ script : |
27+ const { owner, repo } = context.repo;
28+ const workflowRunId = context.payload.workflow_run.id;
29+
30+ // List all artifacts from the triggering workflow run
31+ const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
32+ owner,
33+ repo,
34+ run_id: workflowRunId,
35+ });
36+
37+ // Download and extract each artifact
38+ const fs = require('fs');
39+ const path = require('path');
40+ const AdmZip = require('adm-zip');
41+
42+ for (const artifact of artifacts.data.artifacts) {
43+ const download = await github.rest.actions.downloadArtifact({
44+ owner,
45+ repo,
46+ artifact_id: artifact.id,
47+ archive_format: 'zip',
48+ });
49+
50+ const zip = new AdmZip(Buffer.from(download.data));
51+ zip.extractAllTo(path.join(process.env.GITHUB_WORKSPACE, '.metrics', artifact.name), true);
52+ console.log(`Extracted artifact: ${artifact.name}`);
53+ }
54+
55+ // Extract PR number (adapted from PDF logic)
56+ let prNumber = null;
57+ const pullRequest = await github.rest.pulls.list({
58+ owner,
59+ repo,
60+ head: `${context.payload.workflow_run.head_repository.full_name}:${context.payload.workflow_run.head_branch}`,
61+ });
62+
63+ if (pullRequest.data.length > 0) {
64+ prNumber = pullRequest.data[0].number;
65+ } else {
66+ // Fallback to commit SHA if needed
67+ const commitSha = context.payload.workflow_run.head_sha;
68+ const prsForCommit = await github.rest.repos.listPullRequestsAssociatedWithCommit({
69+ owner,
70+ repo,
71+ commit_sha: commitSha,
72+ });
73+ if (prsForCommit.data.length > 0) {
74+ prNumber = prsForCommit.data[0].number;
75+ }
76+ }
77+
78+ if (prNumber) {
79+ console.log(`Found PR Number: ${prNumber}`);
80+ core.setOutput('pr_number', prNumber);
81+ } else {
82+ console.log('Could not determine PR number. Skipping comment.');
83+ core.setFailed('Could not determine PR number for commenting.');
84+ }
85+ env :
86+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
87+
88+ - name : Install dependencies
89+ if : success() && steps.download-artifacts.outputs.pr_number
90+ run : |
91+ python3 -m pip install prometheus-client
92+ npm install @actions/core @actions/github
93+
94+ - name : Compare metrics and generate summary
95+ if : success() && steps.download-artifacts.outputs.pr_number
96+ id : compare-metrics
97+ shell : bash
98+ run : |
99+ bash ./scripts/e2e/metrics_summary.sh
100+ env :
101+ LINK_TO_ARTIFACT : " https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}"
102+
103+ - name : Post PR comment with combined metrics summary
104+ if : steps.compare-metrics.outputs.DIFF_FOUND == 'true' && steps.download-artifacts.outputs.pr_number
105+ uses : thollander/actions-comment-pull-request@v3
106+ with :
107+ file-path : ./.metrics/combined_summary.md
108+ github-token : ' ${{ secrets.GITHUB_TOKEN }}'
109+ comment-tag : " ## Metrics Comparison Summary"
110+ pr-number : ${{ steps.download-artifacts.outputs.pr_number }}
0 commit comments