From f29719fa0b5e1bfb7908b0bad78a46d82d64f8ba Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 14 Mar 2023 11:34:02 +0100 Subject: [PATCH] ci(common): force scoping commit messages with components Add simple changelog generator and mdns tag formatter --- .pre-commit-config.yaml | 10 ++++ ci/changelog.py | 98 ++++++++++++++++++++++++++++++++++++++++ components/mdns/.cz.yaml | 8 ++++ 3 files changed, 116 insertions(+) create mode 100644 ci/changelog.py create mode 100644 components/mdns/.cz.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3def3ffa7..9fbc575b4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,6 +16,7 @@ repos: rev: 5.0.4 hooks: - id: flake8 + args: ['--config=.flake8', '--tee', '--benchmark'] - repo: https://github.com/pre-commit/mirrors-mypy rev: v0.981 hooks: @@ -32,6 +33,7 @@ repos: rev: "v0.32.0" hooks: - id: yapf + args: ['style={based_on_style: google, column_limit: 160, indent_width: 4}'] - repo: https://github.com/pre-commit/mirrors-isort rev: v5.10.1 hooks: @@ -56,3 +58,11 @@ repos: - id: commitizen - id: commitizen-branch stages: [push] + - repo: local + hooks: + - id: commit message scopes + name: "commit message must be scoped with: mdns, modem, websocket, asio, mqtt_cxx, common" + entry: '\A(?!(feat|fix|ci|bump|test|docs)\((mdns|modem|common|websocket|asio|mqtt_cxx)\)\:)' + language: pygrep + args: [--multiline] + stages: [commit-msg] diff --git a/ci/changelog.py b/ci/changelog.py new file mode 100644 index 000000000..d4f42471d --- /dev/null +++ b/ci/changelog.py @@ -0,0 +1,98 @@ +# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 +import argparse +import os +import re + +import sh + + +def main(): + parser = argparse.ArgumentParser( + description='Generates a change log from cz commits') + parser.add_argument('component') + args = parser.parse_args() + + old_ref = os.environ.get('CZ_PRE_CURRENT_TAG_VERSION') + new_tag = os.environ.get('CZ_PRE_NEW_TAG_VERSION') + new_ref = os.environ.get('CZ_PRE_NEW_VERSION') + component = args.component + + root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) + + git = sh.git.bake(_cwd=root_path) + + keys = ['breaking', 'major', 'feat', 'fix', 'update'] + items = {key: [] for key in keys} + sections = { + 'feat': 'Features', + 'fix': 'Bug Fixes', + 'update': 'Updated', + 'breaking': 'Breaking changes', + 'major': 'Major changes' + } + brief_log = git.log('--oneline', '{}..HEAD'.format(old_ref), '--', 'components/' + component, _tty_out=False) + for oneline in brief_log.splitlines(): + [commit, brief_msg] = oneline.split(' ', 1) + if 'Merge' in str(brief_msg) or brief_msg.startswith('bump('): # skip Merge commits and bumps + continue + find_title = re.findall(r'^(fix|feat|esp_modem|esp-modem|mdns)(\([^\)]+\))?:\s*(.+)$', str(brief_msg)) + item_type = '' # default prefix + item_message = brief_msg # default body + if find_title is not None and len(find_title) > 0: # Use scopes & types + item_type = find_title[0][0] # prefix (fix/feat/ci) + item_message = find_title[0][-1] # title body + + # Add details in parentheses (commit-id and references from the FOOTER section) + details = '[{}](https://github.com/espressif/esp-protocols/commit/{})'.format(commit, commit) + msg_details = git.show('-s', commit, _tty_out=False) + # check references + if any(str(msg_details) in s for s in ['esp-protocols/issues', 'BREAKING CHANGE', 'MAJOR CHANGE']): + # Closes + closes = re.findall(r'Closes ([^\d]+/(\d+))', str(msg_details), re.MULTILINE) + if closes and closes[0] is not None: + details += ', [#{}]({})'.format(closes[0][1], closes[0][0]) + # Breaking changes + find_breaking = re.findall(r'BREAKING CHANGE:\s*([^\n]*)', str(msg_details), re.MULTILINE) + if find_breaking is not None and len(find_breaking) > 0: + if find_breaking[0] != '': + items['breaking'].append('{} ([{}](https://github.com/espressif/esp-protocols/commit/{}))'.format(find_breaking[0], item_message, commit)) + else: + items['breaking'].append('{} ({})'.format(item_message, details)) + details += ', !BREAKING' + # Major changes + find_major = re.findall(r'MAJOR CHANGE:\s*([^\n]*)', + str(msg_details), re.MULTILINE) + if find_major is not None and len( + find_major) > 0 and find_major[0] != '': + items['major'].append('{} ([{}](https://github.com/espressif/esp-protocols/commit/{}))'.format(find_major[0], item_message, commit)) + if item_type in ['fix', 'feat']: + items[item_type].append('{} ({})'.format(item_message, details)) + else: + items['update'].append('{} ({})'.format(item_message, details)) + # Generate changelog + changelog = '## [{}](https://github.com/espressif/esp-protocols/commits/{})\n'.format( + new_ref, new_tag) + for section, item in items.items(): + if len(item) > 0: + changelog += '\n### {}\n\n'.format(sections[section]) + for it in item: + changelog += '- {}\n'.format(it) + changelog += '\n' + filename = os.path.join(root_path, 'components', component, 'CHANGELOG.md') + # insert the actual changelog to the beginning of the file, just after the title (2nd line) + with open(filename, 'r') as orig_changelog: + changelog_title = orig_changelog.readline( + ) # expect # Changelog title on the first line + changelog_nl = orig_changelog.readline() + orig_items = orig_changelog.read() + with open(filename, 'w') as updated_changelog: + updated_changelog.write(changelog_title) + updated_changelog.write(changelog_nl) + updated_changelog.write(changelog) + updated_changelog.write(orig_items) + git.add(filename) + + +if __name__ == '__main__': + main() diff --git a/components/mdns/.cz.yaml b/components/mdns/.cz.yaml new file mode 100644 index 000000000..9ce402527 --- /dev/null +++ b/components/mdns/.cz.yaml @@ -0,0 +1,8 @@ +--- +commitizen: + bump_message: 'bump(mdns): $current_version -> $new_version' + pre_bump_hooks: python ../../ci/changelog.py mdns + tag_format: mdns-v$version + version: 1.0.8 + version_files: + - idf_component.yml