-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored michaeljoseph/changes #282
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,15 @@ | |
# TODO: leverage bumpversion | ||
def extract_attribute(module_name, attribute_name): | ||
"""Extract metatdata property from a module""" | ||
with open('%s/__init__.py' % module_name) as input_file: | ||
with open(f'{module_name}/__init__.py') as input_file: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for line in input_file: | ||
if line.startswith(attribute_name): | ||
return ast.literal_eval(line.split('=')[1].strip()) | ||
|
||
|
||
def replace_attribute(module_name, attribute_name, new_value, dry_run=True): | ||
"""Update a metadata attribute""" | ||
init_file = '%s/__init__.py' % module_name | ||
init_file = f'{module_name}/__init__.py' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
_, tmp_file = tempfile.mkstemp() | ||
|
||
with open(init_file) as input_file: | ||
|
@@ -38,7 +38,8 @@ def replace_attribute(module_name, attribute_name, new_value, dry_run=True): | |
|
||
def has_attribute(module_name, attribute_name): | ||
"""Is this attribute present?""" | ||
init_file = '%s/__init__.py' % module_name | ||
init_file = f'{module_name}/__init__.py' | ||
return any( | ||
[attribute_name in init_line for init_line in open(init_file).readlines()] | ||
attribute_name in init_line | ||
for init_line in open(init_file).readlines() | ||
Comment on lines
-41
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,12 +33,10 @@ def write_new_changelog(repo_url, filename, content_lines, dry_run=True): | |
def replace_sha_with_commit_link(repo_url, git_log_content): | ||
git_log_content = git_log_content.split('\n') | ||
for index, line in enumerate(git_log_content): | ||
# http://stackoverflow.com/a/468378/5549 | ||
sha1_re = re.match(r'^[0-9a-f]{5,40}\b', line) | ||
if sha1_re: | ||
if sha1_re := re.match(r'^[0-9a-f]{5,40}\b', line): | ||
sha1 = sha1_re.group() | ||
|
||
new_line = line.replace(sha1, '[%s](%s/commit/%s)' % (sha1, repo_url, sha1)) | ||
new_line = line.replace(sha1, f'[{sha1}]({repo_url}/commit/{sha1})') | ||
Comment on lines
-36
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
log.debug('old line: %s\nnew line: %s', line, new_line) | ||
git_log_content[index] = new_line | ||
|
||
|
@@ -61,16 +59,16 @@ def generate_changelog(context): | |
git_log_content = None | ||
git_log = 'log --oneline --no-merges --no-color'.split(' ') | ||
try: | ||
git_log_tag = git_log + ['%s..master' % context.current_version] | ||
git_log_tag = git_log + [f'{context.current_version}..master'] | ||
git_log_content = git(git_log_tag) | ||
log.debug('content: %s' % git_log_content) | ||
log.debug(f'content: {git_log_content}') | ||
except Exception: | ||
log.warn('Error diffing previous version, initial release') | ||
git_log_content = git(git_log) | ||
|
||
git_log_content = replace_sha_with_commit_link(context.repo_url, git_log_content) | ||
# turn change log entries into markdown bullet points | ||
if git_log_content: | ||
if git_log_content := replace_sha_with_commit_link( | ||
context.repo_url, git_log_content | ||
): | ||
Comment on lines
-64
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
[ | ||
changelog_content.append('* %s\n' % line) if line else line | ||
for line in git_log_content[:-1] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
|
||
from . import __version__ | ||
|
||
VERSION = 'changes {}'.format(__version__) | ||
VERSION = f'changes {__version__}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
||
|
||
@contextlib.contextmanager | ||
|
@@ -62,7 +62,7 @@ def status(repo_directory): | |
""" | ||
Shows current project release status. | ||
""" | ||
repo_directory = repo_directory if repo_directory else '.' | ||
repo_directory = repo_directory or '.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
with work_in(repo_directory): | ||
status_command.status() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,11 @@ def echo(message, style): | |
|
||
|
||
def debug(message): | ||
echo('{}...'.format(message), 'debug') | ||
echo(f'{message}...', 'debug') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def info(message): | ||
echo('{}...'.format(message), 'info') | ||
echo(f'{message}...', 'info') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def note(message): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,28 +16,28 @@ def publish(): | |
info('No staged release to publish') | ||
return | ||
|
||
info('Publishing release {}'.format(release.version)) | ||
info(f'Publishing release {release.version}') | ||
|
||
files_to_add = BumpVersion.read_from_file( | ||
Path('.bumpversion.cfg') | ||
).version_files_to_replace | ||
files_to_add += ['.bumpversion.cfg', str(release.release_file_path)] | ||
|
||
info('Running: git add {}'.format(' '.join(files_to_add))) | ||
info(f"Running: git add {' '.join(files_to_add)}") | ||
repository.add(files_to_add) | ||
|
||
commit_message = release.release_file_path.read_text(encoding='utf-8') | ||
info('Running: git commit --message="{}"'.format(commit_message)) | ||
info(f'Running: git commit --message="{commit_message}"') | ||
repository.commit(commit_message) | ||
|
||
info('Running: git tag {}'.format(release.version)) | ||
info(f'Running: git tag {release.version}') | ||
repository.tag(release.version) | ||
|
||
if click.confirm('Happy to publish release {}'.format(release.version)): | ||
if click.confirm(f'Happy to publish release {release.version}'): | ||
info('Running: git push --tags') | ||
repository.push() | ||
|
||
info('Creating GitHub Release') | ||
repository.create_release(release) | ||
|
||
info('Published release {}'.format(release.version)) | ||
info(f'Published release {release.version}') | ||
Comment on lines
-19
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,19 +20,19 @@ def discard(release_name='', release_description=''): | |
info('No staged release to discard') | ||
return | ||
|
||
info('Discarding currently staged release {}'.format(release.version)) | ||
info(f'Discarding currently staged release {release.version}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
bumpversion = BumpVersion.read_from_file(Path('.bumpversion.cfg')) | ||
git_discard_files = bumpversion.version_files_to_replace + [ | ||
# 'CHANGELOG.md', | ||
'.bumpversion.cfg' | ||
] | ||
|
||
info('Running: git {}'.format(' '.join(['checkout', '--'] + git_discard_files))) | ||
info(f"Running: git {' '.join(['checkout', '--'] + git_discard_files)}") | ||
repository.discard(git_discard_files) | ||
|
||
if release.release_file_path.exists(): | ||
info('Running: rm {}'.format(release.release_file_path)) | ||
info(f'Running: rm {release.release_file_path}') | ||
release.release_file_path.unlink() | ||
|
||
|
||
|
@@ -44,26 +44,22 @@ def stage(draft, release_name='', release_description=''): | |
release.description = release_description | ||
|
||
if not repository.pull_requests_since_latest_version: | ||
error("There aren't any changes to release since {}".format(release.version)) | ||
error(f"There aren't any changes to release since {release.version}") | ||
return | ||
|
||
info( | ||
'Staging [{}] release for version {}'.format( | ||
release.release_type, release.version | ||
) | ||
) | ||
info(f'Staging [{release.release_type}] release for version {release.version}') | ||
|
||
# Bumping versions | ||
if BumpVersion.read_from_file(Path('.bumpversion.cfg')).current_version == str( | ||
release.version | ||
): | ||
info('Version already bumped to {}'.format(release.version)) | ||
info(f'Version already bumped to {release.version}') | ||
else: | ||
bumpversion_arguments = ( | ||
BumpVersion.DRAFT_OPTIONS if draft else BumpVersion.STAGE_OPTIONS | ||
) + [release.bumpversion_part] | ||
|
||
info('Running: bumpversion {}'.format(' '.join(bumpversion_arguments))) | ||
info(f"Running: bumpversion {' '.join(bumpversion_arguments)}") | ||
Comment on lines
-47
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
bumpversion.main(bumpversion_arguments) | ||
|
||
# Release notes generation | ||
|
@@ -84,14 +80,15 @@ def stage(draft, release_name='', release_description=''): | |
releases_directory.mkdir(parents=True) | ||
|
||
release_notes_path = releases_directory.joinpath( | ||
'{}.md'.format(release.release_note_filename) | ||
f'{release.release_note_filename}.md' | ||
) | ||
|
||
|
||
if draft: | ||
info('Would have created {}:'.format(release_notes_path)) | ||
info(f'Would have created {release_notes_path}:') | ||
debug(release_notes) | ||
else: | ||
info('Writing release notes to {}'.format(release_notes_path)) | ||
info(f'Writing release notes to {release_notes_path}') | ||
if release_notes_path.exists(): | ||
release_notes_content = release_notes_path.read_text(encoding='utf-8') | ||
if release_notes_content != release_notes: | ||
|
@@ -107,10 +104,8 @@ def stage(draft, release_name='', release_description=''): | |
) | ||
if click.confirm( | ||
click.style( | ||
'{} has modified content, overwrite?'.format( | ||
release_notes_path | ||
), | ||
**STYLES['error'] | ||
f'{release_notes_path} has modified content, overwrite?', | ||
**STYLES['error'], | ||
) | ||
): | ||
release_notes_path.write_text(release_notes, encoding='utf-8') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,41 +8,33 @@ def status(): | |
|
||
release = changes.release_from_pull_requests() | ||
|
||
info('Status [{}/{}]'.format(repository.owner, repository.repo)) | ||
info(f'Status [{repository.owner}/{repository.repo}]') | ||
|
||
info('Repository: ' + highlight('{}/{}'.format(repository.owner, repository.repo))) | ||
info(f"Repository: {highlight(f'{repository.owner}/{repository.repo}')}") | ||
Comment on lines
-11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
info('Latest Version') | ||
note(repository.latest_version) | ||
|
||
info('Changes') | ||
unreleased_changes = repository.pull_requests_since_latest_version | ||
note( | ||
'{} changes found since {}'.format( | ||
len(unreleased_changes), repository.latest_version | ||
) | ||
f'{len(unreleased_changes)} changes found since {repository.latest_version}' | ||
) | ||
|
||
|
||
for pull_request in unreleased_changes: | ||
note( | ||
'#{} {} by @{}{}'.format( | ||
pull_request.number, | ||
pull_request.title, | ||
pull_request.author, | ||
' [{}]'.format(','.join(pull_request.label_names)) | ||
f" [{','.join(pull_request.label_names)}]" | ||
if pull_request.label_names | ||
else '', | ||
) | ||
) | ||
|
||
|
||
if unreleased_changes: | ||
info( | ||
'Computed release type {} from changes issue tags'.format( | ||
release.release_type | ||
) | ||
) | ||
info( | ||
'Proposed version bump {} => {}'.format( | ||
repository.latest_version, release.version | ||
) | ||
) | ||
info(f'Computed release type {release.release_type} from changes issue tags') | ||
info(f'Proposed version bump {repository.latest_version} => {release.version}') |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,12 @@ | |
DEFAULT_CONFIG_FILE = str( | ||
os.environ.get( | ||
'CHANGES_CONFIG_FILE', | ||
expanduser('~/.changes') | ||
if not compat.IS_WINDOWS | ||
else expandvars(r'%APPDATA%\\.changes'), | ||
expandvars(r'%APPDATA%\\.changes') | ||
if compat.IS_WINDOWS | ||
else expanduser('~/.changes'), | ||
) | ||
) | ||
|
||
Comment on lines
-22
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
PROJECT_CONFIG_FILE = '.changes.toml' | ||
DEFAULT_RELEASES_DIRECTORY = 'docs/releases' | ||
|
||
|
@@ -38,20 +39,19 @@ def load(cls): | |
str( | ||
os.environ.get( | ||
'CHANGES_CONFIG_FILE', | ||
expanduser('~/.changes') | ||
if not compat.IS_WINDOWS | ||
else expandvars(r'%APPDATA%\\.changes'), | ||
expandvars(r'%APPDATA%\\.changes') | ||
if compat.IS_WINDOWS | ||
else expanduser('~/.changes'), | ||
) | ||
) | ||
) | ||
|
||
|
||
tool_settings = None | ||
if tool_config_path.exists(): | ||
tool_settings = Changes(**(toml.load(tool_config_path.open())['changes'])) | ||
|
||
# envvar takes precedence over config file settings | ||
auth_token = os.environ.get(AUTH_TOKEN_ENVVAR) | ||
if auth_token: | ||
if auth_token := os.environ.get(AUTH_TOKEN_ENVVAR): | ||
Comment on lines
-41
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
info('Found Github Auth Token in the environment') | ||
tool_settings = Changes(auth_token=auth_token) | ||
elif not (tool_settings and tool_settings.auth_token): | ||
|
@@ -104,11 +104,7 @@ def load(cls, repository): | |
) | ||
|
||
if not releases_directory.exists(): | ||
debug( | ||
'Releases directory {} not found, creating it.'.format( | ||
releases_directory | ||
) | ||
) | ||
debug(f'Releases directory {releases_directory} not found, creating it.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
releases_directory.mkdir(parents=True) | ||
|
||
project_settings = Project( | ||
|
@@ -127,10 +123,7 @@ def load(cls, repository): | |
|
||
|
||
def configure_labels(github_labels): | ||
labels_keyed_by_name = {} | ||
for label in github_labels: | ||
labels_keyed_by_name[label['name']] = label | ||
|
||
labels_keyed_by_name = {label['name']: label for label in github_labels} | ||
Comment on lines
-130
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# TODO: streamlined support for github defaults: enhancement, bug | ||
changelog_worthy_labels = prompt.choose_labels( | ||
[properties['name'] for _, properties in labels_keyed_by_name.items()] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
release_from_pull_requests
refactored with the following changes:use-named-expression
)comprehension-to-generator
)collection-builtin-to-comprehension
)identity-comprehension
)