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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
import logging
import random
import re
import typing as t
from dataclasses import dataclass
from datetime import datetime
from urllib.parse import quote
import discord
from aiohttp import ClientResponse
from discord.ext import commands
from bot.bot import Bot
from bot.constants import Colours, ERROR_REPLIES, Emojis, NEGATIVE_REPLIES, Tokens
log = logging.getLogger(__name__)
GITHUB_API_URL = "https://api.github.com"
REQUEST_HEADERS = {
"Accept": "application/vnd.github.v3+json"
}
REPOSITORY_ENDPOINT = "https://api.github.com/orgs/{org}/repos?per_page=100&type=public"
ISSUE_ENDPOINT = "https://api.github.com/repos/{user}/{repository}/issues/{number}"
PR_ENDPOINT = "https://api.github.com/repos/{user}/{repository}/pulls/{number}"
if Tokens.github:
REQUEST_HEADERS["Authorization"] = f"token {Tokens.github.get_secret_value()}"
CODE_BLOCK_RE = re.compile(
r"^`([^`\n]+)`" # Inline codeblock
r"|```(.+?)```", # Multiline codeblock
re.DOTALL | re.MULTILINE
)
# Maximum number of issues in one message
MAXIMUM_ISSUES = 5
# Regex used when looking for automatic linking in messages
# regex101 of current regex https://regex101.com/r/V2ji8M/6
AUTOMATIC_REGEX = re.compile(
r"((?P<org>[a-zA-Z0-9][a-zA-Z0-9\-]{1,39})\/)?(?P<repo>[\w\-\.]{1,100})#(?P<number>[0-9]+)"
)
@dataclass(eq=True, frozen=True)
class FoundIssue:
"""Dataclass representing an issue found by the regex."""
organisation: t.Optional[str]
repository: str
number: str
@dataclass(eq=True, frozen=True)
class FetchError:
"""Dataclass representing an error while fetching an issue."""
return_code: int
message: str
@dataclass(eq=True, frozen=True)
class IssueState:
"""Dataclass representing the state of an issue."""
repository: str
number: int
url: str
title: str
emoji: str
class GithubInfo(commands.Cog):
"""A Cog that fetches info from GitHub."""
def __init__(self, bot: Bot):
self.bot = bot
self.repos = []
@staticmethod
def remove_codeblocks(message: str) -> str:
"""Remove any codeblock in a message."""
return CODE_BLOCK_RE.sub("", message)
async def fetch_issue(
self,
number: int,
repository: str,
user: str
) -> t.Union[IssueState, FetchError]:
"""
Retrieve an issue from a GitHub repository.
Returns IssueState on success, FetchError on failure.
"""
url = ISSUE_ENDPOINT.format(user=user, repository=repository, number=number)
pulls_url = PR_ENDPOINT.format(user=user, repository=repository, number=number)
json_data, r = await self.fetch_data(url)
if r.status == 403:
if r.headers.get("X-RateLimit-Remaining") == "0":
log.info(f"Ratelimit reached while fetching {url}")
return FetchError(403, "Ratelimit reached, please retry in a few minutes.")
return FetchError(403, "Cannot access issue.")
elif r.status in (404, 410):
return FetchError(r.status, "Issue not found.")
elif r.status != 200:
return FetchError(r.status, "Error while fetching issue.")
# The initial API request is made to the issues API endpoint, which will return information
# if the issue or PR is present. However, the scope of information returned for PRs differs
# from issues: if the 'issues' key is present in the response then we can pull the data we
# need from the initial API call.
if "issues" in json_data["html_url"]:
if json_data.get("state") == "open":
emoji = Emojis.issue_open
else:
emoji = Emojis.issue_closed
# If the 'issues' key is not contained in the API response and there is no error code, then
# we know that a PR has been requested and a call to the pulls API endpoint is necessary
# to get the desired information for the PR.
else:
pull_data, _ = await self.fetch_data(pulls_url)
if pull_data["draft"]:
emoji = Emojis.pull_request_draft
elif pull_data["state"] == "open":
emoji = Emojis.pull_request_open
# When 'merged_at' is not None, this means that the state of the PR is merged
elif pull_data["merged_at"] is not None:
emoji = Emojis.pull_request_merged
else:
emoji = Emojis.pull_request_closed
issue_url = json_data.get("html_url")
return IssueState(repository, number, issue_url, json_data.get("title", ""), emoji)
@staticmethod
def format_embed(
results: t.List[t.Union[IssueState, FetchError]]
) -> discord.Embed:
"""Take a list of IssueState or FetchError and format a Discord embed for them."""
description_list = []
for result in results:
if isinstance(result, IssueState):
description_list.append(
f"{result.emoji} [[{result.repository}] #{result.number} {result.title}]({result.url})"
)
elif isinstance(result, FetchError):
description_list.append(f":x: [{result.return_code}] {result.message}")
resp = discord.Embed(
colour=Colours.bright_green,
description="\n".join(description_list)
)
resp.set_author(name="GitHub")
return resp
@commands.group(name="github", aliases=("gh", "git"))
@commands.cooldown(1, 10, commands.BucketType.user)
async def github_group(self, ctx: commands.Context) -> None:
"""Commands for finding information related to GitHub."""
if ctx.invoked_subcommand is None:
await self.bot.invoke_help_command(ctx)
@commands.Cog.listener()
async def on_message(self, message: discord.Message) -> None:
"""
Automatic issue linking.
Listener to retrieve issue(s) from a GitHub repository using automatic linking if matching <org>/<repo>#<issue>.
"""
# Ignore bots
if message.author.bot:
return
issues = [
FoundIssue(*match.group("org", "repo", "number"))
for match in AUTOMATIC_REGEX.finditer(self.remove_codeblocks(message.content))
]
links = []
if issues:
# Block this from working in DMs
if not message.guild:
return
log.trace(f"Found {issues = }")
# Remove duplicates
issues = list(dict.fromkeys(issues))
if len(issues) > MAXIMUM_ISSUES:
embed = discord.Embed(
title=random.choice(ERROR_REPLIES),
color=Colours.soft_red,
description=f"Too many issues/PRs! (maximum of {MAXIMUM_ISSUES})"
)
await message.channel.send(embed=embed, delete_after=5)
return
for repo_issue in issues:
result = await self.fetch_issue(
int(repo_issue.number),
repo_issue.repository,
repo_issue.organisation or "python-discord"
)
if isinstance(result, IssueState):
links.append(result)
if not links:
return
resp = self.format_embed(links)
await message.channel.send(embed=resp)
async def fetch_data(self, url: str) -> tuple[dict[str], ClientResponse]:
"""Retrieve data as a dictionary and the response in a tuple."""
log.trace(f"Querying GH issues API: {url}")
async with self.bot.http_session.get(url, headers=REQUEST_HEADERS) as r:
return await r.json(), r
@github_group.command(name="user", aliases=("userinfo",))
async def github_user_info(self, ctx: commands.Context, username: str) -> None:
"""Fetches a user's GitHub information."""
async with ctx.typing():
user_data, _ = await self.fetch_data(f"{GITHUB_API_URL}/users/{username}")
# User_data will not have a message key if the user exists
if "message" in user_data:
embed = discord.Embed(
title=random.choice(NEGATIVE_REPLIES),
description=f"The profile for `{username}` was not found.",
colour=Colours.soft_red
)
await ctx.send(embed=embed)
return
org_data, _ = await self.fetch_data(user_data["organizations_url"])
orgs = [f"[{org['login']}](https://github.com/{org['login']})" for org in org_data]
orgs_to_add = " | ".join(orgs)
gists = user_data["public_gists"]
# Forming blog link
if user_data["blog"].startswith("http"): # Blog link is complete
blog = user_data["blog"]
elif user_data["blog"]: # Blog exists but the link is not complete
blog = f"https://{user_data['blog']}"
else:
blog = "No website link available"
embed = discord.Embed(
title=f"`{user_data['login']}`'s GitHub profile info",
description=f"```\n{user_data['bio']}\n```\n" if user_data["bio"] else "",
colour=discord.Colour.og_blurple(),
url=user_data["html_url"],
timestamp=datetime.strptime(user_data["created_at"], "%Y-%m-%dT%H:%M:%SZ")
)
embed.set_thumbnail(url=user_data["avatar_url"])
embed.set_footer(text="Account created at")
if user_data["type"] == "User":
embed.add_field(
name="Followers",
value=f"[{user_data['followers']}]({user_data['html_url']}?tab=followers)"
)
embed.add_field(
name="Following",
value=f"[{user_data['following']}]({user_data['html_url']}?tab=following)"
)
embed.add_field(
name="Public repos",
value=f"[{user_data['public_repos']}]({user_data['html_url']}?tab=repositories)"
)
if user_data["type"] == "User":
embed.add_field(name="Gists", value=f"[{gists}](https://gist.github.com/{quote(username, safe='')})")
embed.add_field(
name=f"Organization{'s' if len(orgs)!=1 else ''}",
value=orgs_to_add if orgs else "No organizations."
)
embed.add_field(name="Website", value=blog)
await ctx.send(embed=embed)
@github_group.command(name='repository', aliases=('repo',))
async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
"""
Fetches a repositories' GitHub information.
The repository should look like `user/reponame` or `user reponame`.
"""
repo = "/".join(repo)
if repo.count("/") != 1:
embed = discord.Embed(
title=random.choice(NEGATIVE_REPLIES),
description="The repository should look like `user/reponame` or `user reponame`.",
colour=Colours.soft_red
)
await ctx.send(embed=embed)
return
async with ctx.typing():
repo_data, _ = await self.fetch_data(f"{GITHUB_API_URL}/repos/{quote(repo)}")
# There won't be a message key if this repo exists
if "message" in repo_data:
embed = discord.Embed(
title=random.choice(NEGATIVE_REPLIES),
description="The requested repository was not found.",
colour=Colours.soft_red
)
await ctx.send(embed=embed)
return
embed = discord.Embed(
title=repo_data["name"],
description=repo_data["description"],
colour=discord.Colour.og_blurple(),
url=repo_data["html_url"]
)
# If it's a fork, then it will have a parent key
try:
parent = repo_data["parent"]
embed.description += f"\n\nForked from [{parent['full_name']}]({parent['html_url']})"
except KeyError:
log.debug("Repository is not a fork.")
repo_owner = repo_data["owner"]
embed.set_author(
name=repo_owner["login"],
url=repo_owner["html_url"],
icon_url=repo_owner["avatar_url"]
)
repo_created_at = datetime.strptime(repo_data["created_at"], "%Y-%m-%dT%H:%M:%SZ").strftime("%d/%m/%Y")
last_pushed = datetime.strptime(repo_data["pushed_at"], "%Y-%m-%dT%H:%M:%SZ").strftime("%d/%m/%Y at %H:%M")
embed.set_footer(
text=(
f"{repo_data['forks_count']} ⑂ "
f"• {repo_data['stargazers_count']} ⭐ "
f"• Created At {repo_created_at} "
f"• Last Commit {last_pushed}"
)
)
await ctx.send(embed=embed)
async def setup(bot: Bot) -> None:
"""Load the GithubInfo cog."""
await bot.add_cog(GithubInfo(bot))
|