|
| 1 | +name: System Contract Preview (PR) |
| 2 | + |
| 3 | +# When a pull request adds or changes a system contract's document schemas, |
| 4 | +# post a link that opens each one in the contract viewer: a new contract on its |
| 5 | +# own, a changed file or a new schema version compared with what it replaces. |
| 6 | +# The viewer reads the files from GitHub itself; this only builds the links. |
| 7 | + |
| 8 | +on: |
| 9 | + pull_request: |
| 10 | + types: [opened, synchronize, reopened] |
| 11 | + paths: |
| 12 | + - 'packages/*-contract/schema/**/*.json' |
| 13 | + - '.github/workflows/system-contract-preview.yml' |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: system-contract-preview-${{ github.event.pull_request.number }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +env: |
| 24 | + VIEWER_URL: https://dashpay.github.io/contract-visualizer/ |
| 25 | + |
| 26 | +jobs: |
| 27 | + preview: |
| 28 | + name: Link the contract viewer |
| 29 | + runs-on: ubuntu-24.04 |
| 30 | + steps: |
| 31 | + - name: Find the changed system contracts and post the links |
| 32 | + uses: actions/github-script@v7 |
| 33 | + with: |
| 34 | + script: | |
| 35 | + const pr = context.payload.pull_request; |
| 36 | + const { owner, repo } = context.repo; |
| 37 | + const viewer = process.env.VIEWER_URL; |
| 38 | +
|
| 39 | + // Compare against where the branch left the base, as GitHub's |
| 40 | + // "Files changed" tab does, so schemas merged into the base since |
| 41 | + // then do not show up as changed |
| 42 | + let base = pr.base.sha; |
| 43 | + try { |
| 44 | + const { data } = await github.rest.repos.compareCommitsWithBasehead({ |
| 45 | + owner, repo, basehead: `${pr.base.sha}...${pr.head.sha}`, per_page: 1, |
| 46 | + }); |
| 47 | + base = data.merge_base_commit.sha; |
| 48 | + } catch (error) { |
| 49 | + core.warning(`Could not find the merge base, using the base tip: ${error.message}`); |
| 50 | + } |
| 51 | +
|
| 52 | + // A system contract's schemas: packages/<name>-contract/schema/v<N>/<file>.json. |
| 53 | + // File names come from the pull request, so only names of this shape |
| 54 | + // are used, and they are printed as code. |
| 55 | + const SCHEMA = /^packages\/([a-z0-9-]+-contract)\/schema\/v([0-9]{1,3})\/([A-Za-z0-9._-]+\.json)$/; |
| 56 | + const files = (await github.paginate(github.rest.pulls.listFiles, { |
| 57 | + owner, repo, pull_number: pr.number, per_page: 100, |
| 58 | + })) |
| 59 | + .map((file) => ({ ...file, match: SCHEMA.exec(file.filename) })) |
| 60 | + .filter((file) => file.match); |
| 61 | +
|
| 62 | + const headRepo = pr.head.repo.full_name; |
| 63 | + const blob = (fullName, sha, path) => `https://github.com/${fullName}/blob/${sha}/${path}`; |
| 64 | + const open = (source) => `${viewer}?url=${encodeURIComponent(source)}`; |
| 65 | + const compare = (before, after) => `${viewer}?base=${encodeURIComponent(before)}&head=${encodeURIComponent(after)}`; |
| 66 | +
|
| 67 | + async function existsAtBase(path) { |
| 68 | + try { |
| 69 | + await github.rest.repos.getContent({ owner, repo, path, ref: base }); |
| 70 | + return true; |
| 71 | + } catch (error) { |
| 72 | + if (error.status === 404) return false; |
| 73 | + throw error; |
| 74 | + } |
| 75 | + } |
| 76 | +
|
| 77 | + const rows = []; |
| 78 | + for (const file of files.slice(0, 30)) { |
| 79 | + const [, contract, version, name] = file.match; |
| 80 | + const path = file.filename; |
| 81 | + const at = `\`${path}\``; |
| 82 | + if (file.status === 'removed') { |
| 83 | + rows.push(`- ${at}: removed ([last version](${open(blob(`${owner}/${repo}`, base, path))}))`); |
| 84 | + continue; |
| 85 | + } |
| 86 | + const head = blob(headRepo, pr.head.sha, path); |
| 87 | + if (file.status === 'added') { |
| 88 | + // A new schema version is compared with the newest earlier |
| 89 | + // version of the same file |
| 90 | + let previous = null; |
| 91 | + for (let older = Number(version) - 1; older >= 1 && !previous; older -= 1) { |
| 92 | + const candidate = `packages/${contract}/schema/v${older}/${name}`; |
| 93 | + if (await existsAtBase(candidate)) previous = { path: candidate, version: older }; |
| 94 | + } |
| 95 | + rows.push(previous |
| 96 | + ? `- ${at}: new version. **[Compare with \`v${previous.version}\`](${compare(blob(`${owner}/${repo}`, base, previous.path), head)})** · [open on its own](${open(head)})` |
| 97 | + : `- ${at}: new contract. **[Open it in the contract viewer](${open(head)})**`); |
| 98 | + continue; |
| 99 | + } |
| 100 | + // Modified or renamed: the file as the base had it, and as the pull request leaves it |
| 101 | + const before = blob(`${owner}/${repo}`, base, file.status === 'renamed' && file.previous_filename ? file.previous_filename : path); |
| 102 | + rows.push(`- ${at}: changed. **[Compare in the contract viewer](${compare(before, head)})** · [open on its own](${open(head)})`); |
| 103 | + } |
| 104 | + if (files.length > 30) rows.push(`- and ${files.length - 30} more`); |
| 105 | +
|
| 106 | + const marker = '<!-- system-contract-preview -->'; |
| 107 | + const body = [ |
| 108 | + marker, |
| 109 | + '📜 **System contracts**', |
| 110 | + '', |
| 111 | + rows.length === 0 |
| 112 | + ? 'This pull request no longer adds or changes a system contract\'s document schemas.' |
| 113 | + : 'This pull request adds or changes system contract document schemas. The contract viewer diagrams each one, and a comparison marks what is added, changed and removed, with the update rule each change meets.', |
| 114 | + '', |
| 115 | + ...rows, |
| 116 | + '', |
| 117 | + `_Compared \`${base.slice(0, 10)}\` with \`${pr.head.sha.slice(0, 10)}\`. Updated at ${new Date().toISOString()}_`, |
| 118 | + ].join('\n'); |
| 119 | +
|
| 120 | + await core.summary.addRaw(body.replace(marker, '')).write(); |
| 121 | +
|
| 122 | + // Fork pull requests run with a read-only token that cannot post comments |
| 123 | + if (pr.head.repo.full_name !== `${owner}/${repo}`) { |
| 124 | + core.notice(body.replace(marker, '')); |
| 125 | + return; |
| 126 | + } |
| 127 | +
|
| 128 | + const comments = await github.paginate(github.rest.issues.listComments, { owner, repo, issue_number: pr.number }); |
| 129 | + const existing = comments.find((comment) => comment.body.includes(marker)); |
| 130 | + if (existing) { |
| 131 | + await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body }); |
| 132 | + } else if (rows.length > 0) { |
| 133 | + await github.rest.issues.createComment({ owner, repo, issue_number: pr.number, body }); |
| 134 | + } |
0 commit comments