$30 off During Our Annual Pro Sale. View Details »

commitizen-tools: What can we gain from craftin...

Lee Wei
August 27, 2020

commitizen-tools: What can we gain from crafting a git message convention? @ Tainan.py June

Lee Wei

August 27, 2020
Tweet

More Decks by Lee Wei

Other Decks in Programming

Transcript

  1. @clleew __name__ = 李唯 / Wei Lee __position__ = [

    Software Engineer @ Rakuten Slice, Volunteer @ PyCon Taiwan, "Maintainer of commitizen-tools, ] __twitter__ = @clleew __github__ = Lee-W __blog__ = http://lee-w.github.io $ cat speaker.py
  2. @clleew File "speaker.py", line 1 __name__ = 李唯 / Wei

    Lee ^ SyntaxError: invalid syntax $ python speaker.py
  3. @clleew Why not use "update" as a commit message? •

    Hard to find the right commit to rollback • Hard to know the trace of this change during code review • Tend to make a lot of changes in a single commit
 (because you don't know how to describe these changes)
  4. @clleew Why should we write good commit message? • Guide

    teams on how code changes should be made • Save reviewers' time to know the intention of changes • Give newcomer a clear view of the evolvement of projects
  5. @clleew How to write good commit messages? 1. Separate subject

    from body with a blank line 2. Limit the subject line to 50 characters 3. Capitalize the subject line 4. Do not end the subject line with a period 5. Use the imperative mood in the subject line 6. Wrap the body at 72 characters 7. Use the body to explain what and why vs. how
  6. @clleew cz commit type of this change A hint for

    users to break down multiple changes into separate commits
  7. @clleew How does the commit look like? feat(tainan.py): awesome meetup

    hope everyone enjoys this talk https://www.meetup.com/Tainan-py-Python-Tainan-User-Group/events/ 272270687/
  8. @clleew Enforce commit check through pre-commit 1.Create .pre-commit-config.yaml
 
 


    
 
 2.Install pre-commit hook repos: - hooks: - id: commitizen stages: - commit-msg repo: https://github.com/commitizen-tools/commitizen rev: v2.1.0
  9. @clleew How can we utilize these standardize commit messages? •

    Auto versioning • Auto generate changelog
  10. @clleew • MAJOR: incompatible API changes • MINOR: add backwards

    compatible functionality • PATCH: make backwards compatible bug fixes Semantic Versioning
  11. @clleew The commit with the highest version to bump is

    feat (The original version of the sample project is 1.2.0.) Bump Version $ cz bump
  12. @clleew message tag to create: 1.3.0 increment detected: MINOR The

    commit with the highest version to bump is feat (The original version of the sample project is 1.2.0.) Bump Version $ cz bump
  13. @clleew Bump Version in Source File [tool.commitizen] name = "cz_conventional_commits"

    version = "0.0.1" tag_format = "$version" version_files = [ "my_project/__init__.py" ]
  14. @clleew Auto generate changelog # CHANGELOG ## Unreleased ### feat

    - add cov task to check test coverage - make scripts a package - add pylint task - add mypy, flake8 style check - add reformat task to reformat code through black
  15. @clleew Cutsomization 1. Use "cz-customize" in configuration file • No

    Python Knowledge needed • Suitable for basic rules 2. Creating a customized python package • Suitable for advance usage
 (e.g., customize changelog parser and hook)
  16. @clleew cz_customize [tool.commitizen] name = "cz_customize" [tool.commitizen.customize] message_template = "{{change_type}}:

    {{message}}" bump_pattern = "^(break|new|hotfix)" bump_map = {"break" = "MAJOR", "new" = "MINOR", "hotfix" = "PATCH"} [tool.commitizen] name = "cz_customize"
  17. @clleew cz_customize [tool.commitizen] name = "cz_customize" [tool.commitizen.customize] message_template = "{{change_type}}:

    {{message}}" bump_pattern = "^(break|new|hotfix)" bump_map = {"break" = "MAJOR", "new" = "MINOR", "hotfix" = "PATCH"} [[tool.commitizen.customize.questions]] type = "list" name = "change_type" choices = [ {value = "feature", name = "feature: A new feature."}, {value = "hotfix", name = "bug fix: A bug fix."} ] message = "Select the type of change you are committing" [[tool.commitizen.customize.questions]] type = "input" name = "message" message = "Body." [tool.commitizen] name = "cz_customize" [tool.commitizen.customize] message_template = "{{change_type}}: {{message}}" bump_pattern = "^(break|new|hotfix)" bump_map = {"break" = "MAJOR", "new" = "MINOR", "hotfix" = "PATCH"} [tool.commitizen] name = "cz_customize"
  18. @clleew Customize through python package from commitizen.cz.base import BaseCommitizen class

    JiraCz(BaseCommitizen): def questions(self) -> list: """Questions regarding the commit message.""" questions = [ {"type": "input", "name": "title", "message": "Commit title"}, {"type": "input", "name": "issue", "message": "Jira Issue number:"}, ] return questions def message(self, answers: dict) -> str: """Generate the message with the given answers.""" return f"{answers['title']} (#{answers['issue']})" discover_this = JiraCz # used by the plug-in system
  19. @clleew Reference • commitizen-tools • Conventional Commits • Semantic Versioning

    • Keep a changelog • How to Write a Git Commit Message