Skip to content

Commit 8e22e3b

Browse files
authored
Merge pull request #12 from 1.2.x
2 parents 3d17020 + 3ae02d4 commit 8e22e3b

24 files changed

+984
-190
lines changed

.github/actions/changelog/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:3
2+
COPY requirements.txt /requirements.txt
3+
RUN pip install -r /requirements.txt
4+
5+
COPY main.py /main.py
6+
CMD ["python", "/main.py"]

.github/actions/changelog/action.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: "Generate CHANGELOG.rst"
2+
description: "Generate the CHANGELOG.tst file from docs/changelog.rst"
3+
author: "Takos22 <[email protected]>"
4+
inputs:
5+
token:
6+
description: "Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}"
7+
required: true
8+
runs:
9+
using: "docker"
10+
image: "Dockerfile"

.github/actions/changelog/main.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pygithub
2+
pydantic
3+
sphobjinv

.github/workflows/changelog.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Generate CHANGELOG.rst from docs/changelog.rst
2+
3+
on: [push]
4+
5+
jobs:
6+
generate-changelog:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: ./.github/actions/changelog
11+
with:
12+
token: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)