Skip to content

Commit

Permalink
feat(main): add options to configure scope related behaviour
Browse files Browse the repository at this point in the history
- DEFAULT_SCOPE: lets the user define a scope under which all un-scoped commits can be found (defaults to general).
- SUPPRESS_UNSCOPED: can be used to have a pre-bugfix-behaviour in which all unscoped commits will get ignored.
  • Loading branch information
sruehl committed Nov 16, 2021
1 parent fcf92df commit 38c7fcd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ inputs:
description: "The name and email of the committer. e.g. 'author <[email protected]>'"
required: false
default: ''
DEFAULT_SCOPE:
description: "The scope under which unscoped commits can be found e.g. 'fix: some general fix'"
required: false
default: 'general'
SUPPRESS_UNSCOPED:
description: "Suppress the generation of release notes for un-scoped commits e.g. 'fix: some general fix'"
required: false
default: 'false'
runs:
using: "docker"
image: "Dockerfile"
32 changes: 22 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def set_env_from_file(file, args, prefix='INPUT'):
params = step['with']
break
option_params = [
'REPO_NAME', 'ACCESS_TOKEN', 'PATH', 'COMMIT_MESSAGE', 'TYPE', 'COMMITTER'
'REPO_NAME', 'ACCESS_TOKEN', 'PATH', 'COMMIT_MESSAGE', 'TYPE', 'COMMITTER', 'DEFAULT_SCOPE', 'SUPPRESS_UNSCOPED'
]
for param in option_params:
if param not in params.keys():
Expand Down Expand Up @@ -232,13 +232,15 @@ def write_data(self, changelog):
self.repo.create_pull(title=self.__commit_message, body=self.__commit_message, base=self.__pull_request, head=self.__branch, draft=False, maintainer_can_modify=True)


def strip_commits(commits, type_regex):
def strip_commits(commits, type_regex, default_scope, suppress_unscoped):
'''
Bypass some commits
Args:
commits (list): list of commit(dict), whose keys are 'head', 'sha', 'url', 'pr_links'
type_regex (string): regex expression to match.
default_scope (str): scope which matches all un-scoped commits
suppress_unscoped (bool): flag which suppresses entries for un-scoped commits
Returns:
dict: selected commits of every scope.
Expand All @@ -251,7 +253,9 @@ def strip_commits(commits, type_regex):
if re.match(regex, head):
scope = re.findall(regex, head)[0]
if scope == '':
scope = 'all'
if suppress_unscoped:
continue
scope = default_scope
if scope.lower() == 'changelog' and regex == r'^docs(?:[(](.+?)[)])?':
continue
subject = re.sub(regex + r'\s?:\s?', '', head)
Expand All @@ -263,19 +267,21 @@ def strip_commits(commits, type_regex):
return scopes


def generate_section(release_commits, regex):
def generate_section(release_commits, regex, default_scope, suppress_unscoped):
'''
Generate scopes of a section
Args:
release_commits (dict): commits of the release
regex (string): regex expression
default_scope (str): scope which matches all un-scoped commits
suppress_unscoped (bool): flag which suppresses entries for un-scoped commits
Returns:
string: content of section
'''
section = ''
scopes = strip_commits(release_commits, regex)
scopes = strip_commits(release_commits, regex, default_scope, suppress_unscoped)
for scope in scopes:
scope_content = f'''- {scope}:\n'''
for sel_commit in scopes[scope]:
Expand All @@ -292,13 +298,15 @@ def generate_section(release_commits, regex):
return section


def generate_release_body(release_commits, part_name):
def generate_release_body(release_commits, part_name, default_scope, suppress_unscoped):
'''
Generate release body using part_name_dict and regex_list
Args:
release_commits (dict): commits of the release
part_name (list): a list of part_name, e.g. feat:Feature
default_scope (str): scope which matches all un-scoped commits
suppress_unscoped (bool): flag which suppresses entries for un-scoped commits
Returns:
string: body part of release info
Expand All @@ -307,19 +315,21 @@ def generate_release_body(release_commits, part_name):
# TODO: add a new attribute to ignore some commits with another new function
for part in part_name:
regex, name = part.split(':')
sec = generate_section(release_commits, regex)
sec = generate_section(release_commits, regex, default_scope, suppress_unscoped)
if sec != '':
release_body = release_body + '### ' + name + '\n\n' + sec
return release_body


def generate_changelog(releases, part_name):
def generate_changelog(releases, part_name, default_scope, suppress_unscoped):
'''
Generate CHANGELOG
Args:
releases: dict of release data
part_name (list): a list of part_name, e.g. feat:Feature
default_scope (str): scope which matches all un-scoped commits
suppress_unscoped (bool): flag which suppresses entries for un-scoped commits
Returns:
string: content of CHANGELOG
Expand Down Expand Up @@ -366,7 +376,7 @@ def generate_changelog(releases, part_name):
if description == '':
description = '*No description*'
release_info = f'''## [{title}]({url}) - {date}\n\n{description}\n\n'''
release_body = generate_release_body(release_commits, part_name)
release_body = generate_release_body(release_commits, part_name, default_scope, suppress_unscoped)
if release_body == '' and release_tag == 'Unreleased':
continue
else:
Expand Down Expand Up @@ -400,10 +410,12 @@ def main():
COMMIT_MESSAGE = get_inputs('COMMIT_MESSAGE')
COMMITTER = get_inputs('COMMITTER')
part_name = re.split(r'\s?,\s?', get_inputs('TYPE'))
DEFAULT_SCOPE = get_inputs('DEFAULT_SCOPE')
SUPPRESS_UNSCOPED = get_inputs('SUPPRESS_UNSCOPED')
changelog = GithubChangelog(ACCESS_TOKEN, REPO_NAME, PATH, BRANCH, PULL_REQUEST, COMMIT_MESSAGE, COMMITTER)
changelog.get_data()

CHANGELOG = generate_changelog(changelog.read_releases(), part_name)
CHANGELOG = generate_changelog(changelog.read_releases(), part_name, DEFAULT_SCOPE, SUPPRESS_UNSCOPED == 'true')

if args.mode == 'local':
with open(args.output, 'w', encoding='utf-8') as f:
Expand Down
Loading

0 comments on commit 38c7fcd

Please sign in to comment.