Skip to content

Commit

Permalink
Merge pull request #25 from takos22/1.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
takos22 authored Feb 11, 2023
2 parents 78aa6ea + 304d368 commit 549e2c0
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 93 deletions.
162 changes: 141 additions & 21 deletions .github/actions/changelog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sphobjinv import Inventory

DOCS_BASE_URL = "https://codingame.readthedocs.io/en/"
STDLIB_DOCS_BASE_URL = "https://docs.python.org/"

ref_to_doc_branch = {"dev": "latest", "master": "stable"}
roles = {
Expand Down Expand Up @@ -40,6 +41,7 @@ def main():
docs_changelog = repo.get_contents(
"docs/changelog.rst", settings.github_ref.split("/")[-1]
)
log("debug", f"docs/changelog.rst at {settings.github_ref_name} downloaded")

docs_url = (
DOCS_BASE_URL
Expand All @@ -50,43 +52,100 @@ def main():
)
+ "/"
)
log("notice", f"Using docs at {docs_url}")

inventory = Inventory(url=docs_url + "objects.inv")
log("debug", "Downloaded codingame's inventory")
stdlib_inventory = Inventory(url=STDLIB_DOCS_BASE_URL + "objects.inv")
log("debug", "Downloaded stdlib's inventory")

content = docs_changelog.decoded_content.decode()
new_content = content
directives: typing.List[re.Match] = list(
re.finditer(r":(\w+):`(.+?)`", content)
)
log("debug", f"Found {len(directives)} in docs/changelog.rst")

links: typing.List[str] = []
cache: typing.Dict[str, int] = {}
stdlib_cache: typing.Dict[str, int] = {}

log("group", "Directive search")

for directive in directives:
if directive[1] == "ref":
links.append("`{} <{}>`__".format(*refs[directive[2]]))
else:
role = roles.get(directive[1], directive[1])
try:
index = [
role, name = directive.groups()
if role == "ref":
links.append("`{} <{}>`__".format(*refs[name]))
log("debug", f"Found :ref:`{name}`")
continue

role = roles.get(role, role)

index = None
stdlib = False
cached = False

if f"{role}:{name}" in cache:
index = cache[f"{role}:{name}"]
cached = True

if f"{role}:{name}" in stdlib_cache:
index = stdlib_cache[f"{role}:{name}"]
stdlib = True
cached = True

if index is None:
indexes = [
i
for _, i in inventory.suggest(
f":py:{role}:`codingame.{name}`",
with_index=True,
thresh=90,
)
]

if not indexes:
indexes = [
i
for _, i in inventory.suggest(
f":py:{role}:`codingame.{directive[2]}`",
for _, i in stdlib_inventory.suggest(
f":py:{role}:`{name}`",
with_index=True,
thresh=90,
)
][0]
except IndexError:
print(
"::warning file=CHANGELOG.rst:: "
f":py:{role}:`codingame.{directive[2]}` not found"
]
stdlib = True

if not indexes:
links.append(f"``{name}``")
log(
"warning",
f":py:{role}:`codingame.{name}` or "
f":py:{role}:`{name}` not found",
title="Directive not found",
)
links.append(f"``{directive[2]}``")
continue

obj = inventory.objects[index]
index = indexes[0]

if stdlib:
obj = stdlib_inventory.objects[index]
links.append(
f"`{obj.dispname_expanded[len('codingame.'):]} "
f"<{docs_url + obj.uri_expanded}>`__"
f"`{obj.dispname_expanded} "
f"<{STDLIB_DOCS_BASE_URL + obj.uri_expanded}>`__"
)
log("debug", f"Found :{role}:`{name}`" + " (cached)" * cached)
stdlib_cache[f"{role}:{name}"] = index
continue

obj = inventory.objects[index]
links.append(
f"`{obj.dispname_expanded[len('codingame.'):]} "
f"<{docs_url + obj.uri_expanded}>`__"
)
log("debug", f"Found :{role}:`codingame.{name}`" + " (cached)" * cached)
cache[f"{role}:{name}"] = index

log("endgroup")

for directive, link in zip(directives[::-1], links[::-1]):
new_content = (
Expand All @@ -102,6 +161,8 @@ def main():
changelog = repo.get_contents(
"CHANGELOG.rst", settings.github_ref.split("/")[-1]
)
log("debug", f"CHANGELOG.rst at {settings.github_ref_name} downloaded")

if new_content != changelog.decoded_content.decode():
repo.update_file(
changelog.path,
Expand All @@ -110,15 +171,74 @@ def main():
changelog.sha,
branch=settings.github_ref.split("/")[-1],
)
log(
"notice",
"Changelog's content changed, updated CHANGELOG.rst",
file="CHANGELOG.rst",
)
else:
log("notice", "Changelog's content hasn't changed")


LOG_PARAMETER_NAMES = {
"end_line": "endLine",
"column": "col",
"end_column": "endColumn",
}


def log(
level: str,
message: str = "",
title: str = None,
file: str = None,
line: int = None,
end_line: int = None,
column: int = None,
end_column: int = None,
):
parameters = dict(
filter(
lambda i: i[1] is not None,
{
"title": title,
"file": file,
"line": line,
"end_line": end_line,
"column": column,
"end_column": end_column,
}.items(),
)
)

print(
"::"
+ level
+ (
(
" "
+ ",".join(
f"{LOG_PARAMETER_NAMES.get(k, k)}={v}"
for k, v in parameters.items()
)
)
if parameters
else ""
)
+ "::"
+ message,
flush=True,
)


if __name__ == "__main__":
try:
main()
except Exception as e:
print(
"::error file=.github/actions/changelog/main.py,"
f"title={e.__class__.__name__}: {str(e)}:: "
+ traceback.format_exc()
log(
"error",
traceback.format_exc(),
title=f"{e.__class__.__name__}: {str(e)}",
file=".github/actions/changelog/main.py",
)
raise
6 changes: 5 additions & 1 deletion .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Generate CHANGELOG.rst from docs/changelog.rst

on: [push]
on:
push:
branches:
- "*"
workflow_dispatch:

jobs:
generate-changelog:
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

name: Lint and test

on: [push, pull_request]
on:
push:
pull_request:
workflow_dispatch:
schedule:
- cron: '0 8 * * 6' # every saturday at 8:00

jobs:
build:
Expand Down
Loading

0 comments on commit 549e2c0

Please sign in to comment.