Skip to content
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

[code-infra] Qr code deploy urls #45078

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions dangerFileContent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { exec } from 'child_process';
import type * as dangerModule from 'danger';
import replaceUrl from '@mui-internal/api-docs-builder/utils/replaceUrl';
// eslint-disable-next-line import/no-relative-packages
import { loadComparison } from './scripts/sizeSnapshot';
import { loadComparison } from 'size-snapshot';

declare const danger: (typeof dangerModule)['danger'];
declare const markdown: (typeof dangerModule)['markdown'];
Expand Down Expand Up @@ -187,7 +186,9 @@ async function reportBundleSize() {
}
}

function addDeployPreviewUrls() {
async function addDeployPreviewUrls() {
const netlifyPreview = `https://deploy-preview-${danger.github.pr.number}--material-ui.netlify.app/`;

/**
* The incoming path from danger does not start with `/`
* e.g. ['docs/data/joy/components/button/button.md']
Expand All @@ -212,10 +213,14 @@ function addDeployPreviewUrls() {
.replace('components/', 'react-');
}

return url;
return new URL(url, netlifyPreview);
}

const netlifyPreview = `https://deploy-preview-${danger.github.pr.number}--material-ui.netlify.app/`;
async function formatMdLinkWithQrCode(path: string) {
const url = String(formatFileToLink(path));
const qr = `${netlifyPreview}edge-functions/qr-code?text=${encodeURIComponent(url)}`;
return `<details><summary>page <a href="${url}">${path}</a></summary><img src="${qr}" alt="QR code" /></details>`;
}

const files = [...danger.git.created_files, ...danger.git.modified_files];

Expand All @@ -224,24 +229,17 @@ function addDeployPreviewUrls() {
.filter((file) => file.startsWith('docs/data') && file.endsWith('.md'))
.slice(0, 5);

const docsMdLines = await Promise.all(docs.map(formatMdLinkWithQrCode));

markdown(`
## Netlify deploy preview

${
docs.length
? docs
.map((path) => {
const formattedUrl = formatFileToLink(path);
return `- [${path}](${netlifyPreview}${formattedUrl})`;
})
.join('\n')
: netlifyPreview
}
${docsMdLines.length ? docsMdLines.join('\n') : await formatMdLinkWithQrCode('/')}
`);
}

async function run() {
addDeployPreviewUrls();
await addDeployPreviewUrls();

switch (dangerCommand) {
case 'prepareBundleSizeReport':
Expand Down
6 changes: 6 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# run `pnpm --package netlify-cli dlx netlify dev --cwd .` to run functions locally
[dev]
command = "pnpm docs:dev"
targetPort = 3000
port = 8888

[build]
# Directory (relative to root of your repo) that contains the deploy-ready
# HTML files and assets generated by the build. If a base directory has
Expand Down
33 changes: 33 additions & 0 deletions netlify/edge-functions/qr-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import qr from 'https://esm.sh/[email protected]';

export default async function handler(req: Request) {
const params = new URL(req.url).searchParams;
const text = params.get('text');

if (!text) {
return new Response('Missing text query parameter', { status: 400 });
}

if (text.length > 1000) {
return new Response('Text query parameter is too long', { status: 400 });
}

const data = qr.imageSync(text, { type: 'png' });

const v = params.get('v') || '';

const cacheKey = new URLSearchParams();
cacheKey.append('text', text);
cacheKey.append('v', v);

return new Response(data, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=31536000, immutable', // Cache for 1 year
},
});
}
export const config = {
cache: 'manual',
path: '/edge-functions/qr-code',
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
"@netlify/functions": "^3.0.0",
"@slack/bolt": "^4.2.0",
"execa": "^9.5.2",
"google-auth-library": "^9.15.0"
"google-auth-library": "^9.15.0",
"qrcode": "^1.5.4"
},
"devDependencies": {
"@argos-ci/core": "^2.12.0",
Expand Down
86 changes: 68 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading