Search code examples
angularhandlebars.jschangelog

Conventional changelog custom template - no line return if no closes specified


I am using conventional changelog (https://github.com/conventional-changelog/conventional-changelog) to generate a changelog based on commits, within an Angular app.

I Work with bitbucket, and so the default template won't work. So I used the custome template feature. My problem is the line return are not generated. I don't know if the issue comes from my template, my config or the default conventional-changelog, including <br/> or double space won't work.

so here is an exemple output

* **scope1:** Commit1 ([9753112](https://MY-BITBUCK-URL-PROJECT/commits/9753112bd59772c4a803547248a9b248523e7f42))    * **scope2:** Commit2 ([f3c3b28](https://MY-BITBUCK-URL-PROJECT/commits/f3c3b28a9634d5c2f83625db4b11eaa11b35bf94)), closes        #662
* **scope1:** Commit3 ([5de5b6b](https://MY-BITBUCK-URL-PROJECT/commits/5de5b6bfbee68410a38d34bc5c2e60b3e291556d)), closes        #637

As you can see, commit2 is not placed on a new line, though commit3 is because, commit2 closes an issue

Any help is welcome

Here is the commit.hbs template I use

*{{#if scope}} **{{scope}}:**
{{~/if}} {{#if subject}}
    {{~subject}}
{{~else}}
    {{~header}}
{{~/if}}

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

{{~!-- commit references --}}
{{~#if references~}}
    , closes
    {{~#each references}}
        #{{this.issue}}
    {{/each}}
{{~else}}
    <br/> <===== THIS IS NOT WORKING
{{~/if}}

And here is the config file :

const compareFunc = require(`compare-func`);
const Q = require('q');
const readFile = Q.denodeify(require('fs').readFile);
const resolve = require('path').resolve;

const writerOpts = {
  transform: (commit, context) => {
    const issues = [];

    commit.notes.forEach(note => {
      note.title = `BREAKING CHANGES`;
    });

    if (commit.type === `feat`) {
      commit.type = `Features`;
    } else if (commit.type === `fix`) {
      commit.type = `Bug Fixes`;
    } else if (commit.type === `perf`) {
      commit.type = `Performance Improvements`;
    } else if (commit.type === `revert`) {
      commit.type = `Reverts`;
    } else if (commit.type === `docs`) {
      commit.type = `Documentation`;
    } else if (commit.type === `style`) {
      commit.type = `Styles`;
    } else if (commit.type === `refactor`) {
      commit.type = `Code Refactoring`;
    } else if (commit.type === `test`) {
      commit.type = `Tests`;
    } else if (commit.type === `build`) {
      commit.type = `Build System`;
    } else if (commit.type === `chore`) {
      commit.type = `Maintenance`;
    } else if (commit.type === `ci`) {
      commit.type = `Continuous Integration`;
    } else {
      return;
    }

    if (commit.scope === `*`) {
      commit.scope = ``;
    }

    if (typeof commit.hash === `string`) {
      commit.shortHash = commit.hash.substring(0, 7);
    }

    if (typeof commit.subject === `string`) {
      let url = context.repository ? `${context.host}/${context.owner}/${context.repository}` : context.repoUrl;
      if (url) {
        url = `${url}/issues/`;
        // Issue URLs.
        commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
          issues.push(issue);
          return `[#${issue}](${url}${issue})`;
        });
      }
      if (context.host) {
        // User URLs.
        commit.subject = commit.subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, username) => {
          if (username.includes('/')) {
            return `@${username}`;
          }

          return `[@${username}](${context.host}/${username})`;
        });
      }
    }

    // remove references that already appear in the subject
    commit.references = commit.references.filter(reference => {
      return issues.indexOf(reference.issue) === -1;
    });
    return commit;
  },
  groupBy: `type`,
  commitGroupsSort: `title`,
  commitsSort: [`scope`, `subject`],
  noteGroupsSort: `title`,
  notesSort: compareFunc
};

Q.all([
  readFile(resolve(__dirname, 'changelog-templates/template.hbs'), 'utf-8'),
  readFile(resolve(__dirname, 'changelog-templates/header.hbs'), 'utf-8'),
  readFile(resolve(__dirname, 'changelog-templates/commit.hbs'), 'utf-8'),
  readFile(resolve(__dirname, 'changelog-templates/footer.hbs'), 'utf-8')
]).spread(function(template, header, commit, footer) {
  writerOpts.mainTemplate = template;
  writerOpts.headerPartial = header;
  writerOpts.commitPartial = commit;
  writerOpts.footerPartial = footer;
});

module.exports = {
  writerOpts: writerOpts
};


Solution

  • I found a way to fix this. It was a template Issue. I added at the end

    {{#unless references}}
    
    {{/unless}}
    

    Whole file:

    *{{#if scope}} **{{scope}}:**
    {{~/if}} {{#if subject}}
        {{~subject}}
    {{~else}}
        {{~header}}
    {{~/if}}
    
    {{~!-- commit link --}} {{#if @root.linkReferences~}}
        ([{{shortHash}}](
        {{~#if @root.repository}}
            {{~#if @root.host}}
                {{~@root.host}}/
            {{~/if}}
            {{~#if @root.owner}}
                {{~@root.owner}}/
            {{~/if}}
            {{~@root.repository}}
        {{~else}}
            {{~@root.repoUrl}}
        {{~/if}}/
        {{~@root.commit}}/{{hash}}))
    {{~else}}
        {{~shortHash}}
    {{~/if}}
    
    {{~!-- commit references --}}
    {{~#if references~}}
        , closes
        {{~#each references}}
            #{{this.issue}}
        {{/each}}
    {{~/if}}
    {{#unless references}}
    
    {{/unless}}