Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions changes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ def release_from_pull_requests():

pull_requests = repository.pull_requests_since_latest_version

labels = set(
[
label_name
for pull_request in pull_requests
for label_name in pull_request.label_names
]
)
labels = {
label_name
for pull_request in pull_requests
for label_name in pull_request.label_names
}

Comment on lines -41 to +46
Copy link
Author

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:


descriptions = [
'\n'.join([pull_request.title, pull_request.description])
Expand All @@ -66,8 +65,7 @@ def release_from_pull_requests():
release_type=release_type,
)

release_files = [release_file for release_file in releases_directory.glob('*.md')]
if release_files:
if release_files := list(releases_directory.glob('*.md')):
release_file = release_files[0]
release.release_file_path = Path(project_settings.releases_directory).joinpath(
release_file.name
Expand Down
9 changes: 5 additions & 4 deletions changes/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function extract_attribute refactored with the following changes:

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'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function replace_attribute refactored with the following changes:

_, tmp_file = tempfile.mkstemp()

with open(init_file) as input_file:
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function has_attribute refactored with the following changes:

)
16 changes: 7 additions & 9 deletions changes/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function replace_sha_with_commit_link refactored with the following changes:

This removes the following comments ( why? ):

# http://stackoverflow.com/a/468378/5549

log.debug('old line: %s\nnew line: %s', line, new_line)
git_log_content[index] = new_line

Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function generate_changelog refactored with the following changes:

This removes the following comments ( why? ):

# turn change log entries into markdown bullet points

[
changelog_content.append('* %s\n' % line) if line else line
for line in git_log_content[:-1]
Expand Down
4 changes: 2 additions & 2 deletions changes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from . import __version__

VERSION = 'changes {}'.format(__version__)
VERSION = f'changes {__version__}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 16-16 refactored with the following changes:



@contextlib.contextmanager
Expand Down Expand Up @@ -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 '.'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function status refactored with the following changes:


with work_in(repo_directory):
status_command.status()
Expand Down
4 changes: 2 additions & 2 deletions changes/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def echo(message, style):


def debug(message):
echo('{}...'.format(message), 'debug')
echo(f'{message}...', 'debug')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function debug refactored with the following changes:



def info(message):
echo('{}...'.format(message), 'info')
echo(f'{message}...', 'info')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function info refactored with the following changes:



def note(message):
Expand Down
12 changes: 6 additions & 6 deletions changes/commands/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function publish refactored with the following changes:

31 changes: 13 additions & 18 deletions changes/commands/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function discard refactored with the following changes:


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()


Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function stage refactored with the following changes:

bumpversion.main(bumpversion_arguments)

# Release notes generation
Expand All @@ -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:
Expand All @@ -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')
Expand Down
24 changes: 8 additions & 16 deletions changes/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function status refactored with the following changes:


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}')
29 changes: 11 additions & 18 deletions changes/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 22-24 refactored with the following changes:

PROJECT_CONFIG_FILE = '.changes.toml'
DEFAULT_RELEASES_DIRECTORY = 'docs/releases'

Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Changes.load refactored with the following changes:

This removes the following comments ( why? ):

# envvar takes precedence over config file settings

info('Found Github Auth Token in the environment')
tool_settings = Changes(auth_token=auth_token)
elif not (tool_settings and tool_settings.auth_token):
Expand Down Expand Up @@ -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.')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Project.load refactored with the following changes:

releases_directory.mkdir(parents=True)

project_settings = Project(
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function configure_labels refactored with the following changes:

# TODO: streamlined support for github defaults: enhancement, bug
changelog_worthy_labels = prompt.choose_labels(
[properties['name'] for _, properties in labels_keyed_by_name.items()]
Expand Down
Loading