Skip to content

Commit

Permalink
Add milestones and full project path to entries
Browse files Browse the repository at this point in the history
  • Loading branch information
subatiq committed Mar 3, 2023
1 parent 4e00e86 commit 4aa959d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
26 changes: 16 additions & 10 deletions src/issues/formating.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from datetime import datetime
from typing import Optional
from src.issues.issues import ClosedIssue, OpenedIssue
from src.issues.issues import ClosedIssue, OpenedIssue, get_closed_issues, get_opened_issues


def format_closed_issues(closed_issues: list[ClosedIssue]) -> Optional[str]:
def format_closed_issues(closed_issues: dict[str, list[ClosedIssue]]) -> Optional[str]:
if not closed_issues:
return None

message = f"# 🏆 Герои дня ({datetime.utcnow().date().isoformat()})\n"
message += "\n## Закрытые сегодня задачки\n"
for closed in closed_issues:
message += f'**{closed.champion}**\n\n'
for issue in [i for i in closed_issues if i.champion == closed.champion]:
message += f'- ✅ {issue.name} ([{issue.short_ref}]({issue.url}))\n'
for champion, issues in closed_issues.items():
message += f'**{champion}**\n\n'
for issue in issues:
milestone = f'[{issue.milestone}] ' if issue.milestone else ''
message += f'- ✅ {milestone}{issue.name} ([{issue.short_ref}]({issue.url}))\n'

message += '\n'

Expand All @@ -25,14 +26,18 @@ def format_opened_issues(opened_issues: list[OpenedIssue]) -> Optional[str]:

message = "\n\n## Открытые сегодня задачки\n"

for opened in opened_issues:
message += f'- 🔲 {opened.name} ([{opened.short_ref}]({opened.url}))\n'
for issue in opened_issues:
milestone = f'[{issue.milestone}] ' if issue.milestone else ''
message += f'- 🔲 {milestone}{issue.name} ([{issue.short_ref}]({issue.url}))\n'

message += '\n\n'
return message


def format_notification(closed_issues: list[ClosedIssue], opened_issues: list[OpenedIssue]) -> Optional[str]:
def format_notification(
closed_issues: dict[str, list[ClosedIssue]],
opened_issues: list[OpenedIssue]
) -> Optional[str]:
closed_message = format_closed_issues(closed_issues) or ""
opened_message = format_opened_issues(opened_issues) or ""

Expand All @@ -43,4 +48,5 @@ def format_notification(closed_issues: list[ClosedIssue], opened_issues: list[Op

return message


if __name__ == "__main__":
print(format_notification(get_closed_issues(), get_opened_issues()))
35 changes: 24 additions & 11 deletions src/issues/issues.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from collections import defaultdict
from dataclasses import dataclass
from datetime import datetime, timedelta
import os
from typing import Optional

from urllib3.exceptions import HTTPError
import httpx
Expand All @@ -25,16 +27,18 @@ class ClosedIssue:
url: str
short_ref: str
champion: str
milestone: Optional[str]


@dataclass
class OpenedIssue:
name: str
url: str
short_ref: str
milestone: Optional[str]


def get_closed_issues() -> list[ClosedIssue]:
def get_closed_issues() -> dict[str, list[ClosedIssue]]:
today = datetime.utcnow().date()
yesterday = today - timedelta(days=1)
period_start = datetime(yesterday.year, yesterday.month, yesterday.day, hour=15, minute=00).isoformat()
Expand All @@ -48,16 +52,23 @@ def get_closed_issues() -> list[ClosedIssue]:
raise

issues = result.json()
parsed = [
ClosedIssue(
name=issue['title'],
url=issue['web_url'],
short_ref=issue['references']['short'],
champion=(issue.get('assignee', {}) or {}).get('name', 'Unknown'))
for issue in issues if issue
]
heroes: dict[str, list[ClosedIssue]] = defaultdict(list)
for issue in issues:
if not issue:
continue

return parsed
champion: str = (issue.get('assignee', {}) or {}).get('name', 'Unknown hero')

heroes[champion].append(
ClosedIssue(
name=issue['title'],
url=issue['web_url'],
short_ref=issue['references']['full'],
champion=champion,
milestone=(issue.get('milestone', {}) or {}).get('title')
))

return heroes



Expand All @@ -67,6 +78,7 @@ def get_opened_issues() -> list[OpenedIssue]:
yesterday = today - timedelta(days=1)
period_start = datetime(yesterday.year, yesterday.month, yesterday.day, hour=15, minute=00).isoformat()
period_end = datetime(today.year, today.month, today.day, hour=15, minute=00).isoformat()

try:
result = httpx.get(
f'{GITLAB_URL}/api/v4/issues?private_token={TOKEN}&scope=all&state=opened&created_after={period_start}&created_before={period_end}'
Expand All @@ -80,7 +92,8 @@ def get_opened_issues() -> list[OpenedIssue]:
OpenedIssue(
name=issue['title'],
url=issue['web_url'],
short_ref=issue['references']['short'])
short_ref=issue['references']['full'],
milestone=(issue.get('milestone', {}) or {}).get('title'))
for issue in issues if issue
]

Expand Down
1 change: 0 additions & 1 deletion src/issues/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def send_email(emails: list[str], body: str):
'''

html = f'<html><head>{head}</head><body>{markdown.markdown(body)}</body></html>'
print(html)
msg.attach(MIMEText(html, 'html'))

server = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
Expand Down
11 changes: 8 additions & 3 deletions src/issues/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
TOKEN = os.getenv('GITLAB_TOKEN')
GITLAB_URL = os.getenv('GITLAB_URL')

BLACKLIST = ['[email protected]']
BLACKLIST = os.getenv('BLACKLIST', '').split('|')


def get_user_emails() -> list[str]:
try:
Expand All @@ -21,10 +22,14 @@ def get_user_emails() -> list[str]:
raise

users = result.json()

logger.info(f"Got users: {users}")

return [
user['email'] for user in users if
user['state'] != 'blocked'
user.get('email')
and user['state'] != 'blocked'
and 'bot' not in user['username']
and user['email'] not in BLACKLIST
and user.get('email') not in BLACKLIST
and 'hero' not in (user.get('pronouns') or '')]

0 comments on commit 4aa959d

Please sign in to comment.