|
| 1 | +name: Validate Canvas Extensions |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [staged] |
| 6 | + types: [opened, synchronize, reopened] |
| 7 | + paths: |
| 8 | + - "extensions/**" |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + validate: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Validate changed canvas extensions |
| 24 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0 |
| 25 | + with: |
| 26 | + script: | |
| 27 | + const fs = require('fs'); |
| 28 | + const path = require('path'); |
| 29 | +
|
| 30 | + // Collect changed extension directories from the PR diff |
| 31 | + const { execSync } = require('child_process'); |
| 32 | + const changedFiles = execSync( |
| 33 | + `git diff --name-only origin/${{ github.base_ref }}...HEAD` |
| 34 | + ).toString().trim().split('\n').filter(Boolean); |
| 35 | +
|
| 36 | + const EXTENSIONS_DIR = 'extensions'; |
| 37 | + const EXTERNAL_ASSETS_DIR = 'external-assets'; |
| 38 | +
|
| 39 | + const changedExtDirs = new Set(); |
| 40 | + for (const file of changedFiles) { |
| 41 | + const parts = file.split('/'); |
| 42 | + if (parts[0] === EXTENSIONS_DIR && parts.length >= 2) { |
| 43 | + const extName = parts[1]; |
| 44 | + // Skip the external-assets directory — it's not a canvas extension |
| 45 | + if (extName !== EXTERNAL_ASSETS_DIR) { |
| 46 | + changedExtDirs.add(path.join(EXTENSIONS_DIR, extName)); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +
|
| 51 | + if (changedExtDirs.size === 0) { |
| 52 | + console.log('No canvas extension directories changed — skipping validation.'); |
| 53 | + return; |
| 54 | + } |
| 55 | +
|
| 56 | + console.log(`Validating ${changedExtDirs.size} extension(s): ${[...changedExtDirs].join(', ')}`); |
| 57 | +
|
| 58 | + const errors = []; |
| 59 | +
|
| 60 | + for (const extDir of changedExtDirs) { |
| 61 | + if (!fs.existsSync(extDir)) { |
| 62 | + // Directory was deleted — skip |
| 63 | + console.log(`${extDir} no longer exists (deleted?), skipping.`); |
| 64 | + continue; |
| 65 | + } |
| 66 | +
|
| 67 | + const extName = path.basename(extDir); |
| 68 | +
|
| 69 | + // Rule 1: must contain extension.mjs |
| 70 | + const mainFile = path.join(extDir, 'extension.mjs'); |
| 71 | + if (!fs.existsSync(mainFile)) { |
| 72 | + errors.push( |
| 73 | + `**\`${extDir}\`**: missing required \`extension.mjs\`. ` + |
| 74 | + `Canvas extensions must have their entry point named \`extension.mjs\`.` |
| 75 | + ); |
| 76 | + } |
| 77 | +
|
| 78 | + // Rule 2: must contain assets/preview.png |
| 79 | + const previewFile = path.join(extDir, 'assets', 'preview.png'); |
| 80 | + if (!fs.existsSync(previewFile)) { |
| 81 | + errors.push( |
| 82 | + `**\`${extDir}\`**: missing required \`assets/preview.png\`. ` + |
| 83 | + `Canvas extensions must include a screenshot at \`assets/preview.png\` ` + |
| 84 | + `so reviewers and users can preview the extension before installing it.` |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | + if (errors.length === 0) { |
| 90 | + console.log('✅ All changed canvas extensions pass validation.'); |
| 91 | + return; |
| 92 | + } |
| 93 | +
|
| 94 | + const isFork = context.payload.pull_request.head.repo.fork; |
| 95 | + const body = [ |
| 96 | + '❌ **Canvas extension validation failed**', |
| 97 | + '', |
| 98 | + 'The following issue(s) were found in changed canvas extension(s):', |
| 99 | + '', |
| 100 | + ...errors.map(e => `- ${e}`), |
| 101 | + '', |
| 102 | + '---', |
| 103 | + '', |
| 104 | + '### Required structure for canvas extensions', |
| 105 | + '', |
| 106 | + 'Each extension folder under `extensions/` must contain:', |
| 107 | + '', |
| 108 | + '| Path | Required | Description |', |
| 109 | + '|------|----------|-------------|', |
| 110 | + '| `extension.mjs` | ✅ | Entry point for the canvas extension |', |
| 111 | + '| `assets/preview.png` | ✅ | Screenshot shown on the website and in the marketplace |', |
| 112 | + '', |
| 113 | + 'Please add the missing file(s) and push an update to this PR.', |
| 114 | + ].join('\n'); |
| 115 | +
|
| 116 | + if (!isFork) { |
| 117 | + try { |
| 118 | + await github.rest.pulls.createReview({ |
| 119 | + owner: context.repo.owner, |
| 120 | + repo: context.repo.repo, |
| 121 | + pull_number: context.issue.number, |
| 122 | + event: 'REQUEST_CHANGES', |
| 123 | + body |
| 124 | + }); |
| 125 | + } catch (error) { |
| 126 | + core.warning(`Could not post PR review: ${error.message}`); |
| 127 | + core.warning(body); |
| 128 | + } |
| 129 | + } else { |
| 130 | + core.warning('PR is from a fork — skipping createReview to avoid permission errors.'); |
| 131 | + core.warning(body); |
| 132 | + } |
| 133 | +
|
| 134 | + core.setFailed(`Canvas extension validation failed with ${errors.length} error(s).`); |
0 commit comments