-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgithub_util.py
More file actions
81 lines (61 loc) · 2.09 KB
/
github_util.py
File metadata and controls
81 lines (61 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import collections.abc
import datetime
import functools
import logging
import time
import github3
import github3.issues
import github3.repos
import github.retry
logger = logging.getLogger(__name__)
def is_remaining_quota_too_low(
gh_api: github3.GitHub,
relative_gh_quota_minimum: float = 0.2,
) -> bool:
rate_limit = gh_api.rate_limit().get('resources', dict()).get('core', dict()).get('limit', -1)
rate_limit_remaining = gh_api.ratelimit_remaining
logger.info(f'{rate_limit_remaining=} {rate_limit=}')
return rate_limit_remaining < relative_gh_quota_minimum * rate_limit
def wait_for_quota_if_required(
gh_api: github3.GitHub,
relative_gh_quota_minimum: float = 0.2,
):
if not is_remaining_quota_too_low(
gh_api=gh_api,
relative_gh_quota_minimum=relative_gh_quota_minimum,
):
return
reset_timestamp = gh_api.rate_limit().get('resources', dict()).get('core', dict()).get('reset')
if not reset_timestamp:
return
reset_datetime = datetime.datetime.fromtimestamp(
timestamp=reset_timestamp,
tz=datetime.timezone.utc,
)
time_until_reset = reset_datetime - datetime.datetime.now(tz=datetime.timezone.utc)
logger.warning(f'github quota too low, will sleep {time_until_reset} until {reset_datetime}')
time.sleep(time_until_reset.total_seconds())
@functools.cache
@github.retry.retry_and_throttle
def all_issues(
repository: github3.repos.Repository,
state: str = 'all',
number: int = -1, # -1 means all issues
):
return set(
repository.issues(
state=state,
number=number,
),
)
def filter_issues_for_labels(
issues: collections.abc.Iterable[github3.issues.ShortIssue],
labels: collections.abc.Iterable[str],
) -> tuple[github3.issues.ShortIssue, ...]:
labels = set(labels)
def filter_issue(
issue: github3.issues.ShortIssue,
) -> bool:
issue_labels = {label.name for label in issue.original_labels}
return labels.issubset(issue_labels)
return tuple(issue for issue in issues if filter_issue(issue))