forked from modmail-dev/Modmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangelog.py
191 lines (160 loc) · 5.47 KB
/
changelog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import asyncio
import re
from subprocess import PIPE
from typing import List
from discord import Embed
from core.models import getLogger
from core.utils import truncate
logger = getLogger(__name__)
class Version:
"""
This class represents a single version of Modmail.
Parameters
----------
bot : Bot
The Modmail bot.
version : str
The version string (ie. "v2.12.0").
lines : str
The lines of changelog messages for this version.
Attributes
----------
bot : Bot
The Modmail bot.
version : str
The version string (ie. "v2.12.0").
lines : str
A list of lines of changelog messages for this version.
fields : Dict[str, str]
A dict of fields separated by "Fixed", "Changed", etc sections.
description : str
General description of the version.
Class Attributes
----------------
ACTION_REGEX : str
The regex used to parse the actions.
DESCRIPTION_REGEX: str
The regex used to parse the description.
"""
ACTION_REGEX = r"###\s*(.+?)\s*\n(.*?)(?=###\s*.+?|$)"
DESCRIPTION_REGEX = r"^(.*?)(?=###\s*.+?|$)"
def __init__(self, bot, branch: str, version: str, lines: str):
self.bot = bot
self.version = version.lstrip("vV")
self.lines = lines.strip()
self.fields = {}
self.changelog_url = f"https://github.com/kyb3r/modmail/blob/{branch}/CHANGELOG.md"
self.description = ""
self.parse()
def __repr__(self) -> str:
return f'Version(v{self.version}, description="{self.description}")'
def parse(self) -> None:
"""
Parse the lines and split them into `description` and `fields`.
"""
self.description = re.match(self.DESCRIPTION_REGEX, self.lines, re.DOTALL)
self.description = self.description.group(1).strip() if self.description is not None else ""
matches = re.finditer(self.ACTION_REGEX, self.lines, re.DOTALL)
for m in matches:
try:
self.fields[m.group(1).strip()] = m.group(2).strip()
except AttributeError:
logger.error(
"Something went wrong when parsing the changelog for version %s.",
self.version,
exc_info=True,
)
@property
def url(self) -> str:
return f"{self.changelog_url}#v{self.version[::2]}"
@property
def embed(self) -> Embed:
"""
Embed: the formatted `Embed` of this `Version`.
"""
embed = Embed(color=self.bot.main_color, description=self.description)
embed.set_author(
name=f"v{self.version} - Changelog",
icon_url=self.bot.user.avatar_url,
url=self.url,
)
for name, value in self.fields.items():
embed.add_field(name=name, value=truncate(value, 1024), inline=False)
embed.set_footer(text=f"Current version: v{self.bot.version}")
embed.set_thumbnail(url=self.bot.user.avatar_url)
return embed
class Changelog:
"""
This class represents the complete changelog of Modmail.
Parameters
----------
bot : Bot
The Modmail bot.
text : str
The complete changelog text.
Attributes
----------
bot : Bot
The Modmail bot.
text : str
The complete changelog text.
versions : List[Version]
A list of `Version`'s within the changelog.
Class Attributes
----------------
VERSION_REGEX : re.Pattern
The regex used to parse the versions.
"""
VERSION_REGEX = re.compile(
r"#\s*([vV]\d+\.\d+(?:\.\d+)?(?:-\w+?)?)\s+(.*?)(?=#\s*[vV]\d+\.\d+(?:\.\d+)(?:-\w+?)?|$)",
flags=re.DOTALL,
)
def __init__(self, bot, branch: str, text: str):
self.bot = bot
self.text = text
logger.debug("Fetching changelog from GitHub.")
self.versions = [Version(bot, branch, *m) for m in self.VERSION_REGEX.findall(text)]
@property
def latest_version(self) -> Version:
"""
Version: The latest `Version` of the `Changelog`.
"""
return self.versions[0]
@property
def embeds(self) -> List[Embed]:
"""
List[Embed]: A list of `Embed`'s for each of the `Version`.
"""
return [v.embed for v in self.versions]
@classmethod
async def from_url(cls, bot, url: str = "") -> "Changelog":
"""
Create a `Changelog` from a URL.
Parameters
----------
bot : Bot
The Modmail bot.
url : str, optional
The URL to the changelog.
Returns
-------
Changelog
The newly created `Changelog` parsed from the `url`.
"""
# get branch via git cli if available
proc = await asyncio.create_subprocess_shell(
"git branch --show-current",
stderr=PIPE,
stdout=PIPE,
)
err = await proc.stderr.read()
err = err.decode("utf-8").rstrip()
res = await proc.stdout.read()
branch = res.decode("utf-8").rstrip()
if not branch or err:
branch = "master" if not bot.version.is_prerelease else "development"
if branch not in ("master", "development"):
branch = "master"
url = url or f"https://raw.githubusercontent.com/kyb3r/modmail/{branch}/CHANGELOG.md"
async with await bot.session.get(url) as resp:
return cls(bot, branch, await resp.text())