|
| 1 | +# pyright: strict, reportUnknownMemberType=false |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import itertools |
| 6 | +import json |
| 7 | +import logging |
| 8 | +import os |
| 9 | +import re |
| 10 | +import requests |
| 11 | +from pathlib import Path |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | +logging.basicConfig(level=logging.DEBUG) |
| 16 | + |
| 17 | + |
| 18 | +def main(): |
| 19 | + gh_token = os.environ["gh_token"] |
| 20 | + hackage_token = os.environ["hackage_token"] |
| 21 | + version = os.environ["version"] |
| 22 | + sdistdir = os.environ["sdistdir"] |
| 23 | + repo = os.environ["GITHUB_REPOSITORY"] |
| 24 | + sha = os.environ["GITHUB_SHA"] |
| 25 | + |
| 26 | + version_name = f"v{version}" |
| 27 | + |
| 28 | + # check inputs |
| 29 | + if not hackage_token: |
| 30 | + raise Exception( |
| 31 | + "Hackage token is not provided (did you add a Secret of the form HACKAGE_TOKEN_<github username>?)" |
| 32 | + ) |
| 33 | + |
| 34 | + # ensure sdist exists |
| 35 | + sdist_archive = Path(sdistdir) / f"persistent-mtl-{version}.tar.gz" |
| 36 | + if not sdist_archive.exists(): |
| 37 | + raise Exception(f"File does not exist: {sdist_archive}") |
| 38 | + |
| 39 | + # check + parse CHANGELOG |
| 40 | + changelog = Path("CHANGELOG.md").read_text() |
| 41 | + changelog = re.sub(r"^# Unreleased\n+", "", changelog) |
| 42 | + if not changelog.startswith(f"# {version_name}"): |
| 43 | + raise Exception("CHANGELOG doesn't look updated") |
| 44 | + version_changes = get_version_changes(changelog) |
| 45 | + |
| 46 | + logger.info(f"Creating release {version_name}") |
| 47 | + create_github_release( |
| 48 | + repo=repo, |
| 49 | + token=gh_token, |
| 50 | + sha=sha, |
| 51 | + version_name=version_name, |
| 52 | + version_changes=version_changes, |
| 53 | + ) |
| 54 | + |
| 55 | + # uploading as candidate because uploads are irreversible, unlike |
| 56 | + # GitHub releases, so just to be extra sure, we'll upload this as |
| 57 | + # a candidate and manually confirm uploading the package on Hackage |
| 58 | + upload_hackage_candidate( |
| 59 | + token=hackage_token, |
| 60 | + archive=sdist_archive, |
| 61 | + ) |
| 62 | + |
| 63 | + logger.info(f"Released persistent-mtl {version_name}!") |
| 64 | + |
| 65 | + |
| 66 | +def get_version_changes(changelog: str) -> str: |
| 67 | + lines = changelog.split("\n") |
| 68 | + |
| 69 | + # skip initial '# vX.Y.Z' line |
| 70 | + lines = lines[1:] |
| 71 | + |
| 72 | + # take lines until the next '# vX.Y.Z' line |
| 73 | + lines = itertools.takewhile(lambda line: not line.startswith("# v"), lines) |
| 74 | + |
| 75 | + return "\n".join(lines) |
| 76 | + |
| 77 | + |
| 78 | +def create_github_release( |
| 79 | + *, |
| 80 | + repo: str, |
| 81 | + token: str, |
| 82 | + sha: str, |
| 83 | + version_name: str, |
| 84 | + version_changes: str, |
| 85 | +): |
| 86 | + session = init_session() |
| 87 | + session.headers["Accept"] = "application/vnd.github.v3+json" |
| 88 | + session.headers["Authorization"] = f"token {token}" |
| 89 | + session.headers["User-Agent"] = repo |
| 90 | + |
| 91 | + payload = { |
| 92 | + "tag_name": version_name, |
| 93 | + "target_commitish": sha, |
| 94 | + "name": version_name, |
| 95 | + "body": version_changes, |
| 96 | + } |
| 97 | + logger.debug(f"Creating release with: {json.dumps(payload)}") |
| 98 | + |
| 99 | + session.post( |
| 100 | + f"https://api.github.com/repos/{repo}/releases", |
| 101 | + json=payload, |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def upload_hackage_candidate( |
| 106 | + *, |
| 107 | + token: str, |
| 108 | + archive: Path, |
| 109 | +): |
| 110 | + session = init_session() |
| 111 | + with archive.open("rb") as f: |
| 112 | + session.post( |
| 113 | + "https://hackage.haskell.org/packages/candidates", |
| 114 | + headers={"Authorization": f"X-ApiKey {token}"}, |
| 115 | + files={"package": f}, |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def init_session() -> requests.Session: |
| 120 | + session = requests.Session() |
| 121 | + |
| 122 | + def _check_status(r: requests.Response, *args: Any, **kwargs: Any): |
| 123 | + r.raise_for_status() |
| 124 | + |
| 125 | + # https://github.com/python/typeshed/issues/7776 |
| 126 | + session.hooks["response"].append( # pyright: ignore[reportFunctionMemberAccess] |
| 127 | + _check_status, |
| 128 | + ) |
| 129 | + |
| 130 | + return session |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + main() |
0 commit comments