语义发布 - 在变更日志中包含提交主题、正文和页脚

问题描述 投票:0回答:2

我正在尝试配置语义发布以包含CHANGELOG.md文件中提交消息的subjectbody

footer
,而不仅仅是提交消息的主题,但没有成功。在我的变更日志中,我仍然只有提交消息的简短主题。有没有办法以这种方式设置语义释放?

任何建议都会对我有帮助。

commit changelog semantic-release release-notes conventional-changelog
2个回答
0
投票

我试图查找有关此广告的一些问题,但只发现了这个 https://github.com/conventional-changelog/standard-version/issues/242


0
投票

这是可以做到的,但并不那么简单。关键是为

writerOpts
插件设置
@semantic-release/release-notes-generator

在以下用于配置语义发布的示例

release.config.js
文件中,我设置了两个选项:
preset
(您可以使用不同的预设)和
writerOpts
(重要部分)。

import { readFileSync } from 'node:fs'

const commitPartial = readFileSync('./changelog-template-commit.hbs', { encoding: 'utf-8' })

function finalizeContext (context) {
    for (const commitGroup of context.commitGroups) {
        for (const commit of commitGroup.commits) {
            commit.bodyLines = commit.body?.split('\n').filter((line) => line !== '') ?? []
        }
    }

    return context
}

export default {
    plugins: [
        '@semantic-release/commit-analyzer',
        ['@semantic-release/release-notes-generator', {
            preset: 'conventionalcommits',
            writerOpts: {
                commitPartial,
                finalizeContext,
            },
        }],
        '@semantic-release/changelog',
    ],
}

首先我们来看看

writerOpts.commitPartial
。我正在加载存储在
changelog-template-commit.hbs
文件中的修改后的模板:

{{!--
  Copy of https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-conventionalcommits/templates/commit.hbs

  The following have been replaced:

  - `commitUrlFormat` with `{{@root.host}}/{{@root.owner}}/{{@root.repository}}/commit/{{hash}})`
  - `issueUrlFormat` with `{{@root.host}}/{{@root.owner}}/{{@root.repository}}/issues/{{this.id}}`

  As they won't be replaced when overriding the commitPartial
--}}
*{{#if scope}} **{{scope}}:**
{{~/if}} {{#if subject}}
  {{~subject}}
{{~else}}
  {{~header}}
{{~/if}}

{{~!-- commit link --}}{{~#if hash}} {{#if @root.linkReferences~}}
  ([{{shortHash}}]({{@root.host}}/{{@root.owner}}/{{@root.repository}}/commit/{{hash}}))
{{~else}}
  {{~shortHash}}
{{~/if}}{{~/if}}

{{~!-- commit references --}}
{{~#if references~}}
  , closes
  {{~#each references}} {{#if @root.linkReferences~}}
    [
    {{~#if this.owner}}
      {{~this.owner}}/
    {{~/if}}
    {{~this.repository}}{{this.prefix}}{{this.issue}}]({{@root.host}}/{{@root.owner}}/{{@root.repository}}/issues/{{this.id}})
  {{~else}}
    {{~#if this.owner}}
      {{~this.owner}}/
    {{~/if}}
    {{~this.repository}}{{this.prefix}}{{this.issue}}
  {{~/if}}{{/each}}
{{~/if}}
{{!-- End of copy --}}

{{!-- Start of custom additions --}}
{{#each bodyLines}}

  {{this}}
{{/each}}{{#each notes}}
  **BREAKING CHANGE**: {{text}}
{{/each}}

这是常规提交预设使用的原始提交模板的稍微修改版本。我已经在文件中评论了我对其所做的更改。在底部,您将找到用于添加提交正文和我所做的重大更改注释的附加内容。

为此,需要上面配置中的

writerOpts.finalizeContext
选项。它获取每个提交的
body
并将其转换为逐行数组。从技术上讲,您可以在模板中使用
body
而不必担心这一点,但如果您有多条正文线,它不会正确缩进。我的版本已解决此问题。


这可能并不完美,我希望 convention-changelog (由

@semantic-release/release-notes-generator
使用)能够提供一些开箱即用的功能,但它对我有用。

请注意,我还没有完全考虑提交页脚中可能包含的所有信息,例如插件可能会在此处以不同方式处理的引用。您可能需要进行更多调整。

哦,是的,我强烈建议首先使用

npx semantic-release --dryRun
在本地尝试此操作。

© www.soinside.com 2019 - 2024. All rights reserved.