Skip to content

Commit b2ed216

Browse files
authored
chore: fix linting (#60)
1 parent 6eba7f3 commit b2ed216

1 file changed

Lines changed: 53 additions & 30 deletions

File tree

bot/exts/utilities/githubinfo.py

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
GITHUB_API_URL = "https://api.github.com"
2020

21-
REQUEST_HEADERS = {"Accept": "application/vnd.github.v3+json"}
21+
REQUEST_HEADERS = {
22+
"Accept": "application/vnd.github.v3+json"
23+
}
2224

2325
REPOSITORY_ENDPOINT = "https://api.github.com/orgs/{org}/repos?per_page=100&type=public"
2426
MOST_STARRED_ENDPOINT = "https://api.github.com/search/repositories?q={name}&sort=stars&order=desc&per_page=100"
@@ -32,9 +34,9 @@
3234
REQUEST_HEADERS["Authorization"] = f"token {Tokens.github.get_secret_value()}"
3335

3436
CODE_BLOCK_RE = re.compile(
35-
r"`([^`\n]+)`" # Inline codeblock
37+
r"`([^`\n]+)`" # Inline codeblock
3638
r"|```(.+?)```", # Multiline codeblock
37-
re.DOTALL | re.MULTILINE,
39+
re.DOTALL | re.MULTILINE
3840
)
3941

4042
# Maximum number of issues in one message
@@ -102,12 +104,18 @@ async def cog_unload(self) -> None:
102104
"""
103105
self.refresh_repos.cancel()
104106

107+
105108
@staticmethod
106109
def remove_codeblocks(message: str) -> str:
107110
"""Remove any codeblock in a message."""
108111
return CODE_BLOCK_RE.sub("", message)
109112

110-
async def fetch_issue(self, number: int, repository: str, user: str) -> IssueState | FetchError:
113+
async def fetch_issue(
114+
self,
115+
number: int,
116+
repository: str,
117+
user: str
118+
) -> IssueState | FetchError:
111119
"""
112120
Retrieve an issue from a GitHub repository.
113121
@@ -159,7 +167,9 @@ async def fetch_issue(self, number: int, repository: str, user: str) -> IssueSta
159167
return IssueState(repository, number, issue_url, json_data.get("title", ""), emoji)
160168

161169
@staticmethod
162-
def format_embed(results: list[IssueState | FetchError]) -> discord.Embed:
170+
def format_embed(
171+
results: list[IssueState | FetchError]
172+
) -> discord.Embed:
163173
"""Take a list of IssueState or FetchError and format a Discord embed for them."""
164174
description_list = []
165175

@@ -171,7 +181,10 @@ def format_embed(results: list[IssueState | FetchError]) -> discord.Embed:
171181
elif isinstance(result, FetchError):
172182
description_list.append(f":x: [{result.return_code}] {result.message}")
173183

174-
resp = discord.Embed(colour=Colours.bright_green, description="\n".join(description_list))
184+
resp = discord.Embed(
185+
colour=Colours.bright_green,
186+
description="\n".join(description_list)
187+
)
175188

176189
resp.set_author(name="GitHub")
177190
return resp
@@ -213,14 +226,16 @@ async def on_message(self, message: discord.Message) -> None:
213226
embed = discord.Embed(
214227
title=random.choice(ERROR_REPLIES),
215228
color=Colours.soft_red,
216-
description=f"Too many issues/PRs! (maximum of {MAXIMUM_ISSUES})",
229+
description=f"Too many issues/PRs! (maximum of {MAXIMUM_ISSUES})"
217230
)
218231
await message.channel.send(embed=embed, delete_after=5)
219232
return
220233

221234
for repo_issue in issues:
222235
result = await self.fetch_issue(
223-
int(repo_issue.number), repo_issue.repository, repo_issue.organisation or "python-discord"
236+
int(repo_issue.number),
237+
repo_issue.repository,
238+
repo_issue.organisation or "python-discord"
224239
)
225240
if isinstance(result, IssueState):
226241
links.append(result)
@@ -248,7 +263,7 @@ async def github_user_info(self, ctx: commands.Context, username: str) -> None:
248263
embed = discord.Embed(
249264
title=random.choice(NEGATIVE_REPLIES),
250265
description=f"The profile for `{username}` was not found.",
251-
colour=Colours.soft_red,
266+
colour=Colours.soft_red
252267
)
253268

254269
await ctx.send(embed=embed)
@@ -273,29 +288,33 @@ async def github_user_info(self, ctx: commands.Context, username: str) -> None:
273288
description=f"```\n{user_data['bio']}\n```\n" if user_data["bio"] else "",
274289
colour=discord.Colour.og_blurple(),
275290
url=user_data["html_url"],
276-
timestamp=datetime.strptime(user_data["created_at"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=UTC),
291+
timestamp=datetime.strptime(user_data["created_at"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=UTC)
277292
)
278293
embed.set_thumbnail(url=user_data["avatar_url"])
279294
embed.set_footer(text="Account created at")
280295

281296
if user_data["type"] == "User":
297+
282298
embed.add_field(
283-
name="Followers", value=f"[{user_data['followers']}]({user_data['html_url']}?tab=followers)"
299+
name="Followers",
300+
value=f"[{user_data['followers']}]({user_data['html_url']}?tab=followers)"
284301
)
285302
embed.add_field(
286-
name="Following", value=f"[{user_data['following']}]({user_data['html_url']}?tab=following)"
303+
name="Following",
304+
value=f"[{user_data['following']}]({user_data['html_url']}?tab=following)"
287305
)
288306

289307
embed.add_field(
290-
name="Public repos", value=f"[{user_data['public_repos']}]({user_data['html_url']}?tab=repositories)"
308+
name="Public repos",
309+
value=f"[{user_data['public_repos']}]({user_data['html_url']}?tab=repositories)"
291310
)
292311

293312
if user_data["type"] == "User":
294313
embed.add_field(name="Gists", value=f"[{gists}](https://gist.github.com/{quote(username, safe='')})")
295314

296315
embed.add_field(
297316
name=f"Organization{'s' if len(orgs) != 1 else ''}",
298-
value=orgs_to_add if orgs else "No organizations.",
317+
value=orgs_to_add if orgs else "No organizations."
299318
)
300319
embed.add_field(name="Website", value=blog)
301320

@@ -314,7 +333,7 @@ def build_embed(self, repo_data: dict) -> discord.Embed:
314333
title=repo_data["name"],
315334
description=repo_data["description"],
316335
colour=discord.Colour.og_blurple(),
317-
url=repo_data["html_url"],
336+
url=repo_data["html_url"]
318337
)
319338
# if its a fork it will have a parent key
320339
try:
@@ -324,17 +343,19 @@ def build_embed(self, repo_data: dict) -> discord.Embed:
324343
log.debug("Repository is not a fork.")
325344

326345
repo_owner = repo_data["owner"]
327-
embed.set_author(name=repo_owner["login"], url=repo_owner["html_url"], icon_url=repo_owner["avatar_url"])
328-
329-
repo_created_at = (
330-
datetime.strptime(repo_data["created_at"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=UTC).strftime("%d/%m/%Y")
331-
)
332-
last_pushed = (
333-
datetime.strptime(repo_data["pushed_at"], "%Y-%m-%dT%H:%M:%SZ")
334-
.replace(tzinfo=UTC)
335-
.strftime("%d/%m/%Y at %H:%M")
346+
embed.set_author(
347+
name=repo_owner["login"],
348+
url=repo_owner["html_url"],
349+
icon_url=repo_owner["avatar_url"]
336350
)
337351

352+
repo_created_at = datetime.strptime(
353+
repo_data["created_at"], "%Y-%m-%dT%H:%M:%SZ"
354+
).replace(tzinfo=UTC).strftime("%d/%m/%Y")
355+
last_pushed = datetime.strptime(
356+
repo_data["pushed_at"], "%Y-%m-%dT%H:%M:%SZ"
357+
).replace(tzinfo=UTC).strftime("%d/%m/%Y at %H:%M")
358+
338359
embed.set_footer(
339360
text=(
340361
f"{repo_data['forks_count']:,} ⑂ "
@@ -603,11 +624,12 @@ async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
603624
repo_query = "/".join(repo)
604625
repo_query_casefold = repo_query.casefold()
605626

627+
606628
if repo_query.count("/") > 1:
607629
embed = discord.Embed(
608630
title=random.choice(NEGATIVE_REPLIES),
609631
description="There cannot be more than one `/` in the repository.",
610-
colour=Colours.soft_red,
632+
colour=Colours.soft_red
611633
)
612634
await ctx.send(embed=embed)
613635
return
@@ -621,10 +643,11 @@ async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
621643
is_pydis = True
622644
else:
623645
fetch_most_starred = True
646+
624647
async with ctx.typing():
625648
# Case 1: PyDis repo
626649
if is_pydis:
627-
repo_data = repo_query # repo_query already contains the matched repo
650+
repo_data = repo_query # repo_query already contains the matched repo
628651

629652
# Case 2: Not stored or PyDis, fetch most-starred matching repo
630653
elif fetch_most_starred:
@@ -634,7 +657,7 @@ async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
634657
embed = discord.Embed(
635658
title=random.choice(NEGATIVE_REPLIES),
636659
description=f"No repositories found matching `{repo_query}`.",
637-
colour=Colours.soft_red,
660+
colour=Colours.soft_red
638661
)
639662
await ctx.send(embed=embed)
640663
return
@@ -647,11 +670,12 @@ async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
647670
embed = discord.Embed(
648671
title=random.choice(NEGATIVE_REPLIES),
649672
description=f"No repositories found matching `{repo_query}`.",
650-
colour=Colours.soft_red,
673+
colour=Colours.soft_red
651674
)
652675
await ctx.send(embed=embed)
653676
return
654677

678+
655679
# Case 3: Regular GitHub repo
656680
else:
657681
repo_data, _ = await self.fetch_data(f"{GITHUB_API_URL}/repos/{quote(repo_query)}")
@@ -660,15 +684,14 @@ async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
660684
embed = discord.Embed(
661685
title=random.choice(NEGATIVE_REPLIES),
662686
description="The requested repository was not found.",
663-
colour=Colours.soft_red,
687+
colour=Colours.soft_red
664688
)
665689
await ctx.send(embed=embed)
666690
return
667691

668692
embed = self.build_embed(repo_data)
669693
await ctx.send(embed=embed)
670694

671-
672695
async def setup(bot: Bot) -> None:
673696
"""Load the GithubInfo cog."""
674697
await bot.add_cog(GithubInfo(bot))

0 commit comments

Comments
 (0)