forked from svigerske/trac-to-github
-
Notifications
You must be signed in to change notification settings - Fork 5
/
migration_archive_writer.py
189 lines (171 loc) · 8.03 KB
/
migration_archive_writer.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
'''
Copyright © 2022 Matthias Koeppe
This software is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This sotfware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
'''
import json
import logging
import pathlib
from urllib.parse import urlparse, urlunparse, urljoin, quote
from collections import defaultdict
from copy import copy
from rich.pretty import pprint, pretty_repr
log = logging.getLogger("trac_to_gh")
# class MigrationArchiveWriter:
# # https://github.com/PyGithub/PyGithub/blob/master/github/Repository.py
# def create_issue(
# self,
# title,
# body=None,
# assignee=None,
# milestone=None,
# labels=None,
# assignees=None,
# ):
def pluralize(t):
if t.endswith('y'):
return t[:-1] + 'ies'
return t + 's'
class MigrationArchiveWritingRequester:
def __init__(self, migration_archive=None, wiki=None):
if migration_archive is None:
self._migration_archive = None
else:
self._migration_archive = pathlib.Path(migration_archive)
self._migration_archive.mkdir(parents=True, exist_ok=True)
if wiki is None:
self._wiki = None
else:
self._wiki = pathlib.Path(wiki)
self._wiki.mkdir(parents=True, exist_ok=True)
self._num_issues = 0
self._num_issue_comments = 0
self._num_issue_events = 0
self._num_json_by_type = defaultdict(lambda: 0)
self._batch_by_type = defaultdict(list)
def requestJsonAndCheck(self, verb, url, parameters=None, headers=None, input=None):
log.debug(f'# {verb} {url} {parameters=} {headers=} input={pretty_repr(input, max_string=60, max_width=200)}')
parse_result = urlparse(url)
endpoint = parse_result.path.split('/')[1:]
base_url = urlunparse([parse_result.scheme,
parse_result.netloc,
'/'.join(parse_result.path.split('/')[:3]) + '/',
None, None, None])
responseHeaders = None
output = copy(input)
match verb, endpoint:
case 'POST', [org, repo, 'labels']:
output['type'] = 'label'
output['repository'] = base_url[:-1] # strip final /
url = urljoin(base_url, 'labels/' + quote(input['name']))
case 'POST', [org, repo, 'milestones']:
output['type'] = 'milestone'
output['repository'] = base_url[:-1] # strip final /
url = urljoin(base_url, 'milestones/' + quote(input['title']))
case 'POST', [org, repo, 'issues']:
# Create a new issue
output['type'] = 'issue'
if 'number' in input:
self._num_issues = int(input['number'])
del output['number']
else:
self._num_issues += 1
issue = self._num_issues
output['repository'] = base_url[:-1] # strip final /
url = urljoin(base_url, f'issues/{issue}')
case 'POST', [org, repo, 'issues', issue, 'comments']:
# Create an issue comment
output['type'] = 'issue_comment'
output['issue'] = urljoin(base_url, f'issues/{issue}')
self._num_issue_comments += 1
id = self._num_issue_comments
url = urljoin(base_url, f'issues/{issue}#issuecomment-{id}')
case 'POST', [org, repo, 'issues', issue, 'events']:
# Create an issue event
output['type'] = 'issue_event'
output['issue'] = urljoin(base_url, f'issues/{issue}')
self._num_issue_events += 1
id = self._num_issue_events
url = urljoin(base_url, f'issues/{issue}#event-{id}')
case 'POST', [org, repo, 'issues', issue, 'attachments']:
# Create an attachment
output['type'] = 'attachment'
output['repository'] = base_url[:-1] # strip final /
output['issue'] = urljoin(base_url, f'issues/{issue}')
# https://github.github.com/enterprise-migrations/#/./2.1-export-archive-format?id=attachment
attachment_path = '/'.join(urlparse(input['asset_url']).path.split('/')[2:])
url = urljoin(base_url, f'attachments/{attachment_path}')
case 'POST', [org, repo, 'files']:
output['type'] = 'repository_file'
output['repository'] = base_url[:-1] # strip final /
file_path = '/'.join(urlparse(input['file_url']).path.split('/')[2:])
url = urljoin(base_url, f'files/{file_path}')
case 'GET', ['users', login]:
if output is None:
output = {}
output['type'] = 'user'
output['login'] = login
output['emails'] = []
url = f"https://github.com/{login}"
case 'PATCH', [org, repo]:
with open(self._migration_archive / f'repositories_000001.json.in', "r") as f:
repos = json.load(f)
repos[0].update(input)
output = repos[0]
if isinstance(output, dict):
output['url'] = url
dump = json.dumps(output, sort_keys=True, indent=4)
if self._migration_archive and 'type' in output:
t = output['type']
id = self._num_json_by_type[t] + 1
json_file = self._migration_archive / f'{pluralize(t)}_{id:06}.json'
self._batch_by_type[t].append(output)
if len(self._batch_by_type[t]) == 100:
self.flush_type(t)
else:
print(dump)
if self._wiki:
def issue_wiki_file():
thousands = int(issue) // 1000
dir = self._wiki / f'Issues-{thousands:02}xxx'
dir.mkdir(parents=True, exist_ok=True)
return dir / f'{issue}.md'
match verb, endpoint:
case 'POST', [org, repo, 'issues']:
with open(issue_wiki_file(), 'w') as f:
title = output['title']
f.write(f'# Issue {issue}: {title}\n\n')
f.write(f'{json_file}:\n')
f.write(f'```json\n{dump}\n```\n')
f.write(output['body'])
f.write('\n')
case 'POST', [org, repo, 'issues', issue, _]:
with open(issue_wiki_file(), 'a') as f:
f.write('\n\n\n---\n\n')
f.write(f'{json_file}:\n')
f.write(f'```json\n{dump}\n```\n')
if 'body' in output:
f.write('\n')
f.write(output['body'])
f.write('\n')
return responseHeaders, output
def flush_type(self, t):
self._num_json_by_type[t] += 1 # Starts at 1
id = self._num_json_by_type[t]
json_file = self._migration_archive / f'{pluralize(t)}_{id:06}.json'
dump = json.dumps(self._batch_by_type[t], sort_keys=True, indent=4)
with open(json_file, 'w') as f:
f.write(dump)
log.info(f'# Wrote {json_file}')
self._batch_by_type[t] = list()
def flush(self):
for t in self._batch_by_type:
self.flush_type(t)