Skip to content

Commit

Permalink
[publish] Add '--add-all' flag
Browse files Browse the repository at this point in the history
Adds the '--add-all' option to the 'publish' command
to include all changed files in the release commit.

Signed-off-by: Eva Millán <[email protected]>
  • Loading branch information
evamillan committed Oct 10, 2023
1 parent d4543d6 commit a5156f0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
39 changes: 24 additions & 15 deletions release_tools/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
help="Do not remove changelog entries from the repository.")
@click.option('--remote-branch', 'remote_branch', default="master",
help="Remote branch to push. Default 'master'.")
def publish(version, author, remote, only_push, no_cleanup, remote_branch):
@click.option('--add-all', is_flag=True,
help="Add all changed files to the release commit.")
def publish(version, author, remote, only_push, no_cleanup, remote_branch, add_all):
"""Publish a new release.
This script will generate a new release in the repository.
Expand All @@ -71,6 +73,10 @@ def publish(version, author, remote, only_push, no_cleanup, remote_branch):
When '--no-cleanup' argument is specified, do not remove changelog
entries.
By default, the release commit will include the version, pyproject,
release notes, news and authors files. To add all changed files to
the release commit use the `--add-all` flag.
VERSION: version of the new release.
AUTHOR: author of the new release (e.g. John Smith <[email protected]>)
Expand All @@ -88,7 +94,7 @@ def publish(version, author, remote, only_push, no_cleanup, remote_branch):
if not only_push:
if not no_cleanup:
remove_unreleased_changelog_entries(project)
add_release_files(project, version)
add_release_files(project, version, add_all)
commit(project, version, author)

if remote:
Expand Down Expand Up @@ -126,28 +132,31 @@ def rollback_add_release_files(project):
pass


def add_release_files(project, version):
def add_release_files(project, version, add_all):
"""Add to the repository all the files needed to publish a release."""

click.echo("Adding files to the release commit...", nl=False)

# Add version file
version_file = project.version_file
if add_all:
project.repo.add_all()
else:
# Add version file
version_file = project.version_file

if not version_file:
rollback_add_release_files(project)
raise click.ClickException("version file not found")
if not version_file:
rollback_add_release_files(project)
raise click.ClickException("version file not found")

project.repo.add(version_file)
project.repo.add(version_file)

# Add pyproject.toml file
pyproject_file = project.pyproject_file
# Add pyproject.toml file
pyproject_file = project.pyproject_file

if not pyproject_file:
rollback_add_release_files(project)
raise click.ClickException("pyproject file not found")
if not pyproject_file:
rollback_add_release_files(project)
raise click.ClickException("pyproject file not found")

project.repo.add(pyproject_file)
project.repo.add(pyproject_file)

# Add release notes file
notes_file = os.path.join(project.releases_path, version + '.md')
Expand Down
4 changes: 4 additions & 0 deletions release_tools/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def add(self, filename):
cmd = ['git', 'add', filename]
self._exec(cmd, cwd=self.dirpath, env=self.gitenv)

def add_all(self):
cmd = ['git', 'add', '-A']
self._exec(cmd, cwd=self.dirpath, env=self.gitenv)

def rm(self, filename):
cmd = ['git', 'rm', '-f', filename]
self._exec(cmd, cwd=self.dirpath, env=self.gitenv)
Expand Down
11 changes: 11 additions & 0 deletions releases/unreleased/option-to-add-all-files-to-release-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Option to add all files to release commit
category: added
author: Eva Millán <[email protected]>
issue: null
notes: >
To include all changed files in the release commit,
add the `--add-all` flag to the `publish` command.
This is useful for non-Python projects that may
need to change the version number in files other
than '_version.py'.
34 changes: 34 additions & 0 deletions tests/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,40 @@ def test_publish_different_branch(self, mock_project):
mock_project.return_value.repo.push.assert_any_call('myremote', 'main')
mock_project.return_value.repo.push.assert_any_call('myremote', '0.8.10')

@unittest.mock.patch('release_tools.publish.read_changelog_entries')
@unittest.mock.patch('release_tools.publish.Project')
def test_add_all(self, mock_project, mock_read_changelog):
"""Test if '--add-all' adds all files to the release commit."""
version_number = "0.8.10"

runner = click.testing.CliRunner()

with runner.isolated_filesystem() as fs:
os.path.join(fs, 'package.json')
os.path.join(fs, 'Dockerfile')
notes_file = os.path.join(fs, version_number + '.md')
news_file = os.path.join(fs, 'NEWS')
authors_file = os.path.join(fs, 'AUTHORS')

self.setup_release_notes(fs, notes_file, newsfile=news_file, authorsfile=authors_file)

mock_project.return_value.unreleased_processed_entries_path = fs
mock_project.return_value.releases_path = fs
mock_project.return_value.news_file = news_file
mock_project.return_value.authors_file = authors_file

# Run the command
result = runner.invoke(publish.publish,
[version_number, "John Smith <[email protected]>",
"--add-all"])
self.assertEqual(result.exit_code, 0)

# Check mock calls
mock_project.return_value.repo.add_all.assert_called()
mock_project.return_value.repo.add.assert_any_call(notes_file)
mock_project.return_value.repo.add.assert_any_call(news_file)
mock_project.return_value.repo.add.assert_any_call(authors_file)


if __name__ == '__main__':
unittest.main()

0 comments on commit a5156f0

Please sign in to comment.