This commit is contained in:
Dima Gerasimov 2019-12-06 23:28:06 +00:00
parent 9fc2c29779
commit e9152d055c

View file

@ -1,4 +1,4 @@
from typing import Dict, List, Union, Any, NamedTuple, Tuple, Optional, Iterator, TypeVar from typing import Dict, List, Union, Any, NamedTuple, Tuple, Optional, Iterator, TypeVar, Set
from datetime import datetime from datetime import datetime
import json import json
from pathlib import Path from pathlib import Path
@ -28,17 +28,17 @@ T = TypeVar('T')
Res = Union[T, Exception] Res = Union[T, Exception]
# TODO split further, title too # TODO split further, title too
def _get_summary(e) -> Tuple[str, Optional[str]]: def _get_summary(e) -> Tuple[str, Optional[str], Optional[str]]:
tp = e['type'] tp = e['type']
pl = e['payload'] pl = e['payload']
rname = e['repo']['name'] rname = e['repo']['name']
if tp == 'ForkEvent': if tp == 'ForkEvent':
url = e['payload']['forkee']['html_url'] url = e['payload']['forkee']['html_url']
return f"forked {rname}", url return f"forked {rname}", url, None
elif tp == 'PushEvent': elif tp == 'PushEvent':
return f"pushed to {rname}", None return f"pushed to {rname}", None, None
elif tp == 'WatchEvent': elif tp == 'WatchEvent':
return f"watching {rname}", None return f"watching {rname}", None, None
elif tp == 'CreateEvent': elif tp == 'CreateEvent':
# TODO eh, only weird API link? # TODO eh, only weird API link?
return f"created {rname}", None, f'created_{rname}' return f"created {rname}", None, f'created_{rname}'
@ -53,7 +53,7 @@ def _get_summary(e) -> Tuple[str, Optional[str]]:
iss = pl['issue'] iss = pl['issue']
link = iss['html_url'] link = iss['html_url']
title = iss['title'] title = iss['title']
return f"{action} issue {title}", link return f"{action} issue {title}", link, None
elif tp == "IssueCommentEvent": elif tp == "IssueCommentEvent":
com = pl['comment'] com = pl['comment']
link = com['html_url'] link = com['html_url']
@ -65,14 +65,14 @@ def _get_summary(e) -> Tuple[str, Optional[str]]:
rel = pl['release'] rel = pl['release']
tag = rel['tag_name'] tag = rel['tag_name']
link = rel['html_url'] link = rel['html_url']
return f"{action} {rname} [{tag}]", link return f"{action} {rname} [{tag}]", link, None
elif tp in ( elif tp in (
"DeleteEvent", "DeleteEvent",
"PublicEvent", "PublicEvent",
): ):
return tp, None # TODO ??? return tp, None, None # TODO ???
else: else:
return tp, None return tp, None, None
def get_model(): def get_model():
@ -102,7 +102,7 @@ def _parse_repository(d: Dict) -> Event:
pref = 'https://github.com/' pref = 'https://github.com/'
url = d['url'] url = d['url']
assert url.startswith(pref); name = url[len(pref):] assert url.startswith(pref); name = url[len(pref):]
return Event( return Event( # type: ignore[misc]
**_parse_common(d), **_parse_common(d),
summary='created ' + name, summary='created ' + name,
eid='created_' + name, # TODO ?? eid='created_' + name, # TODO ??
@ -110,7 +110,7 @@ def _parse_repository(d: Dict) -> Event:
def _parse_issue_comment(d: Dict) -> Event: def _parse_issue_comment(d: Dict) -> Event:
url = d['url'] url = d['url']
return Event( return Event( # type: ignore[misc]
**_parse_common(d), **_parse_common(d),
summary=f'commented on issue {url}', summary=f'commented on issue {url}',
eid='issue_comment_' + url, eid='issue_comment_' + url,
@ -120,7 +120,7 @@ def _parse_issue_comment(d: Dict) -> Event:
def _parse_issue(d: Dict) -> Event: def _parse_issue(d: Dict) -> Event:
url = d['url'] url = d['url']
title = d['title'] title = d['title']
return Event( return Event( # type: ignore[misc]
**_parse_common(d), **_parse_common(d),
summary=f'opened issue {title}', summary=f'opened issue {title}',
eid='issue_comment_' + url, eid='issue_comment_' + url,
@ -130,7 +130,7 @@ def _parse_issue(d: Dict) -> Event:
def _parse_pull_request(d: Dict) -> Event: def _parse_pull_request(d: Dict) -> Event:
url = d['url'] url = d['url']
title = d['title'] title = d['title']
return Event( return Event( # type: ignore[misc]
**_parse_common(d), **_parse_common(d),
# TODO distinguish incoming/outgoing? # TODO distinguish incoming/outgoing?
# TODO action? opened/closed?? # TODO action? opened/closed??
@ -141,7 +141,7 @@ def _parse_pull_request(d: Dict) -> Event:
def _parse_release(d: Dict) -> Event: def _parse_release(d: Dict) -> Event:
tag = d['tag_name'] tag = d['tag_name']
return Event( return Event( # type: ignore[misc]
**_parse_common(d), **_parse_common(d),
summary=f'released {tag}', summary=f'released {tag}',
eid='release_' + tag, eid='release_' + tag,
@ -150,7 +150,7 @@ def _parse_release(d: Dict) -> Event:
def _parse_commit_comment(d: Dict) -> Event: def _parse_commit_comment(d: Dict) -> Event:
url = d['url'] url = d['url']
return Event( return Event( # type: ignore[misc]
**_parse_common(d), **_parse_common(d),
summary=f'commented on {url}', summary=f'commented on {url}',
eid='commoit_comment_' + url, eid='commoit_comment_' + url,
@ -158,10 +158,9 @@ def _parse_commit_comment(d: Dict) -> Event:
def _parse_event(d: Dict) -> Event: def _parse_event(d: Dict) -> Event:
xx = _get_summary(d) summary, link, eid = _get_summary(d)
if len(xx) != 3: if eid is None:
xx += (d['id'], ) eid = d['id']
summary, link, eid = xx
body = d.get('payload', {}).get('comment', {}).get('body') body = d.get('payload', {}).get('comment', {}).get('body')
return Event( return Event(
dt=_parse_dt(d['created_at']), dt=_parse_dt(d['created_at']),
@ -221,7 +220,7 @@ def iter_backup_events():
def iter_events() -> Iterator[Res[Event]]: def iter_events() -> Iterator[Res[Event]]:
logger = get_logger() logger = get_logger()
from itertools import chain from itertools import chain
emitted = set() emitted: Set[Tuple[datetime, str]] = set()
for e in chain(iter_gdpr_events(), iter_backup_events()): for e in chain(iter_gdpr_events(), iter_backup_events()):
if isinstance(e, Exception): if isinstance(e, Exception):
yield e yield e