Skip to content

Commit

Permalink
Merge pull request #1011 from alphagov/upgrade-version-script
Browse files Browse the repository at this point in the history
Add script to update apps to latest version
  • Loading branch information
quis committed Dec 12, 2022
2 parents 8a1d7ac + 43c90a3 commit 33906c2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This is only used for recording changes for major version bumps.
More minor changes may optionally be recorded here too.

## 59.3.0

* Add tooling to bump utils version in apps

## 59.2.0

* Add support for creating redis cache keys for a specific notification type.
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# - `make version-minor` for new features
# - `make version-patch` for bug fixes

__version__ = "59.2.0" # 878ba4ddf0ee30577340afab610b7a34
__version__ = "59.3.0" # 9a88d4e7655fc7856a9729df319308cd
76 changes: 76 additions & 0 deletions notifications_utils/version_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pathlib

import requests

requirements_file = pathlib.Path("requirements.in")
repo_name = "alphagov/notifications-utils"


class color:
PURPLE = "\033[95m"
CYAN = "\033[96m"
DARKCYAN = "\033[36m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
END = "\033[0m"


def upgrade_version():

current_version = get_app_version()
newest_version = get_remote_version()

write_version_to_requirements_file(newest_version)

print(
f"{color.GREEN}{color.BOLD}notifications-utils bumped to {newest_version}{color.END}\n\n"
f"{color.YELLOW}{color.UNDERLINE}Now run:{color.END}\n\n"
f" make freeze-requirements\n"
f" git add requirements* && git commit\n\n"
f"{color.YELLOW}{color.UNDERLINE}Suggested commit message:{color.END}\n\n"
f"Bump utils to {newest_version}\n\n"
f"{get_relevant_changelog_lines(current_version, newest_version)}\n***\n\n"
f"Complete changes: https://github.com/{repo_name}/compare/{current_version}...{newest_version}\n"
)


def get_remote_version():
exec(get_file_contents_from_github("main", "notifications_utils/version.py"))
return locals()["__version__"]


def get_app_version():
return next(line.split("@")[-1] for line in requirements_file.read_text().split("\n") if repo_name in line)


def write_version_to_requirements_file(version):
def replace_line(line):
if repo_name in line:
return f"notifications-utils @ git+https://github.com/{repo_name}.git@{version}\n"
return line

new_requirements_file_contents = "".join(
replace_line(line) for line in requirements_file.read_text().splitlines(True)
)

requirements_file.write_text(new_requirements_file_contents)


def get_relevant_changelog_lines(current_version, newest_version):

old_changelog, new_changelog = (
get_file_contents_from_github(version, "CHANGELOG.md") for version in (current_version, newest_version)
)

lines_added = new_changelog.count("\n") - old_changelog.count("\n")
header_lines = new_changelog.split("##")[0].count("\n")

return "\n".join(new_changelog.split("\n")[header_lines : header_lines + lines_added])


def get_file_contents_from_github(branch_or_tag, path):
return requests.get(f"https://raw.githubusercontent.com/{repo_name}/{branch_or_tag}/{path}").text
15 changes: 1 addition & 14 deletions scripts/bump_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path

from notifications_utils.version import __version__
from notifications_utils.version_tools import color

version_parts = ("major", "minor", "patch")

Expand Down Expand Up @@ -42,20 +43,6 @@
with Path("notifications_utils/version.py").open("w") as version_file:
version_file.write(output)


class color:
PURPLE = "\033[95m"
CYAN = "\033[96m"
DARKCYAN = "\033[36m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
END = "\033[0m"


print("")
print(
f"{color.BOLD}{color.GREEN}"
Expand Down

0 comments on commit 33906c2

Please sign in to comment.