Zhengqbbb / cz-git

cz-git | czg 🛠️ DX first and more engineered, lightweight, customizable, standard output format Commitizen adapter and CLI
https://cz-git.qbb.sh
MIT License
1.13k stars 41 forks source link

[Feature Request] Add branch number to commit name #182

Closed Ibochkarev closed 2 months ago

Ibochkarev commented 2 months ago

💭 Describe the feature

Thanks for the development of the project!

I need to add the branch name to the commit name. Is it possible to automate the process of inserting a branch name into a commit by introducing a new parameter.

Example of a commit name after adding feat(infrastructure) functionality: [PREMPROD-30287] Refine git branch detection by mask

Thanks in advance!

💡 Proposed Solution

No response

Zhengqbbb commented 2 months ago

Sorry for the delayed response.
First, this commit message does not comply with the Conventional Commits specification, so it will not be included as a feature.


However, it can be completed with full custom configuration: https://cz-git.qbb.sh/config/engineer#formatmessagecb https://cz-git.qbb.sh/recipes/default-issues

  1. Does functionality correspond to feat?
  2. Can [PREMPROD-30287] be obtained from, for example, the branch name?

commitlint.config.cjs

const { execSync } = require('node:child_process')

const issue = execSync('git rev-parse --abbrev-ref HEAD')
    .toString()
    .trim()
    .split('_')?.[1] || '0'

const mapping = (type) => {
    const removeAnsi = type.replace(/\x1b\[[0-9;]*m/g, '')
    return {
        feat: 'functionality',
    }[removeAnsi]
}

const pusher = execSync('git config user.name || true').toString().trim()

/** @type {import('czg').UserConfig} */
module.exports = {
    prompt: {
        scopes: ['infrastructure'],
        customScopesAlign: 'bottom',
        skipQuestions: ['body', 'breaking'],
        allowCustomIssuePrefix: false,
        allowEmptyIssuePrefix: false,
        issuePrefixes: ['by'],
        defaultIssues: pusher,
        messages: {
            footer: 'The author of this commit is:\n',
        },
        formatMessageCB: ({ type, scope, subject, footer }) => {
            // console.log(type)
            return `${type}(${scope}) ${mapping(type)}: [PREMPROD-${issue}] ${subject} by ${footer.trim()}`
        },
    },
}

Using formatMessageCB, you can create any commit message format you want.The information can be obtained from the command or Node library in the JS file.

CleanShot 2024-09-10 at 18 49 41@2x