-
Notifications
You must be signed in to change notification settings - Fork 104
175 lines (152 loc) · 5.97 KB
/
grug-variant-diff.yaml
File metadata and controls
175 lines (152 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: Grug Variant Diff
on:
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
permissions:
contents: write
pull-requests: write
jobs:
changes:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
should_run: ${{ steps.filter.outputs.relevant }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
relevant:
- 'experiments/grug/**'
- 'scripts/grug_dir_diff.py'
- 'scripts/grug_variant_diff_ci.py'
- '.github/workflows/grug-variant-diff.yaml'
grug-variant-diff:
needs: changes
if: needs.changes.outputs.should_run == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Generate variant diff reports
run: |
python -m scripts.grug_variant_diff_ci \
--base-sha "${{ github.event.pull_request.base.sha }}" \
--head-sha "${{ github.event.pull_request.head.sha }}" \
--output-dir grug-diff-report \
--manifest-path grug-diff-report/manifest.json
- name: Read manifest metadata
id: manifest
run: |
python - <<'PY'
import json
import os
from pathlib import Path
manifest = json.loads(Path("grug-diff-report/manifest.json").read_text(encoding="utf-8"))
report_count = int(manifest.get("report_count", 0))
has_reports = "true" if report_count > 0 else "false"
with Path(os.environ["GITHUB_OUTPUT"]).open("a", encoding="utf-8") as output:
output.write(f"report_count={report_count}\n")
output.write(f"has_reports={has_reports}\n")
PY
- name: Upload report artifact
id: artifact
if: steps.manifest.outputs.has_reports == 'true'
uses: actions/upload-artifact@v4
with:
name: grug-variant-diff-pr-${{ github.event.pull_request.number }}
path: grug-diff-report
retention-days: 14
- name: Publish rendered report to gh-pages
id: publish
if: steps.manifest.outputs.has_reports == 'true' && github.event.pull_request.head.repo.full_name == github.repository
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: grug-diff-report
destination_dir: grug-diffs/pr-${{ github.event.pull_request.number }}
keep_files: true
- name: Post PR comment with report links
if: "!cancelled() && github.event.pull_request.head.repo.full_name == github.repository"
uses: actions/github-script@v7
env:
ARTIFACT_URL: ${{ steps.artifact.outputs.artifact-url }}
PAGES_URL: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/grug-diffs/pr-${{ github.event.pull_request.number }}
PAGES_PUBLISHED: ${{ steps.publish.outcome == 'success' }}
with:
script: |
const fs = require('fs');
const marker = '<!-- grug-variant-diff-report -->';
const manifest = JSON.parse(fs.readFileSync('grug-diff-report/manifest.json', 'utf8'));
const reportCount = Number(manifest.report_count || 0);
const pagesUrl = process.env.PAGES_URL;
const pagesPublished = process.env.PAGES_PUBLISHED === 'true';
const artifactUrl = process.env.ARTIFACT_URL || '';
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.find((comment) => comment.body && comment.body.includes(marker));
if (reportCount === 0) {
if (existing) {
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: existing.id,
});
}
return;
}
let body = '🤖 Grug variant diff report\n';
body += `${marker}\n\n`;
if (!pagesPublished) {
body += 'Rendered GitHub Pages links were not published for this run.\n\n';
}
body += '| New Variant | Closest Existing Variant | Distance Score | Diff |\n';
body += '|---|---|---:|---|\n';
for (const report of manifest.reports) {
const variant = report.variant;
const closest = report.closest_variant;
const score = report.distance_score;
const link = pagesPublished
? `[Open](${pagesUrl}/${variant}/index.html)`
: `artifact:\`${report.report_relpath}\``;
body += `| \`${variant}\` | \`${closest}\` | ${score} | ${link} |\n`;
}
if (artifactUrl) {
body += `\nArtifact fallback: [Download report bundle](${artifactUrl})\n`;
}
if (!pagesPublished) {
body += '\nFor fork PRs, this workflow usually cannot publish to `gh-pages`; artifact link remains available.';
}
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}