
Renovate Semantic Commits 配置指南自动检测、前缀规则与自定义实践【免费下载链接】renovateHome of the Renovate CLI: Cross-platform Dependency Automation by Mend.io项目地址: https://gitcode.com/GitHub_Trending/re/renovateRenovate 会根据仓库已有提交历史自动判断是否启用语义化提交Semantic Commits并为依赖更新生成chore(deps): update eslint to v7.30.0这类标准化的提交信息与 PR 标题。本文以 docs/usage/semantic-commits.md 为骨架结合 Renovate 源码中的检测算法、配置项与内置预设完整讲解其工作原理、默认行为以及手动启用、修改类型与作用域的实战方法帮助你让自动化依赖更新与团队的 Conventional Commits 规范无缝衔接。Semantic Commits 的自动检测机制Renovate 并不要求你在配置里显式声明仓库是否使用语义化提交默认情况下它会自己去看。当配置项semanticCommits处于默认值auto时Renovate 会检查基础分支base branch最近20 条提交信息据此判断该仓库是否在使用语义化提交。具体实现位于 lib/util/git/semantic.tsexport async function detectSemanticCommits(): PromiseDetectedSemanticCommit { // 先从仓库缓存读取避免重复检测 if (cache.semanticCommits) { return cache.semanticCommits; } const commitMessages await getCommitMessages(); const score detectSemanticCommitScore(commitMessages); // score 0 判定为 enabled否则 disabled }提交信息的获取来自 lib/util/git/index.ts 的getCommitMessages()它通过git log实现const res await git.log({ n: 20, // 最近 20 条 format: { message: %s }, --no-merges: null, // 忽略合并提交 });也就是说合并提交merge commits会被忽略不会参与判断这与文档描述一致。检测算法受 conventional-commits-detector 启发的评分制拿到 20 条提交信息后Renovate 采用一个受conventional-commits-detector包启发的评分算法逐条匹配 Angular 风格的正则表达式命中加 1 分未命中减 1 分最终分数大于 0 即判定仓库使用语义化提交。// lib/util/git/semantic.ts const angular regEx(/^(?:\w*)(?:\((?:.*)\))?!?: (?:.*)$/); return commitMessages.reduce((count, message) { if (angular.test(message)) { return count 1; } return count - 1; }, 0);从该正则可以看出 Renovate 的识别范围type: description例如fix: correct typotype(scope): description例如feat(api): add endpoint支持破坏性变更的!标记例如feat!: breaking change。需要特别说明Renovate 只能识别 Conventional Commits 风格含 Angular 风格并不能理解其他提交约定。如果仓库历史提交不是 conventional 风格自动检测就会判定为未启用语义化提交此时生成的提交信息不带chore(deps):前缀。自动检测的缓存行为检测结果会被写入仓库级缓存cache.semanticCommits同一次运行中后续逻辑直接读取缓存避免重复执行git log。这意味着首次检测后结果固定若你想改变仓库提交风格后再让 Renovate 重新判定最稳妥的做法是显式配置semanticCommits而非依赖自动检测。默认前缀与 config:recommended 的行为当 Renovate 判定仓库使用语义化提交或你手动启用后提交信息与 PR 标题会按如下格式生成chore(deps): update eslint to v7.30.0其中chore是默认的提交类型semanticCommitType默认值见 lib/config/options/index.tsdeps是默认的作用域semanticCommitScope默认值见 lib/config/options/index.ts。如果你继承了config:recommended预设Renovate 会对几乎所有更新使用chore前缀。原因是config:recommended内部引用了:semanticPrefixFixDepsChoreOthers预设见 lib/config/presets/internal/config.preset.ts而该预设的完整规则定义在 lib/config/presets/internal/default.preset.tssemanticPrefixFixDepsChoreOthers: { description: Use semantic commit type fix for dependencies and chore for all others if semantic commits are in use., packageRules: [ { matchPackageNames: [*], semanticCommitType: chore }, { matchDepTypes: [dependencies, require], semanticCommitType: fix }, { matchDatasources: [maven], matchDepTypes: [compile, provided, runtime, system, import, parent], semanticCommitType: fix, }, // poetry / pep621 的依赖类型也会用 fix // isLockfileUpdate 的锁文件更新仍用 chore ], },由此可以梳理出config:recommended下的例外规则当depType是已知的生产依赖类型如 npm 的dependencies、require时使用fix前缀当更新使用maven数据源且depType为compile、provided、runtime、system、import、parent等生产类型时使用fix前缀从源码还可以看到poetry/pep621管理的依赖project.dependencies、project.optional-dependencies、extras等同样适用fix前缀而锁文件更新isLockfileUpdate true这类非代码变更仍使用chore。从源码结构看这套依赖修复用 fix、其余用 chore的策略正是 Renovate 对生产依赖与非生产依赖的语义区分生产依赖升级视为需要进入变更日志的修复fix其余视为杂务chore。注意commitMessagePrefix 优先级更高需要特别留意如果配置了commitMessagePrefix语义化提交功能将不会生效。从 lib/workers/repository/model/commit-message-factory.ts 的源码可以看到message.prefix this._config.commitMessagePrefix ?? ; // 仅当未配置 commitMessagePrefix 且 semanticCommits 为 enabled 时 // 才会把 type/scope 组装成语义化前缀 !this._config.commitMessagePrefix this._config.semanticCommits enabled也就是说commitMessagePrefix会直接覆盖并优先于语义化前缀。若你同时配置了两者实际生效的是commitMessagePrefix。手动启用或禁用 Semantic Commits如果你不想依赖自动检测可以显式覆盖默认设置。强制启用语义化提交{ extends: [:semanticCommits] }强制关闭语义化提交{ extends: [:semanticCommitsDisabled] }这两个预设定义在 lib/config/presets/internal/default.preset.tssemanticCommits: { description: Use semantic prefixes for commit messages and PR titles., semanticCommits: enabled, }, semanticCommitsDisabled: { description: Disable semantic prefixes for commit messages and PR titles., semanticCommits: disabled, },对应的底层配置项semanticCommits定义在 lib/config/options/index.ts它是一个字符串枚举allowedValues为auto/enabled/disabled默认auto取值行为auto按基础分支最近 20 条提交自动检测默认enabled强制启用所有提交信息与 PR 标题使用语义化前缀disabled强制关闭不使用语义化前缀因此也可以绕过预设直接写semanticCommits: enabled效果与:semanticCommits相同。修改 Semantic Commit 类型默认的提交类型是chore但你可以通过:semanticCommitTypeAll预设为每一个 PR指定统一类型。让所有 PR 使用chore类型{ extends: [:semanticCommitTypeAll(chore)] }此时 PR 标题与提交信息均以chore(deps):开头。让所有 PR 使用ci类型{ extends: [:semanticCommitTypeAll(ci)] }此时 PR 标题与提交信息均以ci(deps):开头。从源码看:semanticCommitTypeAll的实现是借助packageRules通配所有文件实现的lib/config/presets/internal/default.preset.tssemanticCommitTypeAll: { description: If Renovate detects semantic commits, it will use semantic commit type {{arg0}} for all commits., packageRules: [ { matchFileNames: [**/*], semanticCommitType: {{arg0}}, }, ], },注意其描述中的条件If Renovate detects semantic commits——即它只在语义化提交处于启用状态时生效。除了semanticCommitTypeAll仓库还提供了几个相关的内置预设lib/config/presets/internal/default.preset.ts可按需选用:semanticCommitType(chore)/:semanticCommitType(fix)将提交类型统一为指定值semanticCommitType的底层实现:semanticPrefixChore等价于extends: [:semanticCommitType(chore)]:semanticPrefixFix等价于extends: [:semanticCommitType(fix)]:semanticPrefixFixDepsChoreOthers依赖更新用fix、其余用chore即config:recommended所引用的规则。底层配置项semanticCommitType定义在 lib/config/options/index.ts默认值为chore并支持模板supportsTemplating: true因此理论上也可以使用handlebars模板动态生成类型。修改 Semantic Commit 作用域默认作用域是depschore(deps):你可以换成自己的词。将作用域改为package{ extends: [:semanticCommitScope(package)] }此时生成的提交信息形如chore(package): update ...。移除作用域{ extends: [:semanticCommitScopeDisabled] }此时 Renovate 生成chore:而非chore(deps):。对应的预设定义在 lib/config/presets/internal/default.preset.tssemanticCommitScope: { description: Use semantic commit scope {{arg0}} for all commits and PR titles., semanticCommitScope: {{arg0}}, }, semanticCommitScopeDisabled: { description: Disable semantic commit scope for all commits and PR titles., semanticCommitScope: null, },底层配置项semanticCommitScope定义在 lib/config/options/index.ts默认deps同样支持模板。注意:semanticCommitScopeDisabled通过将值设为null来删除作用域这与直接留空的效果不同。提交信息的完整组装流程理解上述配置后可以看一下最终提交信息是如何拼装出来的。Renovate 在 lib/workers/repository/model/commit-message-factory.ts 中把commitMessagePrefix、semanticCommitType、semanticCommitScope等信息组合成语义化提交消息而 lib/workers/repository/model/semantic-commit-message.ts 定义了其解析正则与 Conventional Commits v1.0.0 摘要格式对应type[optional scope]: descriptionprivate static readonly REGEXP regEx( /^(?type[\w])(?:\((?scope[\w-])\))?(?breaking!)?: (?:(?issue(?:[A-Z]-|#)[\d]) )?(?description.*)/, );该正则还额外支持解析breaking标记与#123/ABC-123形式的 issue 引用说明 Renovate 对语义化提交的处理不止是拼前缀还会在需要时解析已有消息的 type、scope 与 subject。常见配置速查以下是一份可直接复制的完整配置示例覆盖本文涉及的主要场景{ extends: [ config:recommended, :semanticCommits, :semanticCommitTypeAll(chore), :semanticCommitScope(package) ] }配置项一览配置 / 预设作用默认值semanticCommitsauto/enabled/disabledautosemanticCommitType提交类型choresemanticCommitScope提交作用域deps:semanticCommits/:semanticCommitsDisabled启用 / 关闭语义化提交—:semanticCommitTypeAll(x)所有 PR 统一使用类型x—:semanticCommitType(x)将类型统一为x—:semanticCommitScope(x)将作用域设为x—:semanticCommitScopeDisabled移除作用域chore:—:semanticPrefixFixDepsChoreOthers依赖用fix、其余用choreconfig:recommended已内置最后提醒两点自动检测有前提它只扫描基础分支最近 20 条非合并提交且只识别 Conventional Commits 风格。仓库历史若混用多种提交风格检测结果可能不符合预期建议关键仓库显式配置semanticCommitscommitMessagePrefix会压制语义化前缀两者同时配置时commitMessagePrefix优先生效语义化提交功能不会生效排障时优先检查这一点。【免费下载链接】renovateHome of the Renovate CLI: Cross-platform Dependency Automation by Mend.io项目地址: https://gitcode.com/GitHub_Trending/re/renovate创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考