-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
733 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
breathecode/assignments/management/commands/send_deletion_order_notifications.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import re | ||
from typing import Any, Optional | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from breathecode.assignments import tasks | ||
from breathecode.assignments.models import RepositoryDeletionOrder | ||
from breathecode.authenticate.models import AcademyAuthSettings | ||
from breathecode.services.github import Github | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Clean data from marketing module" | ||
github_url_pattern = re.compile(r"https?://github\.com/(?P<user>[^/\s]+)/(?P<repo>[^/\s]+)/?") | ||
|
||
def handle(self, *args, **options): | ||
self.github() | ||
|
||
def github(self): | ||
processed = set() | ||
for settings in AcademyAuthSettings.objects.filter( | ||
github_owner__isnull=False, github_owner__credentialsgithub__isnull=False | ||
).exclude(github_username=""): | ||
self.github_client = Github( | ||
org=settings.github_username, token=settings.github_owner.credentialsgithub.token | ||
) | ||
|
||
key = (settings.github_username, settings.github_owner.id) | ||
if key in processed: | ||
continue | ||
|
||
processed.add(key) | ||
allowed_users = ["breatheco-de", "4GeeksAcademy", "4geeksacademy"] | ||
|
||
items = RepositoryDeletionOrder.objects.filter(provider=RepositoryDeletionOrder.Provider.GITHUB) | ||
for deletion_order in items: | ||
if deletion_order.repository_user not in allowed_users: | ||
continue | ||
|
||
new_owner = self.get_username(deletion_order.repository_user, deletion_order.repository_name) | ||
if new_owner is None: | ||
continue | ||
|
||
tasks.send_repository_deletion_notification.delay(deletion_order.id, new_owner) | ||
|
||
def check_path(self, obj: dict, *indexes: str) -> bool: | ||
try: | ||
value = obj | ||
for index in indexes: | ||
value = value[index] | ||
return True | ||
except Exception: | ||
return False | ||
|
||
def how_many_added_members(self, events: list[dict[str, Any]]) -> int: | ||
return len( | ||
[ | ||
event | ||
for event in events | ||
if self.check_path(event, "type") | ||
and self.check_path(event, "payload", "action") | ||
and event["type"] == "MemberEvent" | ||
and event["payload"]["action"] == "added" | ||
] | ||
) | ||
|
||
def get_username(self, owner: str, repo: str) -> Optional[str]: | ||
r = repo | ||
repo = repo.lower() | ||
index = -1 | ||
for events in self.github_client.get_repo_events(owner, r): | ||
index += 1 | ||
for event in events: | ||
if self.check_path(event, "type") is False: | ||
continue | ||
|
||
if ( | ||
index == 0 | ||
and event["type"] == "MemberEvent" | ||
and len(events) < 30 | ||
and self.check_path(event, "payload", "action") | ||
and self.how_many_added_members(events) == 1 | ||
and self.check_path(event, "payload", "member", "login") | ||
and event["payload"]["action"] == "added" | ||
): | ||
return event["payload"]["member"]["login"] | ||
|
||
if ( | ||
event["type"] == "watchEvent" | ||
and self.check_path(event, "actor", "login") | ||
and event["actor"]["login"].replace("-", "").lower() in repo | ||
): | ||
return event["actor"]["login"] | ||
|
||
if ( | ||
event["type"] == "MemberEvent" | ||
and self.check_path(event, "payload", "member", "login") | ||
and event["payload"]["member"]["login"].replace("-", "").lower() in repo | ||
): | ||
return event["payload"]["member"]["login"] | ||
|
||
if ( | ||
event["type"] == "IssuesEvent" | ||
and self.check_path(event, "payload", "assignee", "login") | ||
and event["payload"]["assignee"]["login"].replace("-", "").lower() in repo | ||
): | ||
return event["payload"]["assignee"]["login"] | ||
|
||
if ( | ||
self.check_path(event, "actor", "login") | ||
and event["actor"]["login"].replace("-", "").lower() in repo | ||
): | ||
return event["actor"]["login"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.