Skip to content

Commit

Permalink
Add Releases section to generate_readme.py
Browse files Browse the repository at this point in the history
  • Loading branch information
soininen committed May 15, 2024
1 parent 8e8670b commit 6c56855
Showing 1 changed file with 53 additions and 15 deletions.
68 changes: 53 additions & 15 deletions generate_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime
import logging
from operator import attrgetter
from typing import Union

import mdutils
import requests
Expand All @@ -13,20 +14,36 @@
logging.basicConfig()
logger = logging.getLogger(__name__)

headers = {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28"
}


@dataclass
class Artifact:
name: str
download_url: str
created_at: datetime

def md_link(self) -> str:
label = f"{self.name}.zip"
return mdutils.tools.Link.Inline.new_link(self.download_url, label)


@dataclass
class ReleaseAsset:
version: str
name: str
download_url: str
created_at: datetime

def md_link(self) -> str:
return mdutils.tools.Link.Inline.new_link(self.download_url, self.name)


def request_artifacts() -> list[Artifact]:
artifacts: list[Artifact] = []
headers = {
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28"
}
current_url = "https://api.github.com/repos/spine-tools/Spine-Toolbox/actions/artifacts"
while current_url is not None and len(artifacts) < MAX_ARTIFACTS:
raw_response = requests.get(current_url, headers=headers)
Expand Down Expand Up @@ -60,19 +77,38 @@ def request_artifacts() -> list[Artifact]:
return artifacts


def build_link(artifact: Artifact) -> str:
label = f"{artifact.name}.zip"
return mdutils.tools.Link.Inline.new_link(artifact.download_url, label)
def build_link_list_items(items: list[Union[Artifact, ReleaseAsset]]) -> list[str]:
link_items: list[str] = []
for item in items:
link_items.append(f"{item.md_link()} ({item.created_at.strftime("%Y-%m-%d")})")
return link_items


snapshot_link_items = build_link_list_items(request_artifacts())


def build_link_list_items(artifacts: list[Artifact]) -> list[str]:
items = []
for artifact in artifacts:
items.append(f"{build_link(artifact)} ({artifact.created_at.strftime("%Y-%m-%d")})")
return items
def request_release_assets() -> list[ReleaseAsset]:
assets: list[ReleaseAsset] = []
url = "https://api.github.com/repos/spine-tools/Spine-Toolbox/releases"
raw_response = requests.get(url, headers=headers)
response = raw_response.json()
for release in response:
created_at = datetime.fromisoformat(release["published_at"])
if created_at < datetime(year=2024, month=5, day=1):
continue
version = release["tag_name"]
assets_url = release["assets_url"]
assets_raw_response = requests.get(assets_url, headers=headers)
assets_response = assets_raw_response.json()
for asset in assets_response:
name = asset["name"]
download_url = asset["browser_download_url"]
assets.append(ReleaseAsset(version, name, download_url, created_at))
return sorted(assets, key=attrgetter("created_at"))


link_items = build_link_list_items(request_artifacts())
release_link_items = build_link_list_items(request_release_assets())

readme = mdutils.MdUtils(file_name="README.md", title="Downloads")
readme.new_header(level=1, title="Spine Toolbox")
readme.new_header(level=2, title="Relocatable bundles for Windows")
Expand All @@ -96,11 +132,13 @@ def build_link_list_items(artifacts: list[Artifact]) -> list[str]:
see Toolbox [installation](https://github.com/spine-tools/Spine-Toolbox?tab=readme-ov-file#installation).
"""
)
readme.new_header(level=3, title="Development snapshots")
readme.new_header(level=3, title="Releases")
readme.write(
"""
Consider taking backups of your projects and Spine databases if you are upgrading from version 0.7.x.
"""
)
readme.new_list(link_items)
readme.new_list(release_link_items)
readme.new_header(level=3, title="Development snapshots")
readme.new_list(snapshot_link_items)
readme.create_md_file()

0 comments on commit 6c56855

Please sign in to comment.