Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom writerOpts transform #188

Closed
asibilia opened this issue Oct 1, 2020 · 0 comments
Closed

Custom writerOpts transform #188

asibilia opened this issue Oct 1, 2020 · 0 comments

Comments

@asibilia
Copy link

asibilia commented Oct 1, 2020

I'm trying to write a custom writerOpts.transform to add Jira project links to our change logs. I've tried forking the conventional-changelog/conventional-changelog-angular preset, updating the transform there, and then including my custom conventional changelog preset like so:

// package.json
"release": {
    "branches": [
      "master"
    ],
    "plugins": [
      "@semantic-release/commit-analyzer",
      [
        "@semantic-release/release-notes-generator",
        {
          "config": "@my-team/conventional-changelog-angular-jira", <<<<  custom-npm-package
        }
      ],
      "@semantic-release/changelog",
      "@semantic-release/git",
      [
        "@semantic-release/exec",
       ]
    ],
    "preset": "angular" <<<<  UPDATE...REMOVE_THIS
  }

I've also tried pulling just the transform function out into its own file and referencing it in the writerOpts field like so:

// package.json
"release": {
    "branches": [
      "master"
    ],
    "plugins": [
      "@semantic-release/commit-analyzer",
      [
        "@semantic-release/release-notes-generator",
        {
          "writerOpts": "./writer-opts.js"
        }
      ],
      "@semantic-release/changelog",
      "@semantic-release/git",
      [
        "@semantic-release/exec",
      ]
    ]
  }
// writer-opts.js
const compareFunc = require('compare-func')

module.exports = {
    transform: (commit, context) => {
        let discard = true
        const issues = []

        commit.notes.forEach((note) => {
            note.title = 'BREAKING CHANGES'
            discard = false
        })

        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.revert) {
            commit.type = 'Reverts'
        } else if (discard) {
            return
        } 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 === 'ci') {
            commit.type = 'Continuous Integration'
        }

        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})`
                    }
                )

                // JIRA Ticket
                commit.subject = commit.subject.replace(
                    /([a-zA-Z]+)-(\d+)/,
                    (match, project, number) => {
                        return `[${match}](https://cadence.atlassian.net/browse/${project}-${number})`
                    }
                )
            }
            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) => {
            if (issues.indexOf(reference.issue) === -1) {
                return true
            }

            return false
        })

        return commit
    },
    groupBy: 'type',
    commitGroupsSort: 'title',
    commitsSort: ['scope', 'subject'],
    noteGroupsSort: 'title',
    notesSort: compareFunc,
}

Neither of these approaches seem to work. Any thoughts on how best to achieve this would be much appreciated!

UPDATE

For anyone finding this, I got it working after some more experimenting. The custom preset library I made does work but I had to remove "preset": "angular" from my package.json. In case anyone is curious, pointing directly at a file containing the writerOpts.transform method did not work.

@asibilia asibilia closed this as completed Oct 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant