-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrelease_utils.py
278 lines (228 loc) · 7.21 KB
/
release_utils.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import annotations
import contextlib
import os
import re
import sys
from contextlib import contextmanager
from datetime import datetime
from pathlib import Path
from git import Repo
from github import Github, Milestone
from tqdm import tqdm
from unidecode import unidecode
from yaml import safe_load
PR_NUM_PATTERN = re.compile(r'\(#(\d+)\)(?:$|\n)')
@contextmanager
def short_cache(new_time):
"""
context manager for change cache time in requests_cache
"""
try:
import requests_cache
except ImportError:
yield
return
import requests
if requests_cache.get_cache() is None:
yield
return
session = requests.Session()
old_time = session.expire_after
session.expire_after = new_time
try:
yield
finally:
session.expire_after = old_time
def setup_cache(timeout=3600):
"""
setup cache for speedup execution and reduce number of requests to GitHub API
by default cache will expire after 1h (3600s)
"""
try:
import requests_cache
except ImportError:
print('requests_cache not installed', file=sys.stderr)
return
"""setup cache for requests"""
requests_cache.install_cache(
'github_cache', backend='sqlite', expire_after=timeout
)
LOCAL_DIR = Path(__file__).parent
REPO_DIR_NAME = 'project_repo'
GH = os.environ.get('GH', 'github.com')
GH_USER = os.environ.get('GH_USER', 'napari')
GH_REPO = os.environ.get('GH_REPO', 'napari')
GH_DOCS_REPO = os.environ.get('GH_REPO', 'docs')
GH_TOKEN = os.environ.get('GH_TOKEN')
_G = None
def get_github():
global _G
if _G is None:
if GH_TOKEN is None:
raise RuntimeError(
'It is necessary that the environment variable `GH_TOKEN` '
'be set to avoid running into problems with rate limiting. '
'One can be acquired at https://github.com/settings/tokens.\n\n'
'You do not need to select any permission boxes while generating '
'the token.'
)
_G = Github(GH_TOKEN)
return _G
def get_repo(user=GH_USER, repo=GH_REPO):
g = get_github()
return g.get_repo(f'{user}/{repo}')
def get_local_repo(path=None):
"""
get the local repository
"""
from git import Repo
if path is not None:
return Repo(path)
return Repo(REPO_DIR_NAME)
def get_common_ancestor(commit1, commit2):
"""
find the common ancestor for two commits
"""
local_repo = get_local_repo()
return local_repo.merge_base(commit1, commit2)[0]
def get_commits_to_ancestor(ancestor, rev='main'):
local_repo = get_local_repo()
yield from local_repo.iter_commits(f'{ancestor.hexsha}..{rev}')
def get_commit_counts_from_ancestor(release, rev='main'):
"""
get number of commits from ancestor to release
"""
ancestor = get_common_ancestor(release, rev)
return sum(
PR_NUM_PATTERN.search(c.message) is not None
for c in get_commits_to_ancestor(ancestor, rev)
)
def get_milestone(
milestone_name: str | None,
repo: str = f'{GH_USER}/{GH_REPO}',
) -> Milestone.Milestone | None:
if milestone_name is None:
return None
repository = get_repo(*repo.split('/'))
with contextlib.suppress(ValueError):
return repository.get_milestone(int(milestone_name))
for milestone in repository.get_milestones():
if milestone.title == milestone_name:
return milestone
raise RuntimeError(f'Milestone {milestone_name} not found')
def get_split_date(previous_release, rev='main'):
common_ancestor = get_common_ancestor(previous_release, rev)
remote_commit = get_repo().get_commit(common_ancestor.hexsha)
return datetime.strptime(
remote_commit.last_modified, '%a, %d %b %Y %H:%M:%S %Z'
)
def iter_pull_request(additional_query, user=GH_USER, repo=GH_REPO):
iterable = get_github().search_issues(
f'repo:{user}/{repo} is:pr sort:created-asc {additional_query}'
)
print(
f'Found {iterable.totalCount} pull requests on query: {additional_query}'
f' for repo {user}/{repo}',
file=sys.stderr,
)
for pull_issue in tqdm(
iterable,
desc=f'Pull Requests ({user}/{repo})...',
total=iterable.totalCount,
):
yield pull_issue.as_pull_request()
def get_pr_commits_dict(repo: Repo, branch: str = 'main') -> dict[int, str]:
"""
Calculate mapping from PR number in commit hash from a provided branch
Parameters
----------
repo: Repo
Object representing local repository
branch: str
branch name
Returns
-------
dict from PR number to commit hash
"""
res = {}
for commit in repo.iter_commits(branch):
if (match := PR_NUM_PATTERN.search(commit.message)) is not None:
pr_num = int(match[1])
res[pr_num] = commit.hexsha
return res
def get_consumed_pr(repo: Repo, target_branch: str) -> set[int]:
"""
Get a set of commits that are already cherry-picked
Parameters
----------
repo: Repo
object representing local repository
target_branch: str
branch to check for merged PR
Returns
-------
"""
res = set()
for commit in repo.iter_commits(target_branch):
if (match := PR_NUM_PATTERN.search(commit.message)) is not None:
pr_num = int(match[1])
res.add(pr_num)
return res
def existing_file(path: str) -> Path:
path = Path(path)
if not path.exists():
raise FileNotFoundError(f'{path} not found')
return path
BOT_LIST = {
'github-actions[bot]',
'pre-commit-ci[bot]',
'dependabot[bot]',
'napari-bot',
None,
}
def get_correction_dict(file_path: Path | None) -> dict[str, str]:
"""
Read file with correction of name between
"""
if not file_path or not file_path.exists():
return {}
correction_dict = {}
with open(file_path) as f:
corrections = safe_load(f)
for correction in corrections['login_to_name']:
correction_dict[correction['login']] = unidecode(
correction['corrected_name'].lower()
)
return correction_dict
def get_corrections_from_citation_cff(
cff_data: str | Path | dict,
) -> dict[str, str]:
if isinstance(cff_data, (str, Path)):
cff_data = Path(cff_data)
if not cff_data.exists():
return {}
with cff_data.open(encoding='utf8') as f:
cff_data = safe_load(f)
res = {}
for author in cff_data['authors']:
if 'alias' in author:
res[author['alias']] = (
f'{author["given-names"]} {author["family-names"]}'
)
return res
def get_corrections_from_citation_cff2(
cff_data: str | Path | dict,
) -> dict[str, str]:
if isinstance(cff_data, (str, Path)):
cff_data = Path(cff_data)
if not cff_data.exists():
return {}
with cff_data.open(encoding='utf8') as f:
cff_data = safe_load(f)
res = {}
for author in cff_data['authors']:
if 'alias' in author:
res[author['alias']] = unidecode(
f'{author["given-names"]} {author["family-names"]}'.lower()
)
return res