mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-12-02 16:49:06 +00:00
* ci: show icon previews in the PR description Replace the icon-preview comment action with a workflow that writes a preview image per added or changed icon into the PR description, between the "Icon preview" markers from the PR template. The images are served by review.lucide.dev, so the workflow no longer needs dependencies or the PR's SVGs. - scripts/updatePullRequestDescriptionPreview.mts renders the markup and replaces it between the markers (idempotent, keeps CRLF bodies and the surrounding text as is), and updates the body through the REST API. - .github/workflows/pull-request-description-preview.yml runs it on pull_request_target from the base branch only. - Add the "Icon preview" section with the markers to the PR template. - Remove the pull-request-icon-preview workflow and the icon-preview action. Co-Authored-By: Claude <noreply@anthropic.com> * ci: move the PR description preview into the icon-preview action Move the description preview script and its tests from scripts/ into .github/actions/icon-preview, a composite action like the old one, with a package.json (start, debug, test) and an .env.example for local runs. The workflow now calls the action with the PR number, head SHA, changed files and token as inputs. Co-Authored-By: Claude <noreply@anthropic.com> * ci: adjust the icon-preview action instead of adding a new workflow Restore the icon-preview action and its workflow from main and adjust them in place: the action now runs updatePullRequestDescriptionPreview.mts (Node built-ins only, so no dependencies or install step) to write the previews into the PR description, and the workflow calls it with the PR number, head SHA, changed files and token. The separate pull-request-description-preview workflow is removed. Co-Authored-By: Claude <noreply@anthropic.com> * fix(icon-preview): remove a lone marker before adding a fresh pair When only the start or the end marker is left in the description, remove it (with its line break) before prepending a new marker pair, so the body never keeps an orphan marker. Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
223 lines
7.6 KiB
TypeScript
223 lines
7.6 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { describe, it } from 'node:test';
|
|
|
|
import {
|
|
endMarker,
|
|
MAX_ICONS,
|
|
renderIconPreview,
|
|
replaceBetweenMarkers,
|
|
startMarker,
|
|
} from './updatePullRequestDescriptionPreview.mts';
|
|
|
|
const SHA = 'abc1234def5678';
|
|
|
|
describe('renderIconPreview', () => {
|
|
it('renders a linked picture per icon', () => {
|
|
assert.equal(
|
|
renderIconPreview({ pr: 42, sha: SHA, icons: [{ name: 'heart', status: 'added' }] }),
|
|
[
|
|
'<a href="https://review.lucide.dev/pr/42?icon=heart" target="_blank">',
|
|
'<picture>',
|
|
' <source media="(min-width: 1280px)" srcset="https://review.lucide.dev/api/pr/42/heart/preview.svg?view=full&v=abc1234" />',
|
|
' <source media="(min-width: 768px)" srcset="https://review.lucide.dev/api/pr/42/heart/preview.svg?view=medium&v=abc1234" />',
|
|
' <img alt="heart (added)" src="https://review.lucide.dev/api/pr/42/heart/preview.svg?v=abc1234" />',
|
|
'</picture>',
|
|
'</a>',
|
|
].join('\n'),
|
|
);
|
|
});
|
|
|
|
it('uses the status in the alt text', () => {
|
|
const markup = renderIconPreview({
|
|
pr: 1,
|
|
sha: SHA,
|
|
icons: [
|
|
{ name: 'heart', status: 'changed' },
|
|
{ name: 'star', status: 'added' },
|
|
],
|
|
});
|
|
assert.match(markup, /alt="heart \(changed\)"/);
|
|
assert.match(markup, /alt="star \(added\)"/);
|
|
});
|
|
|
|
it('sorts by name and separates icons with a blank line', () => {
|
|
const markup = renderIconPreview({
|
|
pr: 1,
|
|
sha: SHA,
|
|
icons: [
|
|
{ name: 'zap', status: 'added' },
|
|
{ name: 'anchor', status: 'changed' },
|
|
],
|
|
});
|
|
const blocks = markup.split('\n\n');
|
|
assert.equal(blocks.length, 2);
|
|
assert.match(blocks[0], /icon=anchor"/);
|
|
assert.match(blocks[1], /icon=zap"/);
|
|
});
|
|
|
|
it('drops invalid icon names', () => {
|
|
const markup = renderIconPreview({
|
|
pr: 1,
|
|
sha: SHA,
|
|
icons: [
|
|
{ name: 'heart', status: 'added' },
|
|
{ name: 'Heart', status: 'added' },
|
|
{ name: '"><script>', status: 'added' },
|
|
{ name: '../secret', status: 'changed' },
|
|
{ name: '', status: 'changed' },
|
|
],
|
|
});
|
|
assert.equal(markup.split('\n\n').length, 1);
|
|
assert.match(markup, /icon=heart"/);
|
|
assert.doesNotMatch(markup, /script|secret|Heart/);
|
|
});
|
|
|
|
it('returns an empty string without icons', () => {
|
|
assert.equal(renderIconPreview({ pr: 1, sha: SHA, icons: [] }), '');
|
|
assert.equal(
|
|
renderIconPreview({ pr: 1, sha: SHA, icons: [{ name: 'Not Valid', status: 'added' }] }),
|
|
'',
|
|
);
|
|
});
|
|
|
|
it(`caps the preview at ${MAX_ICONS} icons and links to the rest`, () => {
|
|
const icons = Array.from({ length: MAX_ICONS + 3 }, (_, index) => ({
|
|
name: `icon-${String(index).padStart(2, '0')}`,
|
|
status: 'added' as const,
|
|
}));
|
|
const markup = renderIconPreview({ pr: 7, sha: SHA, icons });
|
|
const blocks = markup.split('\n\n');
|
|
|
|
assert.equal(blocks.length, MAX_ICONS + 1);
|
|
assert.equal(blocks.filter((block) => block.startsWith('<a ')).length, MAX_ICONS);
|
|
assert.doesNotMatch(markup, /icon=icon-40"/);
|
|
assert.match(blocks.at(-1)!, /3 more icons/);
|
|
assert.match(blocks.at(-1)!, /\(https:\/\/review\.lucide\.dev\/pr\/7\)/);
|
|
});
|
|
|
|
it('does not add the overflow line at exactly the cap', () => {
|
|
const icons = Array.from({ length: MAX_ICONS }, (_, index) => ({
|
|
name: `icon-${index}`,
|
|
status: 'added' as const,
|
|
}));
|
|
const blocks = renderIconPreview({ pr: 7, sha: SHA, icons }).split('\n\n');
|
|
assert.equal(blocks.length, MAX_ICONS);
|
|
});
|
|
});
|
|
|
|
describe('replaceBetweenMarkers', () => {
|
|
const markup = 'MARKUP\nLINE 2';
|
|
|
|
it('replaces the content between the markers', () => {
|
|
const body = `Intro\n\n${startMarker}\n\nold preview\n\n${endMarker}\n\nOutro`;
|
|
assert.equal(
|
|
replaceBetweenMarkers(body, markup),
|
|
`Intro\n\n${startMarker}\n\n${markup}\n\n${endMarker}\n\nOutro`,
|
|
);
|
|
});
|
|
|
|
it('keeps the text around the markers byte for byte', () => {
|
|
const before = '## Description\n trailing spaces \n\t<!-- comment -->\n\n\n## Icon preview\n';
|
|
const after = '\n\n## Icon use cases \n- one\n\n\n';
|
|
const body = `${before}${startMarker}${endMarker}${after}`;
|
|
const result = replaceBetweenMarkers(body, markup);
|
|
|
|
assert.ok(result.startsWith(`${before}${startMarker}`));
|
|
assert.ok(result.endsWith(`${endMarker}${after}`));
|
|
});
|
|
|
|
it('keeps the markers with nothing between them for empty markup', () => {
|
|
const body = `Intro\n\n${startMarker}\n\nold preview\n\n${endMarker}\n\nOutro`;
|
|
assert.equal(
|
|
replaceBetweenMarkers(body, ''),
|
|
`Intro\n\n${startMarker}\n\n${endMarker}\n\nOutro`,
|
|
);
|
|
});
|
|
|
|
it('adds the markers at the start when they are missing', () => {
|
|
assert.equal(
|
|
replaceBetweenMarkers('Some description', markup),
|
|
`${startMarker}\n\n${markup}\n\n${endMarker}\n\nSome description`,
|
|
);
|
|
});
|
|
|
|
it('adds the markers to an empty body', () => {
|
|
assert.equal(replaceBetweenMarkers('', markup), `${startMarker}\n\n${markup}\n\n${endMarker}`);
|
|
});
|
|
|
|
it('throws when the end marker comes before the start marker', () => {
|
|
const body = `${endMarker}\n\n${startMarker}`;
|
|
assert.throws(() => replaceBetweenMarkers(body, markup), /comes before/);
|
|
});
|
|
|
|
const count = (text: string, search: string) => text.split(search).length - 1;
|
|
|
|
for (const [name, orphan] of [
|
|
['start', startMarker],
|
|
['end', endMarker],
|
|
]) {
|
|
it(`replaces a lone ${name} marker with a fresh pair`, () => {
|
|
const body = `Intro\n\n${orphan}\nOutro`;
|
|
const result = replaceBetweenMarkers(body, markup);
|
|
|
|
assert.equal(result, `${startMarker}\n\n${markup}\n\n${endMarker}\n\nIntro\n\nOutro`);
|
|
assert.equal(count(result, startMarker), 1);
|
|
assert.equal(count(result, endMarker), 1);
|
|
assert.equal(replaceBetweenMarkers(result, markup), result);
|
|
});
|
|
|
|
it(`removes a lone ${name} marker from a CRLF body`, () => {
|
|
const body = `Intro\r\n\r\n${orphan}\r\nOutro\r\n`;
|
|
const result = replaceBetweenMarkers(body, '');
|
|
|
|
assert.equal(result, `${startMarker}\r\n\r\n${endMarker}\r\n\r\nIntro\r\n\r\nOutro\r\n`);
|
|
assert.equal(count(result, startMarker), 1);
|
|
assert.equal(count(result, endMarker), 1);
|
|
assert.equal(replaceBetweenMarkers(result, ''), result);
|
|
});
|
|
|
|
it(`removes a lone ${name} marker on the last line`, () => {
|
|
const result = replaceBetweenMarkers(`Intro\n${orphan}`, markup);
|
|
|
|
assert.equal(result, `${startMarker}\n\n${markup}\n\n${endMarker}\n\nIntro`);
|
|
assert.equal(replaceBetweenMarkers(result, markup), result);
|
|
});
|
|
}
|
|
|
|
it('keeps CRLF line endings', () => {
|
|
const body = `Intro\r\n\r\n${startMarker}\r\n${endMarker}\r\n\r\nOutro\r\n`;
|
|
assert.equal(
|
|
replaceBetweenMarkers(body, markup),
|
|
`Intro\r\n\r\n${startMarker}\r\n\r\nMARKUP\r\nLINE 2\r\n\r\n${endMarker}\r\n\r\nOutro\r\n`,
|
|
);
|
|
});
|
|
|
|
it('is idempotent', () => {
|
|
const bodies = [
|
|
`Intro\n\n${startMarker}\n${endMarker}\nOutro`,
|
|
`Intro\r\n${startMarker}\r\n${endMarker}\r\nOutro`,
|
|
'No markers yet',
|
|
'',
|
|
];
|
|
for (const body of bodies) {
|
|
for (const content of [markup, '']) {
|
|
const once = replaceBetweenMarkers(body, content);
|
|
assert.equal(replaceBetweenMarkers(once, content), once);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('is idempotent with real preview markup', () => {
|
|
const preview = renderIconPreview({
|
|
pr: 1,
|
|
sha: SHA,
|
|
icons: [
|
|
{ name: 'heart', status: 'added' },
|
|
{ name: 'star', status: 'changed' },
|
|
],
|
|
});
|
|
const once = replaceBetweenMarkers('## Description\n\nText', preview);
|
|
assert.equal(replaceBetweenMarkers(once, preview), once);
|
|
});
|
|
});
|