-
Notifications
You must be signed in to change notification settings - Fork 703
[6x.] Storybook cloudflare #19213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brianjhanson
wants to merge
12
commits into
6.x
Choose a base branch
from
feature/storybook-cloudflare
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+517
−130
Open
[6x.] Storybook cloudflare #19213
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f012183
Add Cloudflare Pages deploy workflows for Storybook
brianjhanson a3b35e7
Rename cp Storybook Cloudflare Pages project to craftcms-ui-storybook
brianjhanson 763e841
Merge branch '6.x' of github.com:craftcms/cms into feature/storybook-…
brianjhanson e3d5f91
Add wrangler as an optional dep
brianjhanson 47b0be7
Change relevant files to test workflow
brianjhanson 209d827
Fix build error
brianjhanson ee703cb
Reorganize the workflows a bit
brianjhanson ae453ea
Revert "Change relevant files to test workflow"
brianjhanson a6895ea
Cleanup organization a bit
brianjhanson 563c990
Remove push running
brianjhanson 4aefc07
Add permissions config
brianjhanson 177c53f
Potential fix for pull request finding
brianjhanson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Creates or updates a single PR comment that links each changed component to | ||
| // its rendered story in the deployed Storybook previews. | ||
| // | ||
| // Invoked from .github/workflows/storybook.yml via actions/github-script: | ||
| // | ||
| // const run = require('./.github/scripts/storybook-preview-comment.cjs') | ||
| // await run({github, context}) | ||
| // | ||
| // The deploy results/URLs are passed in through the environment (CP_RESULT, | ||
| // CP_URL, RESOURCES_RESULT, RESOURCES_URL) so untrusted values never get | ||
| // interpolated into the workflow's inline script. | ||
|
|
||
| const MARKER = '<!-- storybook-preview-comment -->'; | ||
|
|
||
| // Directory portion of a story's importPath, normalized for comparison against | ||
| // the repo-relative paths returned by the GitHub API. | ||
| const dirOf = (p) => | ||
| p | ||
| .replace(/^\.\//, '') | ||
| .split('/') | ||
| .slice(0, -1) | ||
| .join('/'); | ||
|
|
||
| // Storybook publishes an index of every story at `<preview>/index.json`. We use | ||
| // it to turn a changed file into a deep link to its rendered component. | ||
| async function storyIndex(url) { | ||
| try { | ||
| const res = await fetch(`${url}/index.json`); | ||
| if (!res.ok) return []; | ||
| return Object.values((await res.json()).entries || {}); | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| // Build the markdown section for one preview: an "open Storybook" link plus a | ||
| // list of the components touched by this PR, each deep-linked into the preview. | ||
| async function sectionFor(preview, changed) { | ||
| if (preview.result !== 'success' || !preview.url) { | ||
| return `**${preview.name}** — ⚠️ deploy ${preview.result}, no preview available`; | ||
| } | ||
|
|
||
| const entries = await storyIndex(preview.url); | ||
|
|
||
| // A component "changed" if any changed file lives in the same directory as one | ||
| // of this Storybook's story entries. Collapse the several entries per story | ||
| // file down to one link per component, preferring the Docs page when present. | ||
| // | ||
| // Scope the changed-file matching to the relevant subtree so similarly-named | ||
| // directories elsewhere in the repo don't create false positives. | ||
| const baseDir = | ||
| preview.name === '@craftcms/cp' | ||
| ? 'packages/craftcms-cp' | ||
| : preview.name === 'resources/js' | ||
| ? 'resources/js' | ||
| : null; | ||
|
|
||
| const changedInScope = baseDir | ||
| ? changed | ||
| .filter((file) => file.startsWith(`${baseDir}/`)) | ||
| .map((file) => file.slice(baseDir.length + 1)) | ||
| : changed; | ||
|
|
||
| const byImport = new Map(); | ||
| for (const entry of entries) { | ||
| if (!entry?.importPath) continue; | ||
| const dir = dirOf(entry.importPath); | ||
| if (!dir || !changedInScope.some((file) => file.startsWith(`${dir}/`))) continue; | ||
| const current = byImport.get(entry.importPath); | ||
| if (!current || (entry.type === 'docs' && current.type !== 'docs')) { | ||
| byImport.set(entry.importPath, entry); | ||
| } | ||
| } | ||
|
|
||
| const links = [...byImport.values()] | ||
| .sort((a, b) => a.title.localeCompare(b.title)) | ||
| .map((entry) => { | ||
| const kind = entry.type === 'docs' ? 'docs' : 'story'; | ||
| return `- [${entry.title}](${preview.url}/?path=/${kind}/${entry.id})`; | ||
| }); | ||
|
|
||
| const heading = `**${preview.name}** — [open Storybook](${preview.url})`; | ||
| return links.length | ||
| ? `${heading}\n\nChanged components:\n${links.join('\n')}` | ||
| : `${heading}\n\n_No changed components detected in this Storybook._`; | ||
| } | ||
|
|
||
| module.exports = async ({github, context}) => { | ||
| const previews = [ | ||
| {name: '@craftcms/cp', result: process.env.CP_RESULT, url: process.env.CP_URL}, | ||
| {name: 'resources/js', result: process.env.RESOURCES_RESULT, url: process.env.RESOURCES_URL}, | ||
| ]; | ||
|
|
||
| // Every file changed in this PR (excluding deletions, which no longer have a | ||
| // rendered story to point at). | ||
| const changed = ( | ||
| await github.paginate(github.rest.pulls.listFiles, { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: context.issue.number, | ||
| per_page: 100, | ||
| }) | ||
| ) | ||
| .filter((file) => file.status !== 'removed') | ||
| .map((file) => file.filename); | ||
|
|
||
| const sections = []; | ||
| for (const preview of previews) { | ||
| sections.push(await sectionFor(preview, changed)); | ||
| } | ||
|
|
||
| const body = `${MARKER}\n### 📚 Storybook previews\n\n${sections.join('\n\n')}`; | ||
|
|
||
| const {data: comments} = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| }); | ||
| const existing = comments.find((comment) => comment.body.includes(MARKER)); | ||
|
|
||
| if (existing) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: existing.id, | ||
| body, | ||
| }); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body, | ||
| }); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.