From cf06c27412bafb613ade9f08bdd66486f50de778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20de=20Kat?= Date: Thu, 5 Sep 2024 09:47:24 +0100 Subject: [PATCH] ci(GitHub workflow): add changelog.md file generation and automatic version bumping Add changelog.md file generation and automatic version bumping on push to GitHub. --- .cz.toml | 55 ++++++++++++++++++++++++++++++ .github/templates/CHANGELOG.md.j2 | 24 +++++++++++++ .github/workflows/bump_version.yml | 30 ++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 .cz.toml create mode 100644 .github/templates/CHANGELOG.md.j2 create mode 100644 .github/workflows/bump_version.yml diff --git a/.cz.toml b/.cz.toml new file mode 100644 index 0000000..692f21f --- /dev/null +++ b/.cz.toml @@ -0,0 +1,55 @@ +[tool.commitizen] +version = "0.0.1" +template = ".github/templates/CHANGELOG.md.j2" +name = "cz_customize" + +[tool.commitizen.customize] +message_template = "{{change_type}}{% if scope %}({{scope}}){% endif %}: {{subject}}{% if ticket_no %} ({{ticket_no}}){% endif %}{% if body %}\n\n{{body}}{% endif %}" +bump_pattern = "^((BREAKING[\\-\\ ]CHANGE|\\w+)(\\(.+\\))?!?):" +bump_map = { "^.+!$" = "MAJOR", "^BREAKING[\\- ]CHANGE" = "MAJOR", "^feat" = "MINOR", "^fix" = "PATCH", ci = "PATCH", build = "PATCH", "^refactor" = "PATCH", "^perf" = "PATCH" } + +schema = ": " +schema_pattern = "^(build|ci|docs|feat|fix|perf|refactor|style|test|revert)(\\(\\w+\\))?: [\\w -.]+(\\((#)-[0-9]+\\))?(\\[skip ci\\](.|\\n)*)?$" + +commit_parser = "^(?Pbuild|ci|docs|feat|fix|perf|refactor|style|test|revert|BREAKING CHANGE)(?:\\((?P[^)]+)\\))?:?\\s(?P.*)?" + +changelog_pattern = "^(build|ci|docs|feat|fix|perf|refactor|style|test|revert)?(!)?" +change_type_order = ["BREAKING CHANGE", "feat", "fix", "refactor", "perf", "style", "test", "build", "ci"] +change_type_map = {"build" = "Build process", "ci" = "Continuous integration/delivery", "feat" = "New Features", "fix" = "Bug fixes", "style" = "Code style changes", "refactor" = "Refactored", "test" = "Tests" } + +[[tool.commitizen.customize.questions]] +type = "list" +name = "change_type" +choices = [ + {value = "fix", name = "fix: A bug fix. Correlates with PATCH in SemVer"}, + {value = "feat", name = "feat: A new feature. Correlates with MINOR in SemVer"}, + {value = "docs", name = "docs: Documentation only changes"}, + {value = "style", name = "style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)"}, + {value = "refactor", name = "refactor: A code change that neither fixes a bug nor adds a feature"}, + {value = "perf", name = "perf: A code change that improves performance"}, + {value = "test", name = "test: Adding missing or correcting existing tests"}, + {value = "build", name = "build: Changes that affect the build system or external dependencies (example scopes: pip, docker, npm)"}, + {value = "ci", name = "ci: Changes to our CI configuration files and scripts (example scopes: Jenkins, Docker)"}, + {value = "BREAKING CHANGE", name = "BREAKING: A major change."} +] +message = "Select the type of change you are committing" + +[[tool.commitizen.customize.questions]] +type = "input" +name = "scope" +message = "What is the scope of this change? (class or file name): (press [enter] to skip)\n" + +[[tool.commitizen.customize.questions]] +type = "input" +name = "subject" +message = "Write a short and imperative summary of the code changes: (lower case and no period)\n" + +[[tool.commitizen.customize.questions]] +type = "input" +name = "body" +message = "Provide additional contextual information about the code changes: (press [enter] to skip)\n" + +[[tool.commitizen.customize.questions]] +type = "input" +name = "ticket_no" +message = "Ticket number i.e: #7 (Keep empty if no ticket)\n" diff --git a/.github/templates/CHANGELOG.md.j2 b/.github/templates/CHANGELOG.md.j2 new file mode 100644 index 0000000..34e42a4 --- /dev/null +++ b/.github/templates/CHANGELOG.md.j2 @@ -0,0 +1,24 @@ +{% for entry in tree %} + +## {{ entry.version }}{% if entry.date %} ({{ entry.date }}){% endif %} + +{% for change_key, changes in entry.changes.items() %} + +{% if change_key %} +### {{ change_key }} +{% endif %} + +{% for change in changes %} +{% set message_parts = change.message.split('#') %} +{% set issue = message_parts[1] %} +{% set subject = message_parts[0] %} +{% set issue_link = "[#" + issue + "](https://github.com/renedekat/salary-match/issues/" + issue + ") " if issue else "" %} +{% set commit_link = "[" + change.sha1[:7] + "](" + change.sha1 + ")" %} +{% set scope = "**" + change.scope + "**: " if change.scope else "" %} +- {{ commit_link }} - {{ scope }} {{subject }} (*by [{{ change.author }}](mailto:{{ change.author_email }}))* +{% if issue_link %} + - Fixes issue {{ issue_link }} +{% endif %} +{% endfor %} +{% endfor %} +{% endfor %} diff --git a/.github/workflows/bump_version.yml b/.github/workflows/bump_version.yml new file mode 100644 index 0000000..c0327d8 --- /dev/null +++ b/.github/workflows/bump_version.yml @@ -0,0 +1,30 @@ +name: Bump version + +on: + push: + branches: + - master + +jobs: + bump-version: + if: "!startsWith(github.event.head_commit.message, 'bump:')" + runs-on: ubuntu-latest + name: "Bump version and generate changelog with commitizen" + steps: + - name: Check out + uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}" + - name: Create bump and changelog + uses: commitizen-tools/commitizen-action@master + with: + github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} + changelog_increment_filename: body.md + - name: Release + uses: softprops/action-gh-release@v2 + with: + body_path: "body.md" + tag_name: ${{ env.REVISION }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}