|
1 | 1 | name: Template Compatibility Comment |
2 | 2 |
|
3 | 3 | # Triggered when the unprivileged "Template Compatibility Check" workflow |
4 | | -# completes. This workflow has pull-requests: write so it can post PR comments, |
5 | | -# but it never checks out or executes any external code — it only reads the |
6 | | -# artifact produced by the check workflow. |
| 4 | +# completes. This workflow has pull-requests: write so it can post PR comments. |
| 5 | +# It checks out the default branch (only) to load scripts/template-compatibility-comment.js, |
| 6 | +# then reads the artifact produced by the check workflow. |
7 | 7 | # |
8 | 8 | # SECURITY: workflow_run always runs on the default branch, so this workflow |
9 | | -# definition itself cannot be tampered with by a PR contributor. Artifact |
10 | | -# contents are treated as untrusted strings and sanitized before use. |
| 9 | +# definition and checked-out script cannot be tampered with by a PR contributor. |
| 10 | +# Artifact contents are treated as untrusted strings and sanitized before use. |
11 | 11 |
|
12 | 12 | on: |
13 | 13 | workflow_run: |
14 | 14 | workflows: ["Template Compatibility Check"] |
15 | 15 | types: [completed] |
16 | 16 |
|
17 | 17 | permissions: |
| 18 | + contents: read |
18 | 19 | pull-requests: write |
19 | 20 | actions: read # required to download artifacts from another workflow run |
20 | 21 |
|
|
23 | 24 | runs-on: ubuntu-latest |
24 | 25 |
|
25 | 26 | steps: |
| 27 | + - name: Checkout repository |
| 28 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 29 | + |
26 | 30 | - name: Download results artifact |
27 | 31 | uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 |
28 | 32 | with: |
|
35 | 39 | uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
36 | 40 | with: |
37 | 41 | script: | |
38 | | - const fs = require('fs'); |
39 | | -
|
40 | | - const read = (filename) => { |
41 | | - try { return fs.readFileSync(`/tmp/compat-results/${filename}`, 'utf8').trim(); } |
42 | | - catch { return ''; } |
43 | | - }; |
44 | | -
|
45 | | - // Validate PR number — must be a positive integer. |
46 | | - const prNumber = parseInt(read('pr-number.txt'), 10); |
47 | | - if (!Number.isInteger(prNumber) || prNumber <= 0) { |
48 | | - console.log('Invalid or missing PR number in artifact; skipping comment.'); |
49 | | - return; |
50 | | - } |
51 | | -
|
52 | | - const exitCode = read('exit-code.txt'); |
53 | | - const fullOutput = read('output.txt'); |
54 | | - // Sanitize values read from the artifact before embedding in markdown |
55 | | - // to prevent injection (e.g. a malicious branch name or script output |
56 | | - // containing markdown syntax that escapes a code fence). |
57 | | - const templatesRef = read('templates-ref.txt').replace(/[^a-zA-Z0-9._\/-]/g, ''); |
58 | | - const headRef = read('head-ref.txt').replace(/[^a-zA-Z0-9._\/-]/g, ''); |
59 | | -
|
60 | | - const marker = '<!-- template-compat-comment -->'; |
61 | | -
|
62 | | - const { data: comments } = await github.rest.issues.listComments({ |
63 | | - owner: context.repo.owner, |
64 | | - repo: context.repo.repo, |
65 | | - issue_number: prNumber, |
66 | | - }); |
67 | | - const existing = comments.find(c => c.body.includes(marker)); |
68 | | -
|
69 | | - if (exitCode === '0') { |
70 | | - // Templates pass — remove any stale failure comment. |
71 | | - if (existing) { |
72 | | - await github.rest.issues.deleteComment({ |
73 | | - owner: context.repo.owner, |
74 | | - repo: context.repo.repo, |
75 | | - comment_id: existing.id, |
76 | | - }); |
77 | | - } |
78 | | - return; |
79 | | - } |
80 | | -
|
81 | | - // Extract just the "Results" and "Failure Details" sections from output. |
82 | | - const resultsMatch = fullOutput.match(/={8,}\nResults:.*\n={8,}[\s\S]*/); |
83 | | - const failureSummary = resultsMatch ? resultsMatch[0].trim() : fullOutput.trim(); |
84 | | -
|
85 | | - const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.payload.workflow_run.id}`; |
86 | | - const refNote = templatesRef === 'main' |
87 | | - ? 'tested against `cre-templates:main`' |
88 | | - : `tested against \`cre-templates:${templatesRef}\` (compat branch)`; |
89 | | -
|
90 | | - const body = [ |
91 | | - '## ⚠️ Template Compatibility Failures', |
92 | | - '', |
93 | | - `This PR breaks one or more templates in [cre-templates](https://github.com/smartcontractkit/cre-templates) (${refNote}).`, |
94 | | - '', |
95 | | - '```', |
96 | | - failureSummary, |
97 | | - '```', |
98 | | - '', |
99 | | - `[View full output →](${runUrl})`, |
100 | | - '', |
101 | | - '<details>', |
102 | | - '<summary>What should I do?</summary>', |
103 | | - '', |
104 | | - '- **Accidental break:** Fix the SDK change so existing templates continue to compile.', |
105 | | - `- **Intentional breaking change:** Create a branch in \`cre-templates\` named \`compat/${headRef}\` with the template fixes applied. This job will automatically retest against that branch.`, |
106 | | - '', |
107 | | - '</details>', |
108 | | - ].join('\n'); |
109 | | -
|
110 | | - const commentBody = `${marker}\n${body}`; |
111 | | -
|
112 | | - if (existing) { |
113 | | - await github.rest.issues.updateComment({ |
114 | | - owner: context.repo.owner, |
115 | | - repo: context.repo.repo, |
116 | | - comment_id: existing.id, |
117 | | - body: commentBody, |
118 | | - }); |
119 | | - } else { |
120 | | - await github.rest.issues.createComment({ |
121 | | - owner: context.repo.owner, |
122 | | - repo: context.repo.repo, |
123 | | - issue_number: prNumber, |
124 | | - body: commentBody, |
125 | | - }); |
126 | | - } |
| 42 | + const path = require('path'); |
| 43 | + const run = require(path.join(process.env.GITHUB_WORKSPACE, 'scripts', 'template-compatibility-comment.js')); |
| 44 | + await run({ github, context }); |
0 commit comments