Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stale Issues Tracking Attempted #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ async def setup_hook(self) -> None:
"src.roles",
"src.welcome",
"src.testing",
"src.issues",
)
for i, extension in enumerate(extensions):
try:
Expand Down
108 changes: 108 additions & 0 deletions src/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,115 @@ async def get_software_projects(self) -> list[SoftwareProject]:
projects.sort(key=lambda p: p.title)
return projects

async def get_software_issues(
self,
is_open = True,
updated_after: datetime.datetime | None = None,
assignee: str | None = None,
createdBy: str | None = None,
labels: list[str] | None = None,
):

query = """
query {
organization(login: "uf-mil") {
repositories (first: 5, hasIssuesEnabled: true) {
edges {
node {
name
}
}
}
}
}
"""

software_issues = dict()
response = await self.fetch(
url = "https://api.github.com/graphql",
method="POST",
data=json.dumps({"query": query}),
)

for repo in response["data"]["organization"]["repositories"]["edges"]:
issues = await self.bot.github.get_repo_issues(
name = repo["node"]["name"],
is_open = is_open,
updated_after = updated_after,
assignee = assignee,
createdBy = createdBy,
labels = labels,
)
software_issues.update({(repo["node"]["name"]): issues})

return software_issues

async def get_repo_issues(
self,
name: str,
is_open = True,
updated_after: datetime.datetime | None = None,
after: str = "",
assignee: str | None = None,
createdBy: str | None = None,
labels: list[str] | None = None,
):

query = f"""
query {{
organization(login: \"uf-mil\") {{
repository(name: \"{name}\") {{
issues(
first: 100,
states: {'OPEN' if is_open else 'CLOSED'},
after : \"{after}\",
filterBy: {{
{'assignee : \"' + assignee + '\"' if assignee else ""},
{'createdBy : \"' + createdBy + '\"' if createdBy else ""},
{'labels : \"' + labels + '\"' if labels else "" },
{'since : \"' + datetime.datetime.isoformat(updated_after) + '\"' if updated_after else ""}
}}
) {{
edges {{
node {{
title
id
bodyText
createdAt
updatedAt
url
}}
}}

pageInfo {{
endCursor
hasNextPage
}}
}}
}}
}}
}}
"""

response = await self.fetch(
url = "https://api.github.com/graphql",
method="POST",
data= json.dumps({"query": query})
)

software_issues = list()
for issue in response["data"]["organization"]["repository"]["issues"]["edges"]:
software_issues.append(issue["node"])

if response["data"]["organization"]["repository"]["issues"]["pageInfo"]["hasNextPage"]:
software_issues.append(
name = name,
is_open = is_open,
after = response["data"]["organization"]["repository"]["issues"]["pageInfo"]["endCursor"],
)

return software_issues

class GitHubCog(commands.Cog):
def __init__(self, bot: MILBot):
self.bot = bot
Expand Down
170 changes: 170 additions & 0 deletions src/issues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
from __future__ import annotations

import calendar
import datetime
from typing import TYPE_CHECKING

import discord
from discord.ext import commands, tasks

from .github_types import Issue
from .reports import Team
from .tasks import run_on_weekday
from .views import MILBotView

if TYPE_CHECKING:
from .bot import MILBot


class StaleSelect(discord.ui.Select):
def __init__(self, stale_issues: dict):
self.issues = stale_issues
self.index = 0
self.cache = dict()
self.buttons = MILBotView()
self.embed = discord.Embed(
title="Potentially Stale Issue",
color=discord.Color.teal(),
description= ""
)

options = []
for repo in stale_issues:
options.append(
discord.SelectOption(
label = repo,
value = repo,
description = f"{len(stale_issues[repo])} issues to review.",
),
)

super().__init__(
#custom_id="softwareproject:projectname",
placeholder="Choose a repo to review...",
options=options,
max_values=len(options),
)

async def callback(self, interaction: discord.Interaction):
if not isinstance(interaction.user, discord.Member):
return

for value in self.values:
self.cache.update({interaction.user.name : value})

self.buttons.add_item(
self.StaleButton(
label="Dismiss",
style=discord.ButtonStyle.green,
custom_id="issue_stale:dismiss",
stale = self
)
)

self.buttons.add_item(
self.StaleButton(
label="Close",
style=discord.ButtonStyle.red,
custom_id="issue_stale:close",
stale = self
)
)

repo = self.cache[interaction.user.name]
self.buttons.add_item(
discord.ui.Button(
label="Link",
url=self.issues[repo][self.index]['url'],
)
)

self.update_embed(interaction)
await interaction.response.send_message(embed=self.embed, view=self.buttons, ephemeral=True)

def update_embed(self, interaction: discord.Interaction):
repo = self.cache[interaction.user.name]
limit = 800 #embed char limit = 1024
limit -= len(self.issues[repo][self.index]['title']) - len(self.issues[repo][self.index]['id'])
body = (self.issues[repo][self.index]['bodyText'][:limit] + "...") if len(self.issues[repo][self.index]['bodyText']) > limit else self.issues[repo][self.index]['bodyText']

self.embed.description = f"Issue {self.index + 1} of {len(self.issues[repo])}."
self.embed.clear_fields()
self.embed.add_field(
name=f"#{self.issues[repo][self.index]['title']}",
value=f"ID: {self.issues[repo][self.index]['id']}\nDescription: {body}\n",
inline=False,
)
self.buttons.children[2].url = self.issues[repo][self.index]['url']

class StaleButton(discord.ui.Button):
def __init__(self, stale: StaleSelect, style: discord.ButtonStyle, label: str, custom_id: str, emoji: str | None = None):
self.stale = stale
super().__init__(
style=style,
label=label,
custom_id = custom_id,
emoji=emoji,
)

async def callback(self, interaction: discord.Interaction):
self.stale.index += 1

repo = self.stale.cache[interaction.user.name]
if self.stale.index == len(self.stale.issues[repo]):
await interaction.response.send_message(f"Thank you for reviewing these issues.", ephemeral=True)

else:
self.stale.update_embed(interaction)
await interaction.response.defer()
await interaction.edit_original_response(embed = self.stale.embed, view = self.stale.buttons)

#if self.label == "Close":
#TODO Close functionality



class SoftwareIssuesView(MILBotView):
def __init__(self, stale_issues: list[dict]):
super().__init__(timeout=None)
self.add_item(StaleSelect(stale_issues))

class SoftwareIssues(commands.Cog):
def __init__(self, bot: MILBot):
self.bot = bot
self.get_old_issues.start()

@run_on_weekday(calendar.MONDAY, 0, 0)
#@tasks.loop(seconds=600)
async def get_old_issues(self):
await self.bot.wait_until_ready()
open_issues = await self.bot.github.get_software_issues()

stale_issues: dict[list] = dict()
past = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(weeks=4)
for repo in open_issues.keys():
if len(open_issues[repo]) == 0:
continue

issues = []
for issue in open_issues[repo]:
if datetime.datetime.fromisoformat(issue["updatedAt"]) < past:
issues.append(issue)

if len(issues) != 0:
stale_issues[repo] = issues

if (len(stale_issues) == 0):
return

embed = discord.Embed(
title="Potentially Stale Topics for Review",
color=discord.Color.teal(),
description= f"These issues have not been updated since {datetime.datetime.isoformat(past, timespec = "minutes")}.",
)

view = SoftwareIssuesView(stale_issues)
await self.bot.team_leads_ch(Team.SOFTWARE).send(embed=embed, view=view)

async def setup(bot: MILBot):
await bot.add_cog(SoftwareIssues(bot))

Loading