Skip to content

Commit d9d66d2

Browse files
authored
feat(main): add options to configure scope related behaviour (#75)
- Scopeless commits will be picked up now - 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.
1 parent 920f902 commit d9d66d2

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

action.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ inputs:
3636
description: "The name and email of the committer. e.g. 'author <[email protected]>'"
3737
required: false
3838
default: ''
39+
DEFAULT_SCOPE:
40+
description: "The scope under which unscoped commits can be found e.g. 'fix: some general fix'"
41+
required: false
42+
default: 'general'
43+
SUPPRESS_UNSCOPED:
44+
description: "Suppress the generation of release notes for un-scoped commits e.g. 'fix: some general fix'"
45+
required: false
46+
default: 'false'
3947
runs:
4048
using: "docker"
4149
image: "Dockerfile"

main.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def set_env_from_file(file, args, prefix='INPUT'):
8787
params = step['with']
8888
break
8989
option_params = [
90-
'REPO_NAME', 'ACCESS_TOKEN', 'PATH', 'COMMIT_MESSAGE', 'TYPE', 'COMMITTER'
90+
'REPO_NAME', 'ACCESS_TOKEN', 'PATH', 'COMMIT_MESSAGE', 'TYPE', 'COMMITTER', 'DEFAULT_SCOPE', 'SUPPRESS_UNSCOPED'
9191
]
9292
for param in option_params:
9393
if param not in params.keys():
@@ -232,26 +232,33 @@ def write_data(self, changelog):
232232
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)
233233

234234

235-
def strip_commits(commits, type_regex):
235+
def strip_commits(commits, type_regex, default_scope, suppress_unscoped):
236236
'''
237237
Bypass some commits
238238
239239
Args:
240240
commits (list): list of commit(dict), whose keys are 'head', 'sha', 'url', 'pr_links'
241241
type_regex (string): regex expression to match.
242+
default_scope (str): scope which matches all un-scoped commits
243+
suppress_unscoped (bool): flag which suppresses entries for un-scoped commits
242244
243245
Returns:
244246
dict: selected commits of every scope.
245247
'''
246248
# TODO: add an attribute to ignore scope
247-
regex = r'^'+ type_regex + r'[(](.+?)[)]'
249+
regex = r'^' + type_regex + r'(?:[(](.+?)[)])?'
248250
scopes = {}
249251
for commit in commits:
250-
if re.match(regex, commit['head']):
251-
scope = re.findall(regex, commit['head'])[0]
252-
if scope.lower() == 'changelog' and regex == r'^docs[(](.+?)[)]':
252+
head = commit['head']
253+
if re.match(regex, head):
254+
scope = re.findall(regex, head)[0]
255+
if scope == '':
256+
if suppress_unscoped:
257+
continue
258+
scope = default_scope
259+
if scope.lower() == 'changelog' and regex == r'^docs(?:[(](.+?)[)])?':
253260
continue
254-
subject = re.sub(regex + r'\s?:\s?', '', commit['head'])
261+
subject = re.sub(regex + r'\s?:\s?', '', head)
255262
if scope in scopes:
256263
scopes[scope].append({'subject': subject, 'commit': commit})
257264
else:
@@ -260,19 +267,21 @@ def strip_commits(commits, type_regex):
260267
return scopes
261268

262269

263-
def generate_section(release_commits, regex):
270+
def generate_section(release_commits, regex, default_scope, suppress_unscoped):
264271
'''
265272
Generate scopes of a section
266273
267274
Args:
268275
release_commits (dict): commits of the release
269276
regex (string): regex expression
277+
default_scope (str): scope which matches all un-scoped commits
278+
suppress_unscoped (bool): flag which suppresses entries for un-scoped commits
270279
271280
Returns:
272281
string: content of section
273282
'''
274283
section = ''
275-
scopes = strip_commits(release_commits, regex)
284+
scopes = strip_commits(release_commits, regex, default_scope, suppress_unscoped)
276285
for scope in scopes:
277286
scope_content = f'''- {scope}:\n'''
278287
for sel_commit in scopes[scope]:
@@ -289,13 +298,15 @@ def generate_section(release_commits, regex):
289298
return section
290299

291300

292-
def generate_release_body(release_commits, part_name):
301+
def generate_release_body(release_commits, part_name, default_scope, suppress_unscoped):
293302
'''
294303
Generate release body using part_name_dict and regex_list
295304
296305
Args:
297306
release_commits (dict): commits of the release
298307
part_name (list): a list of part_name, e.g. feat:Feature
308+
default_scope (str): scope which matches all un-scoped commits
309+
suppress_unscoped (bool): flag which suppresses entries for un-scoped commits
299310
300311
Returns:
301312
string: body part of release info
@@ -304,19 +315,21 @@ def generate_release_body(release_commits, part_name):
304315
# TODO: add a new attribute to ignore some commits with another new function
305316
for part in part_name:
306317
regex, name = part.split(':')
307-
sec = generate_section(release_commits, regex)
318+
sec = generate_section(release_commits, regex, default_scope, suppress_unscoped)
308319
if sec != '':
309320
release_body = release_body + '### ' + name + '\n\n' + sec
310321
return release_body
311322

312323

313-
def generate_changelog(releases, part_name):
324+
def generate_changelog(releases, part_name, default_scope, suppress_unscoped):
314325
'''
315326
Generate CHANGELOG
316327
317328
Args:
318329
releases: dict of release data
319330
part_name (list): a list of part_name, e.g. feat:Feature
331+
default_scope (str): scope which matches all un-scoped commits
332+
suppress_unscoped (bool): flag which suppresses entries for un-scoped commits
320333
321334
Returns:
322335
string: content of CHANGELOG
@@ -363,7 +376,7 @@ def generate_changelog(releases, part_name):
363376
if description == '':
364377
description = '*No description*'
365378
release_info = f'''## [{title}]({url}) - {date}\n\n{description}\n\n'''
366-
release_body = generate_release_body(release_commits, part_name)
379+
release_body = generate_release_body(release_commits, part_name, default_scope, suppress_unscoped)
367380
if release_body == '' and release_tag == 'Unreleased':
368381
continue
369382
else:
@@ -397,10 +410,12 @@ def main():
397410
COMMIT_MESSAGE = get_inputs('COMMIT_MESSAGE')
398411
COMMITTER = get_inputs('COMMITTER')
399412
part_name = re.split(r'\s?,\s?', get_inputs('TYPE'))
413+
DEFAULT_SCOPE = get_inputs('DEFAULT_SCOPE')
414+
SUPPRESS_UNSCOPED = get_inputs('SUPPRESS_UNSCOPED')
400415
changelog = GithubChangelog(ACCESS_TOKEN, REPO_NAME, PATH, BRANCH, PULL_REQUEST, COMMIT_MESSAGE, COMMITTER)
401416
changelog.get_data()
402417

403-
CHANGELOG = generate_changelog(changelog.read_releases(), part_name)
418+
CHANGELOG = generate_changelog(changelog.read_releases(), part_name, DEFAULT_SCOPE, SUPPRESS_UNSCOPED == 'true')
404419

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

0 commit comments

Comments
 (0)