← 中文资源

简体中文版本

由提交信息生成变更日志

把提交信息按类型归入有序的变更日志小节,并把破坏性变更单独收集成一份列表。

阅读英文原文

任务

按固定的小节顺序聚合提交主题,未知或低信号类型归入 chore,破坏性变更单独列出。

契约

  • 输入:JSON 对象:{ commits: [{ type, scope?, subject, breaking? }] }
  • 输出:JSON 对象:{ sections: [{ type, entries }], breaking: string[] }

样例

  1. 输入 {"commits":[{"type":"fix","scope":"api","subject":"handle empty body"},{"type":"feat","subject":"add webhooks"},{"type":"feat","scope":"api","subject":"rename tenant header","breaking":true}]} -> 期望 {"sections":[{"type":"feat","entries":["add webhooks","api: rename tenant header"]},{"type":"fix","entries":["api: handle empty body"]}],"breaking":["rename tenant header"]}。feat 排在小节顺序上先于 fix,同小节内条目按序排列,破坏性变更单独列出。
  2. 输入 {"commits":[{"type":"chore","subject":"bump dev deps"}]} -> 期望 {"sections":[{"type":"chore","entries":["bump dev deps"]}],"breaking":[]}。未知或低信号类型仍然会出现在 chore 小节里。

参考实现

function solve({ commits }) {
      const sections = { feat: [], fix: [], perf: [], docs: [], chore: [] };
      for (const commit of commits) {
        const key = Object.prototype.hasOwnProperty.call(sections, commit.type) ? commit.type : 'chore';
        sections[key].push((commit.scope ? commit.scope + ': ' : '') + commit.subject);
      }
      const order = ['feat', 'fix', 'perf', 'docs', 'chore'];
      return {
        sections: order
          .filter((type) => sections[type].length)
          .map((type) => ({ type, entries: sections[type].sort() })),
        breaking: commits.filter((commit) => commit.breaking).map((commit) => commit.subject).sort(),
      };
    }

为什么有效

手工拼装的发布说明会逐渐偏离历史事实。由提交类型推导变更日志,既能让使用方知情,也让破坏性变更不可能被遗忘。

小节顺序固定、条目排序输出,这样变更日志的 diff 才保持可评审。