-
Notifications
You must be signed in to change notification settings - Fork 6
/
importBoardsTxt.ts
executable file
·330 lines (283 loc) · 8.9 KB
/
importBoardsTxt.ts
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env -S deno run --allow-env --allow-net --allow-run
/* eslint-disable no-console */
import { cryptoRandomString } from 'https://deno.land/x/crypto_random_string@1.0.0/mod.ts'
import { exec, OutputMode } from 'https://deno.land/x/exec@0.0.5/mod.ts'
import { parse } from 'https://deno.land/std@0.119.0/flags/mod.ts'
import * as path from 'https://deno.land/std@0.167.0/path/mod.ts'
import ProgressBar from 'https://deno.land/x/progress@v1.3.4/mod.ts'
import shuffle from "https://deno.land/x/shuffle@v1.0.1/mod.ts"
const flags = parse(Deno.args, {
string: ['giteaUrl', 'adminToken', 'githubToken'],
boolean: ['help', 'tokenOnly', 'shuffle'],
default: { numberOfRepos: 150, giteaUrl: 'http://localhost:3333', shuffle: true },
})
if (flags.help || !(flags.githubToken || flags.tokenOnly)) {
console.log(`Usage: importBoardsTxt [options]
options:
--giteaUrl: Gitea URL (default: http://localhost:3333)
--adminToken: Gitea admin API token (default: generated automatically)
--githubToken: GitHub API token (classic) (Embedded into the script in staging servers.)
--numberOfRepos: Number of repositories to import (default: 1000)
--tokenOnly: Only generate the admin token and exit.
--shuffle: Shuffle the boards.txt file before importing (default: true)
--help: Show this help
`)
Deno.exit(0)
}
if (!flags.adminToken) {
flags.adminToken = await generateGiteaAdminToken()
if (flags.tokenOnly) {
console.log(flags.adminToken)
Deno.exit(0)
}
}
const KITSPACE_GITEA_URL = flags.giteaUrl
const headers = {
'Content-Type': 'application/json',
Authorization: `token ${flags.adminToken}`,
}
async function main() {
let boards = (await getBoardsTxt())
if (flags.shuffle) {
boards = shuffle(boards)
.slice(0, flags.numberOfRepos)
}
const GithubReposDescriptions = await getAllGithubReposDescriptions(boards)
const progress = new ProgressBar({
title: 'Importing repos',
total: boards.length,
})
for (const [index, repoUrl] of boards.entries()) {
await importRepo(repoUrl, GithubReposDescriptions)
progress.render(index + 1)
}
}
await main()
/***************************************************************/
async function getBoardsTxt() {
const url =
'https://raw.githubusercontent.com/kitspace/kitspace/master/boards.txt'
const response = await fetch(url)
const text = await response.text()
return text.split('\n').filter(Boolean)
}
async function importRepo(
url: string,
GithubReposDescriptions: GithubReposDescriptionsMap,
) {
const userInfo = getUserInfoFromUrl(url)
const user = await createGiteaUser(userInfo)
const repoName = urlToName(url)
await mirrorRepo(url, user, repoName, GithubReposDescriptions)
// Avoid importing so many repos at the same time. This slows down the import process. for all repos.
await waitForRepoToBeReady(user.username, repoName)
}
function getUserInfoFromUrl(url: string): UserInfo {
const username = url.split('/', 5).slice(-2)[0]
return {
username,
email: `${username}@example.com`,
password: cryptoRandomString({ length: 32, type: 'base64' }),
}
}
async function createGiteaUser(userInfo: UserInfo): Promise<GiteaUser> {
const giteaUser = await getGiteaUser(userInfo.username)
if (giteaUser) {
return giteaUser
}
const url = `${KITSPACE_GITEA_URL}/api/v1/admin/users`
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(userInfo),
})
return response.json()
}
async function mirrorRepo(
remoteRepo: string,
user: GiteaUser,
repoName: string,
GithubReposDescriptions: GithubReposDescriptionsMap,
) {
if (await repoExists(user.username, repoName)) {
// Skipped mirroring; repo already exists!
return
}
const service = getGitServiceFromUrl(remoteRepo)
const endpoint = `${KITSPACE_GITEA_URL}/api/v1/repos/migrate`
if (!user.id) {
throw new Error('User id is missing')
}
const giteaOptions = {
clone_addr: remoteRepo,
uid: user.id,
repo_name: repoName,
mirror: true,
wiki: false,
private: false,
pull_requests: false,
releases: true,
issues: false,
// This is the workaround for avoiding the Github rate limit.
service: service === 'github' ? null : service,
description:
service === 'github'
? GithubReposDescriptions.get(`${user.username}/${repoName}`)
: null,
}
const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify(giteaOptions),
})
if (!response.ok) {
throw new Error(
`Failed to mirror ${remoteRepo} to ${user.username}/${repoName}`,
)
}
}
/**
* Use the github graphql API to get the description of all the repos.
* We can't use the REST API because it doesn't allow us to get the description of multiple repos in one request.
* And if we requested the description of each repo separately, we would hit the rate limit.
* @param reposUrls
*/
async function getAllGithubReposDescriptions(
reposUrls: string[],
): Promise<Map<string, string>> {
const githubRepos = reposUrls.filter(
url => getGitServiceFromUrl(url) === 'github',
)
const reposOwnerAndName = githubRepos.map(
repoName => repoName.split('/', 5).slice(-2) as [string, string],
)
const query = `
query {
${reposOwnerAndName.map(
([owner, name], index) => `
r${index}: repository(owner: "${owner}", name: "${name}") {
fullName: nameWithOwner
description
}`,
)}
}
`
const { data: reposInfo }: GitHubRepoInfoGQLResponse = await fetch(
'https://api.github.com/graphql',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `bearer ${flags.githubToken}`,
},
body: JSON.stringify({ query }),
},
).then(res => res.json())
const repoDescriptionsMap = new Map<string, string>()
for (const entry of Object.values(reposInfo)) {
repoDescriptionsMap.set(entry.fullName, entry.description)
}
return repoDescriptionsMap
}
async function waitForRepoToBeReady(
username: string,
repoName: string,
): Promise<void> {
const url = `${KITSPACE_GITEA_URL}/api/v1/repos/${username}/${repoName}`
const response = await fetch(url, { headers })
const repo = await response.json()
if (repo.empty) {
await waitMs(1000)
return waitForRepoToBeReady(username, repoName)
}
}
function urlToName(url: string | URL): string {
try {
url = new URL(url)
} catch {
return ''
}
return path.basename(url.pathname, path.extname(url.pathname))
}
function getGitServiceFromUrl(url: string): string {
let hostName
try {
hostName = new URL(url).hostname
} catch (e) {
console.log({ url })
console.log(e.stack)
throw e
}
switch (hostName) {
case 'github.com':
return 'github'
case 'gitlab.com':
return 'gitlab'
case 'try.gitea.io':
case 'gitea.com':
return 'gitea'
default:
return 'git'
}
}
async function repoExists(username: string, repoName: string) {
const url = `${KITSPACE_GITEA_URL}/api/v1/repos/${username}/${repoName}`
const response = await fetch(url, { headers })
return response.ok
}
async function getGiteaUser(username: string) {
const url = `${KITSPACE_GITEA_URL}/api/v1/users/${username}`
const response = await fetch(url)
if (!response.ok) {
return null
}
return response.json()
}
function waitMs(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function generateGiteaAdminToken() {
const adminUsername =
'importer' + cryptoRandomString({ length: 8, type: 'numeric' })
const adminPassword: string = cryptoRandomString({ length: 32, type: 'base64' })
const getGiteaContainerCommand = await exec(
`bash -c "docker ps | grep gitea | awk '{print $1}'"`,
{ output: OutputMode.Capture },
)
const giteaAdminCommand = await exec(
`bash -c "docker exec --user git ${getGiteaContainerCommand.output} /bin/sh -c 'gitea admin user create --username ${adminUsername} --password ${adminPassword} --email ${adminUsername}@example.com --admin --must-change-password=false'"`,
{ output: OutputMode.Capture },
)
if (!giteaAdminCommand.status.success) {
throw new Error('Failed to create gitea admin user')
}
const giteaAdminTokenCommand = await exec(
`bash -c "docker exec --user git ${getGiteaContainerCommand.output} /bin/sh -c 'gitea admin user generate-access-token --username ${adminUsername} --raw'"`,
{ output: OutputMode.Capture },
)
if (!giteaAdminTokenCommand.status.success) {
throw new Error('Failed to create gitea admin token')
}
return giteaAdminTokenCommand.output
}
type GithubReposDescriptionsMap = Map<string, string>
interface GitHubRepoInfoGQLResponse {
data: {
fullName: string
description: string
}[]
}
interface GiteaUser {
id: number
username: string
email: string
}
interface UserInfo {
username: string
email: string
password: string
}
interface RegistryBoard {
repo: string
hash: string
}