|
| 1 | +import re |
| 2 | +import traceback |
| 3 | +import typing |
| 4 | + |
| 5 | +from github import Github |
| 6 | +from pydantic import BaseSettings, SecretStr |
| 7 | +from sphobjinv import Inventory |
| 8 | + |
| 9 | +docs_url = "https://codingame.readthedocs.io/en/latest/" |
| 10 | +roles = { |
| 11 | + "attr": "attribute", |
| 12 | + "meth": "method", |
| 13 | + "exc": "exception", |
| 14 | +} |
| 15 | +refs = { |
| 16 | + "async": ( |
| 17 | + "Asynchronous client", |
| 18 | + "user_guide/quickstart.html#about-the-asynchronous-client", |
| 19 | + ), |
| 20 | + "login": ( |
| 21 | + "Login", |
| 22 | + "user_guide/quickstart.html#login", |
| 23 | + ), |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +class Settings(BaseSettings): |
| 28 | + input_token: SecretStr |
| 29 | + github_repository: str |
| 30 | + github_ref: str |
| 31 | + |
| 32 | + |
| 33 | +def main(): |
| 34 | + settings = Settings(_env_file=".github/actions/changelog/.env") |
| 35 | + inventory = Inventory(url=docs_url + "objects.inv") |
| 36 | + github = Github(settings.input_token.get_secret_value()) |
| 37 | + repo = github.get_repo(settings.github_repository) |
| 38 | + docs_changelog = repo.get_contents( |
| 39 | + "docs/changelog.rst", settings.github_ref.split("/")[-1] |
| 40 | + ) |
| 41 | + |
| 42 | + content = docs_changelog.decoded_content.decode() |
| 43 | + new_content = content |
| 44 | + directives: typing.List[re.Match] = list( |
| 45 | + re.finditer(r":(\w+):`(.+?)`", content) |
| 46 | + ) |
| 47 | + links: typing.List[str] = [] |
| 48 | + |
| 49 | + for directive in directives: |
| 50 | + if directive[1] == "ref": |
| 51 | + links.append("`{} <{}>`__".format(*refs[directive[2]])) |
| 52 | + else: |
| 53 | + role = roles.get(directive[1], directive[1]) |
| 54 | + try: |
| 55 | + index = [ |
| 56 | + i |
| 57 | + for _, i in inventory.suggest( |
| 58 | + f":py:{role}:`codingame.{directive[2]}`", |
| 59 | + with_index=True, |
| 60 | + thresh=90, |
| 61 | + ) |
| 62 | + ][0] |
| 63 | + except IndexError: |
| 64 | + print( |
| 65 | + "::warning file=CHANGELOG.rst:: " |
| 66 | + f":py:{role}:`codingame.{directive[2]}` not found" |
| 67 | + ) |
| 68 | + links.append(f"``{directive[2]}``") |
| 69 | + continue |
| 70 | + |
| 71 | + obj = inventory.objects[index] |
| 72 | + links.append( |
| 73 | + f"`{obj.dispname_expanded[len('codingame.'):]} " |
| 74 | + f"<{docs_url + obj.uri_expanded}>`__" |
| 75 | + ) |
| 76 | + |
| 77 | + for directive, link in zip(directives[::-1], links[::-1]): |
| 78 | + new_content = ( |
| 79 | + new_content[: directive.start()] |
| 80 | + + link |
| 81 | + + new_content[directive.end() :] # noqa: E203 |
| 82 | + ) |
| 83 | + |
| 84 | + new_content = new_content[ |
| 85 | + len(".. currentmodule:: codingame\n\n") : # noqa: E203 |
| 86 | + ] |
| 87 | + |
| 88 | + changelog = repo.get_contents( |
| 89 | + "CHANGELOG.rst", settings.github_ref.split("/")[-1] |
| 90 | + ) |
| 91 | + if new_content != changelog.decoded_content.decode(): |
| 92 | + repo.update_file( |
| 93 | + changelog.path, |
| 94 | + "Update CHANGELOG.rst", |
| 95 | + new_content, |
| 96 | + changelog.sha, |
| 97 | + branch=settings.github_ref.split("/")[-1], |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + try: |
| 103 | + main() |
| 104 | + except Exception as e: |
| 105 | + print( |
| 106 | + "::error file=.github/actions/changelog/main.py," |
| 107 | + f"title={e.__class__.__name__}: {str(e)}:: " |
| 108 | + + traceback.format_exc() |
| 109 | + ) |
| 110 | + raise e from None |
0 commit comments