feat(ci): auto-generate PR titles from added file paths (#4790)

* feat(ci): auto-generate PR titles from added file paths

* refactor(ci): single source of truth for conventional commit types

* ci: skip PR title generation when "Keep PR title" label is present

Co-authored-by: ericfennis <11825403+ericfennis@users.noreply.github.com>

* refactor(ci): align package path scopes with package.json names

* Apply changes to generated pr titles

* Add .github/conventional-commit-types.json to gitignore

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ericfennis <11825403+ericfennis@users.noreply.github.com>
Co-authored-by: Eric Fennis <eric.fennis@gmail.com>
This commit is contained in:
Copilot
2026-10-15 17:17:01 +02:00
committed by GitHub
co-authored by ericfennis copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Eric Fennis
parent 20fa6adf52
commit 55e223a3a1
7 changed files with 519 additions and 36 deletions
+12
View File
@@ -0,0 +1,12 @@
[
"fix",
"feat",
"perf",
"refactor",
"test",
"style",
"docs",
"ci",
"build",
"chore"
]
+100
View File
@@ -0,0 +1,100 @@
name: PR title
on:
pull_request_target:
types:
- opened
- reopened
- synchronize
- edited
permissions: {}
concurrency:
group: pr-title-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
generate-pr-title:
name: Generate PR Title
# Skip when a maintainer has opted out by adding the "Keep PR title" label,
# and on manual edits so they are not reverted straight away.
if: github.repository == 'lucide-icons/lucide' &&
github.event.action != 'edited' &&
!contains(github.event.pull_request.labels.*.name, 'Keep PR title')
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write # Required to edit the PR title
steps:
# We check out the main branch of the main repository for security reasons.
# The script only reads its input from the environment, so it never runs
# any code from the (untrusted) pull request.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: lucide-icons/lucide
fetch-depth: 0
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version-file: 'package.json'
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6
- name: Generate PR title
id: generate
run: |
NEW_TITLE="$(node ./scripts/generatePullRequestTitle.mts)"
{
echo "new_title<<PR_TITLE_EOF"
echo "$NEW_TITLE"
echo "PR_TITLE_EOF"
} >> "$GITHUB_OUTPUT"
env:
ADDED_FILES: ${{ steps.changed-files.outputs.added_files }}
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_and_modified_files }}
CURRENT_TITLE: ${{ github.event.pull_request.title }}
- name: Update PR title
if: steps.generate.outputs.new_title != ''
run: gh pr edit "$PR_NUMBER" --title "$NEW_TITLE"
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
NEW_TITLE: ${{ steps.generate.outputs.new_title }}
# Runs after the title is generated: an edit made with `GITHUB_TOKEN` does not
# trigger new workflow runs, so a separate workflow would lint the old title.
# The lint action fetches the current title through the API.
lint-pr-title:
name: PR Title Lint
needs: generate-pr-title
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read # Required to read the PR title
steps:
# Checks out the base branch, so the allowed types can't be changed by the PR.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Read conventional commit types
id: commit-types
run: |
{
echo "types<<TYPES_EOF"
jq -r '.[]' .github/conventional-commit-types.json
echo "TYPES_EOF"
} >> "$GITHUB_OUTPUT"
- uses: amannn/action-semantic-pull-request@48f256284bd46cdaab1048c3721360e808335d50 # v6.1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
types: ${{ steps.commit-types.outputs.types }}
requireScope: true
ignoreLabels: |
bot
-36
View File
@@ -1,36 +0,0 @@
name: Linting PR title
on:
pull_request:
types:
- opened
- edited
- reopened
permissions:
contents: read
pull-requests: read # Required to read the PR title
jobs:
lint-pr-title:
name: PR Title Lint
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@48f256284bd46cdaab1048c3721360e808335d50 # v6.1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
types: |
fix
feat
perf
refactor
test
style
docs
ci
build
chore
requireScope: true
ignoreLabels: |
bot
+1
View File
@@ -1,4 +1,5 @@
pnpm-lock.yaml
.github/conventional-commit-types.json
# docs examples
docs/**/examples/
+1
View File
@@ -11,6 +11,7 @@
"test:integrations:vue": "pnpm --filter @lucide/vue build && pnpm -r --workspace-concurrency=1 --filter './integrations/lucide-vue/**' check",
"test:integrations:svelte": "pnpm --filter @lucide/svelte build && pnpm -r --workspace-concurrency=1 --filter './integrations/lucide-svelte/**' check",
"test:font": "pnpm --filter build-font test",
"test:scripts": "node --test scripts/*.test.mts",
"lucide": "pnpm --filter lucide",
"lucide-angular": "pnpm --filter lucide-angular",
"lucide-react": "pnpm --filter lucide-react",
+245
View File
@@ -0,0 +1,245 @@
/**
* Generates a conventional-commit PR title from the paths a pull request changes.
*
* Because merges in this repository are squashed, the PR title becomes the
* commit message and the changelog entry. This script generates the whole title
* for icon contributions and, for every other kind of PR, inserts or updates the
* conventional-commit scope so `lint-pr-title` is satisfied.
*
* It follows the convention of `generateChangedIconsCommentMarkup.mts`: it reads
* its input from the environment, prints the result to stdout and has no other
* side effects. It only imports Node built-ins and the shared
* `conventional-commit-types.json` (a native JSON import), so Node can still run
* it natively as an `.mts` file without a `pnpm install` step.
*
* Env input (CLI wrapper at the bottom):
* - `ADDED_FILES_FILE`: path to a newline-separated list of added files, or
* - `ADDED_FILES`: a space-separated list of added files (handy for local runs).
* - `CHANGED_FILES_FILE` / `CHANGED_FILES`: the same for all changed files
* (added, modified, renamed and deleted).
* - `CURRENT_TITLE`: the current PR title.
*
* Output: the new title on stdout, or nothing at all when no change is needed.
*/
import { readFileSync } from 'node:fs';
// Single source of truth for the conventional-commit types, shared with
// `lint-pr-title` (amannn/action-semantic-pull-request).
import VALID_TYPES from '../.github/conventional-commit-types.json' with { type: 'json' };
// Default type used in prefix mode when the current title has no valid type.
// The path reliably tells us the scope but nothing about whether a change is a
// feat/fix/chore, so we fall back to the most neutral type.
const DEFAULT_TYPE = 'chore';
// How many icon names to list in a generated title before summarising the rest.
const MAX_NAMES = 3;
// How many package scopes to join with `|` before falling back to `packages`.
const MAX_SCOPES = 3;
const PACKAGES_SCOPE = 'packages';
/**
* - `icon`: an added file *is* the subject, so the whole title is generated.
* Changed (not added) icon files only provide a scope.
* - `package`: a package in `packages/`, scoped by its `package.json` name.
* - `repo`: any other area of the repository.
*/
type Kind = 'icon' | 'package' | 'repo';
type Rule = {
pattern: RegExp;
scope: string;
kind: Kind;
};
/**
* Path -> scope rules. Each file is matched by the *first* rule in this list
* that matches it, and scopes are listed in the order of this list, which keeps
* the outcome deterministic. The `icons`, `lab`, `metadata`, `docs` and `site`
* scopes mirror `.github/labeler.yml`; `packages/*` scopes mirror each
* package's `package.json` name.
*/
const RULES: Rule[] = [
{ pattern: /^icons\/[^/]+\.svg$/, scope: 'icons', kind: 'icon' },
{ pattern: /^lab\/[^/]+\.svg$/, scope: 'lab', kind: 'icon' },
{ pattern: /^packages\/lucide-react-native\//, scope: 'lucide-react-native', kind: 'package' },
{ pattern: /^packages\/lucide-react\//, scope: 'lucide-react', kind: 'package' },
{ pattern: /^packages\/lucide-preact\//, scope: 'lucide-preact', kind: 'package' },
{ pattern: /^packages\/lucide-solid\//, scope: 'lucide-solid', kind: 'package' },
{ pattern: /^packages\/lucide-static\//, scope: 'lucide-static', kind: 'package' },
{ pattern: /^packages\/icons\//, scope: '@lucide/icons', kind: 'package' },
{ pattern: /^packages\/vue\//, scope: '@lucide/vue', kind: 'package' },
{ pattern: /^packages\/angular\//, scope: '@lucide/angular', kind: 'package' },
{ pattern: /^packages\/svelte\//, scope: '@lucide/svelte', kind: 'package' },
{ pattern: /^packages\/solid\//, scope: '@lucide/solid', kind: 'package' },
{ pattern: /^packages\/astro\//, scope: '@lucide/astro', kind: 'package' },
{ pattern: /^packages\/lab\//, scope: '@lucide/lab', kind: 'package' },
{ pattern: /^packages\/shared\//, scope: '@lucide/shared', kind: 'package' },
{ pattern: /^packages\/lucide\//, scope: 'lucide', kind: 'package' },
{ pattern: /^(?:icons|categories)\/[^/]+\.json$/, scope: 'metadata', kind: 'repo' },
{ pattern: /^docs\/.*\.md$/, scope: 'docs', kind: 'repo' },
{ pattern: /^docs\//, scope: 'site', kind: 'repo' },
];
/**
* Every scope this script can produce. A title whose scope consists only of
* these is owned by the script and may be updated; any other scope was chosen
* by a person and is kept.
*/
const GENERATED_SCOPES = new Set([...RULES.map((rule) => rule.scope), PACKAGES_SCOPE]);
/**
* Formats a list of icon names into the subject of a generated title, e.g.
* `` `heart` icon ``, `` `heart`, `heart-off` icons `` or
* `` `a`, `b`, `c` and 2 more icons ``.
*/
const formatIconNames = (names: string[]): string => {
const quoted = names.map((name) => `\`${name}\``);
const noun = names.length === 1 ? 'icon' : 'icons';
if (quoted.length <= MAX_NAMES) {
return `${quoted.join(', ')} ${noun}`;
}
const shown = quoted.slice(0, MAX_NAMES).join(', ');
const remaining = quoted.length - MAX_NAMES;
return `${shown} and ${remaining} more ${noun}`;
};
/**
* Builds the whole title for an icon rule from the added files matching it.
* Names are deduped by basename (an icon PR adds both `heart.svg` and
* `heart.json`) and kept in the order they first appear.
*/
const generateTitle = (scope: string, matchedFiles: string[]): string => {
const names = matchedFiles
.map((file) => file.replace(/^.*\//, '').replace(/\.[^.]+$/, ''))
.filter((name, index, all) => all.indexOf(name) === index);
return `feat(${scope}): added ${formatIconNames(names)}`;
};
/**
* Derives the scope from the changed files. Package scopes win over icon
* scopes, which win over any other scope (an icon change usually touches its
* metadata too). Multiple scopes are joined with `|`, e.g.
* `lucide-react|@lucide/vue`, and more than `MAX_SCOPES` packages are
* summarised as `packages`.
*/
const resolveScope = (changedFiles: string[]): string | null => {
const matchedRules = new Set(
changedFiles
.map((file) => RULES.find((rule) => rule.pattern.test(file)))
.filter((rule) => rule != null),
);
const scopesOf = (kind: Kind) =>
RULES.filter((rule) => rule.kind === kind && matchedRules.has(rule)).map((rule) => rule.scope);
const packageScopes = scopesOf('package');
if (packageScopes.length > MAX_SCOPES) return PACKAGES_SCOPE;
if (packageScopes.length > 0) return packageScopes.join('|');
for (const kind of ['icon', 'repo'] as const) {
const scopes = scopesOf(kind);
if (scopes.length > 0) return scopes.join('|');
}
return null;
};
const isGeneratedScope = (scope: string): boolean =>
scope.split('|').every((part) => GENERATED_SCOPES.has(part.trim()));
/**
* Rewrites the current title around the resolved scope: the contributor's own
* type and subject are preserved and the title is normalised to
* `type(scope)!: subject`. A scope chosen by a person is kept; a missing scope or
* one this script generated earlier is replaced by `scope`. When the title has
* no valid type, `DEFAULT_TYPE` is used. When there is no subject or no scope to
* work with, `null` is returned so the title is left untouched.
*/
const prefixTitle = (scope: string | null, currentTitle: string): string | null => {
const match = currentTitle.match(/^\s*(\w+)\s*(?:\(([^)]*)\))?\s*(!?)\s*:\s*(.*)$/);
const type = match?.[1].toLowerCase();
if (match && type != null && VALID_TYPES.includes(type)) {
const [, , currentScope = '', breaking, subject] = match;
if (subject.trim() === '') return null;
const keepScope =
currentScope.trim() !== '' && (scope == null || !isGeneratedScope(currentScope));
const newScope = keepScope ? currentScope.trim() : scope;
if (newScope == null) return null;
return `${type}(${newScope})${breaking}: ${subject.trim()}`;
}
const subject = currentTitle.trim();
if (scope == null || subject === '') return null;
return `${DEFAULT_TYPE}(${scope}): ${subject}`;
};
type PullRequestFiles = {
addedFiles: string[];
changedFiles: string[];
};
/**
* Pure title function: given the added and changed files and the current
* title, returns the new title, or `null` when nothing should change (no
* matching rule, no subject to work with, or the title is already correct).
*/
export const generatePullRequestTitle = (
{ addedFiles, changedFiles }: PullRequestFiles,
currentTitle: string,
): string | null => {
const iconRule = RULES.find(
(rule) => rule.kind === 'icon' && addedFiles.some((file) => rule.pattern.test(file)),
);
// Added files are changed files too, also when only `ADDED_FILES` is given.
const allChangedFiles = [...new Set([...addedFiles, ...changedFiles])];
const newTitle =
iconRule != null
? generateTitle(
iconRule.scope,
addedFiles.filter((file) => iconRule.pattern.test(file)),
)
: prefixTitle(resolveScope(allChangedFiles), currentTitle);
if (newTitle == null || newTitle === currentTitle) return null;
return newTitle;
};
// --- CLI wrapper -----------------------------------------------------------
// Only runs when executed directly, keeping the function above unit-testable.
const readFileList = (filePath: string | undefined, list: string | undefined): string[] => {
const raw = filePath != null && filePath !== '' ? readFileSync(filePath, 'utf-8') : (list ?? '');
return raw
.split(/\s+/)
.map((file) => file.trim())
.filter((file) => file !== '');
};
if (import.meta.main) {
const { ADDED_FILES_FILE, ADDED_FILES, CHANGED_FILES_FILE, CHANGED_FILES, CURRENT_TITLE } =
process.env;
const newTitle = generatePullRequestTitle(
{
addedFiles: readFileList(ADDED_FILES_FILE, ADDED_FILES),
changedFiles: readFileList(CHANGED_FILES_FILE, CHANGED_FILES),
},
CURRENT_TITLE ?? '',
);
if (newTitle != null) {
process.stdout.write(newTitle);
}
}
+160
View File
@@ -0,0 +1,160 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { generatePullRequestTitle } from './generatePullRequestTitle.mts';
const added = (addedFiles: string[], title: string) =>
generatePullRequestTitle({ addedFiles, changedFiles: [] }, title);
const changed = (changedFiles: string[], title: string) =>
generatePullRequestTitle({ addedFiles: [], changedFiles }, title);
describe('generatePullRequestTitle', () => {
describe('icon titles', () => {
it('generates a title for a single icon and dedupes svg and json', () => {
assert.equal(
added(['icons/heart.svg', 'icons/heart.json'], 'add heart'),
'feat(icons): added `heart` icon',
);
});
it('lists multiple icons', () => {
assert.equal(
added(['icons/heart.svg', 'icons/heart-off.svg'], 'icons'),
'feat(icons): added `heart`, `heart-off` icons',
);
});
it('summarises many icons', () => {
assert.equal(
added(['icons/a.svg', 'icons/b.svg', 'icons/c.svg', 'icons/d.svg', 'icons/e.svg'], 'x'),
'feat(icons): added `a`, `b`, `c` and 2 more icons',
);
});
it('generates a title for lab icons', () => {
assert.equal(added(['lab/ghost.svg'], 'x'), 'feat(lab): added `ghost` icon');
});
it('returns null when the title is already correct', () => {
assert.equal(added(['icons/heart.svg'], 'feat(icons): added `heart` icon'), null);
});
it('ignores other changed files when an icon is added', () => {
assert.equal(
generatePullRequestTitle(
{ addedFiles: ['icons/heart.svg'], changedFiles: ['docs/guide.md'] },
'x',
),
'feat(icons): added `heart` icon',
);
});
});
describe('scope resolution', () => {
it('uses the package name as scope', () => {
assert.equal(
changed(['packages/lucide-react/src/a.ts'], 'fix: crash'),
'fix(lucide-react): crash',
);
});
it('joins multiple packages with a pipe', () => {
assert.equal(
changed(['packages/vue/src/b.ts', 'packages/lucide-react/src/a.ts'], 'chore: Fix export'),
'chore(lucide-react|@lucide/vue): Fix export',
);
});
it('summarises more than three packages as `packages`', () => {
assert.equal(
changed(
[
'packages/lucide-react/a.ts',
'packages/vue/a.ts',
'packages/svelte/a.ts',
'packages/angular/a.ts',
],
'refactor: shared types',
),
'refactor(packages): shared types',
);
});
it('prefers packages over docs', () => {
assert.equal(
changed(['docs/guide/react.md', 'packages/lucide-react/src/a.ts'], 'feat: new prop'),
'feat(lucide-react): new prop',
);
});
it('joins docs and site scopes', () => {
assert.equal(
changed(['docs/guide/a.md', 'docs/.vitepress/theme/b.vue'], 'docs: update'),
'docs(docs|site): update',
);
});
it('uses `metadata` for icon and category json files', () => {
assert.equal(changed(['icons/heart.json'], 'fix: tags'), 'fix(metadata): tags');
assert.equal(changed(['categories/shapes.json'], 'fix: category'), 'fix(metadata): category');
});
it('prefers icons over metadata for changed icons', () => {
assert.equal(
changed(['icons/heart.svg', 'icons/heart.json'], 'fix: heart'),
'fix(icons): heart',
);
});
it('supports @lucide/solid', () => {
assert.equal(changed(['packages/solid/src/a.ts'], 'feat: x'), 'feat(@lucide/solid): x');
});
it('does not match lucide-react for lucide-react-native files', () => {
assert.equal(
changed(['packages/lucide-react-native/src/a.ts'], 'fix: x'),
'fix(lucide-react-native): x',
);
});
it('returns null when no rule matches', () => {
assert.equal(changed(['README.md'], 'chore: readme'), null);
});
});
describe('prefix titles', () => {
const files = ['packages/lucide-react/src/a.ts'];
it('keeps a custom scope', () => {
assert.equal(changed(files, 'fix(react): crash'), null);
});
it('extends a generated scope', () => {
assert.equal(
changed([...files, 'packages/vue/src/a.ts'], 'fix(lucide-react): crash'),
'fix(lucide-react|@lucide/vue): crash',
);
});
it('normalises the type casing', () => {
assert.equal(changed(files, 'Fix: crash'), 'fix(lucide-react): crash');
});
it('normalises whitespace and keeps a custom scope', () => {
assert.equal(changed(files, 'feat (react): x'), 'feat(react): x');
});
it('keeps the breaking-change marker', () => {
assert.equal(changed(files, 'feat!: drop old api'), 'feat(lucide-react)!: drop old api');
});
it('returns null for an empty subject', () => {
assert.equal(changed(files, 'fix:'), null);
});
it('falls back to the default type', () => {
assert.equal(changed(files, 'Fix crash'), 'chore(lucide-react): Fix crash');
});
});
});