Coverage for /home/delusionalinsanity/bbrepos/matyan/src/matyan/utils.py : 66%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
get_all_branch_types, get_branch_types, get_ignore_commits_exact_words, get_ignore_commits_prefixes, get_other_branch_type, get_other_branch_type_key, get_unreleased, get_unreleased_key, get_unreleased_key_label, ) REGEX_PATTERN_BRANCH_NAME, REGEX_PATTERN_COMMIT, REGEX_PATTERN_COMMIT_LINE, REGEX_PATTERN_MERGED_BRANCH_NAME, REGEX_PATTERN_TAG, )
'create_config_file', 'generate_changelog_cli', 'generate_empty_tree', 'get_branch_type', 'get_logs', 'json_changelog_cli', 'prepare_changelog', 'prepare_releases_changelog', 'validate_between', )
'"commit_hash": "%H", ' \ '"commit_abbr": "%h", ' \ '"datetime": "%ci", ' \ '"title": "%s",' \ '"author": "%an", ' \ '"author_email": "%ae", ' \ '"merge": "%P"' \ '}'
# BRANCH_TYPES = { # 'other': "Other", # 'feature': "Features", # 'bugfix': "Bugfixes", # 'hotfix': "Hotfixes", # 'deprecation': "Deprecations", # } # UNRELEASED = 'unreleased'
"""Get lots of logs.
:param between: :return: """ try: rev_list_text = REPOSITORY.rev_list(between) rev_list = rev_list_text.split('\n') if len(rev_list) >= 2: upper = rev_list[0] lower = rev_list[-1] except GitCommandError as err: pass
# Merges log text_log_merges_args.append( "{}..{}".format(lower, upper) ) "--pretty={}".format(PRETTY_FORMAT), "--source", # "--all", "--merges", ])
# Commits log text_log_args.append( "{}..{}".format(lower, upper) ) "--pretty={}".format(PRETTY_FORMAT), "--source", # "--all" # TODO: remove ])
# Tags log # if lower and upper: # text_log_tags_args.append( # "{}..{}".format(lower, upper) # ) "--tags", "--source", "--oneline" ])
'TEXT_LOG_MERGES': text_log_merges, 'LOG_MERGES': log_merges, 'TEXT_LOG': text_log, 'LOG': log, 'TEXT_LOG_TAGS': text_log_tags, 'LOG_TAGS': log_tags, 'COMMIT_TAGS': commit_tags, }
"""Get branch type.
:param branch_type: :return: """
"""Generate empty tree.
Example:
{ 'feature': {}, 'bugfix': {}, 'hotfix': {}, 'deprecation': {}, 'other': { TICKET_NUMBER_OTHER: { # 'title': '', 'commits': {} } }, }
:return: """ empty_tree = {} for key, value in BRANCH_TYPES.items(): empty_tree.update({key: {}})
empty_tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER] = {'commits': {}} return empty_tree
between: str = None, unique_commit_messages: bool = False ) -> Dict[ str, Dict[str, Dict[str, Union[str, Dict[str, Union[str, str]]]]] ]: """Prepare changelog.
:param between: :param unique_commit_messages: :return: """ # tree = generate_empty_tree()
# First fill feature branches only except json.decoder.JSONDecodeError: continue # TODO: fix this (when commit message contains " symbols)
# Skip strange feature branches continue
# If no pattern found, use other branch except AttributeError: branch_type = BRANCH_TYPE_OTHER
# If no pattern found, use other ticket number except AttributeError: ticket_number = TICKET_NUMBER_OTHER
# For normal tree
'commit_hash': entry['commit_hash'], 'commit_abbr': entry['commit_abbr'], 'ticket_number': ticket_number, 'branch_type': branch_type, 'slug': branch_title, 'title': branch_title.replace('-', ' ').title(), 'commits': {}, 'release': release, }
# Now go through commits except json.decoder.JSONDecodeError: continue # TODO: fix this (when commit message contains " symbols)
# Skip strange feature branches continue
except AttributeError: branch_type = BRANCH_TYPE_OTHER
except AttributeError: ticket_number = TICKET_NUMBER_OTHER
else: except AttributeError: ticket_number = '' commit_message = entry['title'][:]
# Ignore the following messages continue
continue
if unique_commit_messages \ else entry['commit_hash']
tree[cur_branch_type] = {}
tree[cur_branch_type][cur_branch] = {}
tree[cur_branch_type][cur_branch]['commits'] = {}
'commit_hash': entry['commit_hash'], 'commit_abbr': entry['commit_abbr'], 'author': entry['author'], 'date': entry['datetime'], 'ticket_number': ticket_number, 'title': commit_message, } else: ticket_number, BRANCH_TYPE_OTHER )
'commit_hash': entry['commit_hash'], 'commit_abbr': entry['commit_abbr'], 'author': entry['author'], 'date': entry['datetime'], 'ticket_number': ticket_number, 'title': commit_message, } except: # TODO: Anything here? pass else: if BRANCH_TYPE_OTHER not in tree: tree[BRANCH_TYPE_OTHER] = {}
if TICKET_NUMBER_OTHER not in tree[BRANCH_TYPE_OTHER]: tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER] = {}
if 'commits' not in tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]: tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]['commits'] = {}
tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]['commits'][commit_hash] = { # NOQA 'commit_hash': entry['commit_hash'], 'commit_abbr': entry['commit_abbr'], 'author': entry['author'], 'date': entry['datetime'], 'ticket_number': ticket_number, 'title': commit_message, }
between: str = None, unique_commit_messages: bool = False ) -> Dict[ str, Dict[str, Dict[str, Union[str, Dict[str, Union[str, str]]]]] ]: """Prepare releases changelog.
:param between: :param unique_commit_messages: :return: """ # releases_tree = {tag: generate_empty_tree() for tag in releases}
# First fill feature branches only except json.decoder.JSONDecodeError: continue # TODO: fix this (when commit message contains " symbols)
# Skip strange feature branches continue
# If no pattern found, use other branch except AttributeError: branch_type = BRANCH_TYPE_OTHER
# If no pattern found, use other ticket number except AttributeError: ticket_number = TICKET_NUMBER_OTHER
# For normal tree release = UNRELEASED
'commit_hash': entry['commit_hash'], 'commit_abbr': entry['commit_abbr'], 'ticket_number': ticket_number, 'branch_type': branch_type, 'slug': branch_title, 'title': branch_title.replace('-', ' ').title(), 'commits': {}, 'release': release, }
# Now go through commits except json.decoder.JSONDecodeError: continue # TODO: fix this (when commit message contains " symbols)
# Skip strange feature branches continue
except AttributeError: branch_type = BRANCH_TYPE_OTHER
except AttributeError: ticket_number = TICKET_NUMBER_OTHER
else: except AttributeError: ticket_number = '' commit_message = entry['title'][:]
# Ignore the following messages continue
continue
if unique_commit_messages \ else entry['commit_hash']
release = UNRELEASED
releases_tree[release] = {}
releases_tree[release][cur_branch_type] = {}
releases_tree[release][cur_branch_type][cur_branch] = {}
releases_tree[release][cur_branch_type][cur_branch]['commits'] = {}
'commit_hash': entry['commit_hash'], 'commit_abbr': entry['commit_abbr'], 'author': entry['author'], 'date': entry['datetime'], 'ticket_number': ticket_number, 'title': commit_message, } else: ticket_number, BRANCH_TYPE_OTHER ) releases_tree[release] = {}
'commit_hash': entry['commit_hash'], 'commit_abbr': entry['commit_abbr'], 'author': entry['author'], 'date': entry['datetime'], 'ticket_number': ticket_number, 'title': commit_message, } except: # TODO: Anything here? pass else: if release not in releases_tree: releases_tree[release] = {}
if BRANCH_TYPE_OTHER not in releases_tree[release]: releases_tree[release][BRANCH_TYPE_OTHER] = {}
if TICKET_NUMBER_OTHER not in releases_tree[release][BRANCH_TYPE_OTHER]: releases_tree[release][BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER] = {}
if 'commits' not in releases_tree[release][BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]: releases_tree[release][BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]['commits'] = {}
releases_tree[release][BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]['commits'][commit_hash] = { # NOQA 'commit_hash': entry['commit_hash'], 'commit_abbr': entry['commit_abbr'], 'author': entry['author'], 'date': entry['datetime'], 'ticket_number': ticket_number, 'title': commit_message, }
"""Validate between.
:param between: :return: """ pass # TODO
"""Get latest release.
Command:
git describe --match '*.*' --abbr=0
:return: """ return REPOSITORY.describe('--match', '*.*', '--abbr=0')
"""Get latest <limit> releases.
Command:
git tag --sort=-version:refname -l '*.*' | head -n <limit>
:return: """ return REPOSITORY.tag( '--sort=-version:refname', '--list', '*.*' ).split('\n')[:limit]
"""Generate changelog (JSON format).""" parser = argparse.ArgumentParser(description='Generate JSON changelog') parser.add_argument( 'between', nargs='?', default=None, help="Range, might be tag or a commit or a branch.", ) parser.add_argument( '--no-other', dest="no_other", default=False, action='store_true', help="No `Other` section", ) parser.add_argument( '--show-releases', dest="show_releases", default=False, action='store_true', help="Show releases", ) parser.add_argument( '--latest-release', dest="latest_release", default=False, action='store_true', help="Generate changelog for the latest release only", ) args = parser.parse_args(sys.argv[1:]) between = args.between if validate_between(args.between) else None include_other = not args.no_other show_releases = args.show_releases latest_release = args.latest_release
if latest_release: latest_two_releases = get_latest_releases(limit=2) latest_two_releases = latest_two_releases[::-1] if len(latest_two_releases): between = '..'.join(latest_two_releases)
if not show_releases: tree = prepare_changelog( between=between, unique_commit_messages=True ) pprint(tree) else: releases_tree = prepare_releases_changelog( between=between, unique_commit_messages=True ) pprint(releases_tree)
# if not include_other: # tree.pop(BRANCH_TYPE_OTHER) # pprint(tree)
"""Generate changelog (markdown format).""" 'between', nargs='?', default=None, help="Range, might be tag or a commit or a branch.", ) '--no-other', dest="no_other", default=False, action='store_true', help="No `Other` section", ) '--show-releases', dest="show_releases", default=False, action='store_true', help="Show releases", ) '--latest-release', dest="latest_release", default=False, action='store_true', help="Generate changelog for the latest release only", ) # if show_latest_release and between: # raise Exception( # "--show-latest-release can't be used in combination with specific" # "tags/commits/branches range." # ) latest_two_releases = get_latest_releases(limit=2) latest_two_releases = latest_two_releases[::-1] if len(latest_two_releases): between = '..'.join(latest_two_releases)
between=between, unique_commit_messages=True ) # Skip adding orphaned commits if explicitly asked not to.
# Do not add branch type if no related branches found "\n**{}**".format(BRANCH_TYPES.get(branch_type)) )
# Add tickets "\n*{} {}*\n".format( ticket_number, ticket_data['title'] ) ) else: changelog.append('')
"- {} [{}]".format( commit_data['title'], commit_data['author'] ) ) else: between=between, unique_commit_messages=True ) if release == UNRELEASED \ else release
# import ipdb; ipdb.set_trace() # Skip adding orphaned commits if explicitly asked not to.
# Do not add branch type if no related branches found "\n**{}**".format(BRANCH_TYPES.get(branch_type)) )
# Add tickets except Exception as err: import ipdb; ipdb.set_trace() "\n*{} {}*\n".format(ticket_number, ticket_data['title']) ) else: changelog.append('')
"- {} [{}]".format( commit_data['title'], commit_data['author'] ) )
"""Create config file.
:return: """ source_filename = project_dir('.matyan.ini') dest_filename = os.path.join(os.getcwd(), '.matyan.ini') try: copyfile(source_filename, dest_filename) return True except Exception as err: return False
return not create_config_file() |