Skip to content

Commit

Permalink
delete release creator stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Mar 16, 2024
1 parent c1336fe commit 31c2af7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 84 deletions.
72 changes: 15 additions & 57 deletions porcupine/plugins/update_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,8 @@ def get_date(version: str) -> datetime.date:
return datetime.date(year, month, day)


def fetch_release_creator(version: str) -> str | None:
"""Find out who created a release.
Unfortunately the releases appear as being created by the GitHub Actions
bot account, so we look for a commit created by a script that Porcupine
maintainers run locally.
"""

# Commit date may be off by a day because time zones
start_time = (get_date(version) - datetime.timedelta(days=1)).isoformat() + ":00:00:00Z"
end_time = (get_date(version) + datetime.timedelta(days=1)).isoformat() + ":23:59:59Z"

try:
response = requests.get(
"https://api.github.com/repos/Akuli/porcupine/commits",
params={"since": start_time, "until": end_time},
headers={"Accept": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28"},
timeout=3,
)
response.raise_for_status()
except requests.RequestException:
log.info(f"error fetching commits around release date {version}", exc_info=True)
return None

for commit in response.json():
if commit["commit"]["message"] == f"Version v{version.lstrip('v')}":
return commit["author"]["login"]

# script no longer used in a future version of Porcupine?
return None


def fetch_release_info() -> tuple[str, str | None] | None:
"""Returns (when_released, who_released) for the latest release.
def get_latest_release() -> str | None:
"""Returns name of the latest release, or None if Porcupine is up to date.
This is slow, and runs in a new thread.
"""
Expand All @@ -101,37 +69,27 @@ def fetch_release_info() -> tuple[str, str | None] | None:
log.debug("this is the latest version of Porcupine")
return None

return (version, fetch_release_creator(version))

return version

def format_new_release_message(version: str, who_released: str | None) -> str:
some_days_ago = x_days_ago((datetime.date.today() - get_date(version)).days)
if who_released is None:
return f"A new version of Porcupine was released {some_days_ago}."
else:
return f"{who_released} released a new version of Porcupine {some_days_ago}."

def done_callback(success: bool, result: str | None) -> None:
if not success:
# Handle errors somewhat silently. Update checking is not very important.
log.warning("checking for updates failed")
log.info(f"full error message from update checking:\n{result}")
return

def check_for_updates_in_background() -> None:
def done_callback(success: bool, result: str | tuple[str, str | None] | None) -> None:
if not success:
# Handle errors somewhat silently. Update checking is not very important.
log.warning("checking for updates failed")
log.info(f"full error message from update checking:\n{result}")
return

assert not isinstance(result, str)
if result is not None:
# There is a new release
statusbar.set_global_message(format_new_release_message(*result))

utils.run_in_thread(fetch_release_info, done_callback)
if result is not None:
# There is a new release
some_days_ago = x_days_ago((datetime.date.today() - get_date(result)).days)
statusbar.set_global_message(f"A new version of Porcupine was released {some_days_ago}.")


def setup() -> None:
global_settings.add_option("update_check_on_startup", True)
settings.add_checkbutton(
"update_check_on_startup", text="Check for updates when Porcupine starts"
)

if global_settings.get("update_check_on_startup", bool):
check_for_updates_in_background()
utils.run_in_thread(get_latest_release, done_callback)
4 changes: 0 additions & 4 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ def main():
subprocess.check_call(
["git", "add", "porcupine/__init__.py", "README.md", "CHANGELOG.md", "pyproject.toml"]
)

# Avoid changing this commit message. It is used in the update_check plugin to figure
# out who ran this script to create a release.
subprocess.check_call(["git", "commit", "-m", f"Version {TAG_FORMAT % new_info}"])

subprocess.check_call(["git", "tag", TAG_FORMAT % new_info])
subprocess.check_call(["git", "push", "origin", branch])
subprocess.check_call(["git", "push", "--tags", "origin", branch])
Expand Down
26 changes: 3 additions & 23 deletions tests/test_update_check_plugin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import datetime
import os
import sys

import pytest

from porcupine.plugins import update_check

Expand Down Expand Up @@ -35,27 +31,11 @@ def test_x_days_ago():
assert update_check.x_days_ago(777) == "about 2 years and 2 months ago"


@pytest.mark.skipif(
sys.platform == "macos" and os.environ.get("GITHUB_ACTIONS") == "true",
reason="failed MacOS CI in github actions, don't know why",
)
def test_fetching_release_creator():
# I don't want to know which way it calls this function.
# Let's just make it work in both cases.
assert update_check.fetch_release_creator("2024.03.09") == "Akuli"
assert update_check.fetch_release_creator("v2024.03.09") == "Akuli"


def test_the_message(mocker):
mock_datetime = mocker.patch("porcupine.plugins.update_check.datetime")
mock_datetime.date.side_effect = datetime.date
mock_datetime.date.today.return_value = datetime.date(2024, 3, 16)
mock_set_message = mocker.patch("porcupine.plugins.statusbar.set_global_message")

assert (
update_check.format_new_release_message("v2024.03.09", None)
== "A new version of Porcupine was released 7 days ago."
)
assert (
update_check.format_new_release_message("v2024.03.09", "Akuli")
== "Akuli released a new version of Porcupine 7 days ago."
)
update_check.done_callback(True, "v2024.03.09")
mock_set_message.assert_called_once_with("A new version of Porcupine was released 7 days ago.")

0 comments on commit 31c2af7

Please sign in to comment.