-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit-mirror
executable file
·476 lines (384 loc) · 11.6 KB
/
git-mirror
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env zx
// vim: set filetype=javascript :
import { URL } from "url";
import { GitHub } from "./zx/github.mjs";
import { GitLab } from "./zx/gitlab.mjs";
import { JetBrainsSpace } from "./zx/jetbrains-space.mjs";
$.verbose = false;
const ask_bool = async (prompt) => {
return await question(`${prompt} [y/N] `).then((answer) => {
return answer.toLowerCase() == "y";
});
};
const read_file = async (filename) => {
return await fs.readFile(filename, { encoding: "utf8" });
};
const generate_ssh_keypair = async (comment = "git-mirror") => {
const filename = "/tmp/git-mirror-key";
$`rm -f ${filename} ${filename}.pub`;
await $`ssh-keygen -t ed25519 -C ${comment} -N '' -f ${filename}`;
const [privateKey, publicKey] = await Promise.all([
read_file(filename),
read_file(`${filename}.pub`),
]);
$`rm -f ${filename} ${filename}.pub`;
return { privateKey, publicKey };
};
const allowed_default_branches = ["main", "master"];
async function get_repo_info() {
let ret;
ret = await $`git branch --show-current`;
const current_branch = ret.stdout.trim();
if (!allowed_default_branches.includes(current_branch)) {
throw new Error(
`Current branch: ${current_branch}, expected: ${allowed_default_branches.join(
"/"
)}`
);
}
ret = await $`git remote`;
const remote_names = ret.stdout.trim().split("\n");
const remote = {};
for (const remote_name of remote_names) {
remote[remote_name] = {};
ret = await $`git remote get-url --all ${remote_name}`;
remote[remote_name]["fetch"] = ret.stdout
.trim()
.split("\n")
.map((url) => new URL(url));
ret = await $`git remote get-url --all --push ${remote_name}`;
remote[remote_name]["push"] = ret.stdout
.trim()
.split("\n")
.map((url) => new URL(url));
remote[remote_name].url = remote[remote_name]["fetch"][0];
}
const project = remote.origin.fetch[0].pathname
.replace(/^\//, "")
.replace(/\.git$/, "");
const [owner, name] = project.split("/");
return {
project,
owner,
name,
remote,
current_branch,
};
}
async function refresh_repo_info(repo) {
Object.assign(repo, await get_repo_info());
}
async function ensure_git_mirror_remote(repo, name, remote_url) {
const url = new URL(remote_url);
if (
repo.remote.origin.url.host === url.host ||
repo.remote[`origin-${name}`]
) {
return;
}
await $`git remote add origin-${name} ${url.href}`;
await refresh_repo_info(repo);
}
async function init_gitlab(repo) {
const gitlab = await new GitLab({ id: "git-mirror" }).init();
const get_mirror_project_settings = () => ({
description: `Mirroring: ${repo.remote.origin.url.href}`,
analytics_access_level: "disabled",
builds_access_level: "disabled",
container_registry_access_level: "disabled",
feature_flags_access_level: "disabled",
forking_access_level: "disabled",
infrastructure_access_level: "disabled",
issues_access_level: "disabled",
merge_requests_access_level: "disabled",
monitor_access_level: "disabled",
operations_access_level: "disabled",
packages_enabled: false,
pages_access_level: "disabled",
requirements_access_level: "disabled",
security_and_compliance_access_level: "disabled",
snippets_access_level: "disabled",
wiki_access_level: "disabled",
});
async function create_mirror({ try_import = true, is_public = null } = {}) {
gitlab.log.info(`Creating project: ${repo.project}...`);
if (typeof is_public !== "boolean") {
is_public = await ask_bool("Make project public?");
}
const import_url = try_import ? repo.remote.origin.url.href : null;
const res = await gitlab.api("POST /projects", {
body: {
name: repo.name,
import_url,
visibility: is_public ? "public" : "private",
...get_mirror_project_settings(repo),
},
});
if (res.ok) {
gitlab.log.info(`Created project: ${repo.project}...`);
} else if (try_import && res.status === 422) {
gitlab.log.error(`Failed to create project: ${repo.project}...`);
gitlab.log.error(` Reason: ${res.data.message}`);
gitlab.log.info(`Trying again w/o initial import...`);
return create_mirror({ try_import: false, is_public });
} else {
gitlab.log.error(`Failed to create project: ${repo.project}...`);
throw res;
}
}
async function update_mirror(project) {
const res = await gitlab.api(`PUT /projects/${project.id}`, {
body: get_mirror_project_settings(),
});
if (!res.ok) {
throw res;
}
}
async function ensure_mirror() {
const res = await gitlab.api(
`/projects/${encodeURIComponent(repo.project)}`
);
if (res.ok) {
await update_mirror(res.data);
gitlab.log.info(`Project Found: ${repo.project}`);
return;
}
await create_mirror();
}
async function remove_mirror_branch_protection() {
const res = await gitlab.api(
`/projects/${encodeURIComponent(repo.project)}/protected_branches`
);
for (const item of res.data) {
await gitlab.api(
`DELETE /projects/${encodeURIComponent(
repo.project
)}/protected_branches/${item.name}`
);
gitlab.log.info(`Removed branch protection: ${item.name}`);
}
}
await ensure_mirror();
await remove_mirror_branch_protection();
const remote_url = `https://gitlab.com/${repo.project}.git`;
await ensure_git_mirror_remote(repo, "gitlab", remote_url);
}
async function init_jetbrains_space(repo) {
const space = await new JetBrainsSpace({
id: "git-mirror",
site: "muniftanjim",
}).init();
const project_key = "key:PERSONAL";
const base_url = `/projects/${project_key}/repositories/${repo.name}`;
async function get_mirror_repository_settings_mirror_key(mirror) {
const body = {
remote: {
url: mirror.url,
},
};
let res = await space.api(
`POST /projects/${project_key}/repositories/test-connection`,
{ body }
);
if (!res.ok) {
throw res;
}
if (res.data.success) {
space.log.info(`Successfully connected to remote ${body.remote.url}`);
return null;
}
space.log.warn(res.data.reason.split("\n")[0]);
const github_deploy_key_title = "muniftanjim.jetbrains.space:mirror";
const github = new GitHub({ id: "git-mirror" });
res = await github.api("/repos/{owner}/{repo}/keys", {
jq: `.[] | select(.title=="${github_deploy_key_title}")`,
});
if (!res.ok) {
throw res;
}
if (res.data) {
res = await github.api(
`DELETE /repos/{owner}/{repo}/keys/${res.data.id}`
);
if (!res.ok) {
github.log.info(
`Failed to remove old deploy key (${github_deploy_key_title})`
);
throw res;
}
github.log.info(`Removed old deploy key (${github_deploy_key_title})`);
}
const keypair = await generate_ssh_keypair(github_deploy_key_title);
res = await github.api("POST /repos/{owner}/{repo}/keys", {
fields: {
title: github_deploy_key_title,
key: keypair.publicKey,
read_only: true,
},
});
if (!res.ok) {
github.log.error(`Failed to add deploy key (${github_deploy_key_title})`);
throw res;
}
github.log.info(`Added deploy key (${github_deploy_key_title})`);
body.remote.auth = {
className: "RemoteRepositoryAuth.SSH",
privateKey: keypair.privateKey,
passphrase: "",
};
res = await space.api(
`POST /projects/${project_key}/repositories/test-connection`,
{ body }
);
if (res.ok) {
space.log.info(
`Successfully connected to remote ${body.remote.url} using deploy key`
);
return keypair.privateKey;
}
space.log.error(`Failed to connect to remote: ${body.remote.url}`);
throw res;
}
const repo_description = `Mirroring: ${repo.remote.origin.url.href}`;
const get_mirror_repository_settings = async () => {
const settings = {
mirror: {
version: "",
url: `https://github.com/${repo.project}.git`,
detachHEAD: false,
fetchPeriodically: true,
fetchBeforeGitCall: true,
key: null,
mirrorPullRequest: false,
},
};
settings.mirror.key = await get_mirror_repository_settings_mirror_key(
settings.mirror
);
if (settings.mirror.key) {
settings.mirror.url = `[email protected]:${repo.project}.git`;
settings.mirror.useAuthKey = true;
settings.mirror.keyPassphrase = "";
} else {
settings.mirror.url = `https://github.com/${repo.project}.git`;
settings.mirror.useAuthKey = null;
settings.mirror.keyPassphrase = null;
}
return { settings };
};
async function create_mirror() {
space.log.info(`Creating project: ${repo.name}...`);
const { settings } = await get_mirror_repository_settings(repo);
const info = {
description: repo_description,
remote: {
url: settings.mirror.url,
auth: settings.mirror.useAuthKey
? {
className: "RemoteRepositoryAuth.SSH",
privateKey: settings.mirror.key,
passphrase: settings.mirror.keyPassphrase,
}
: null,
},
mirror: {
syncPeriodically: settings.mirror.fetchPeriodically,
syncOnFetch: settings.mirror.fetchBeforeGitCall,
mirrorPullRequest: settings.mirror.mirrorPullRequest,
},
};
const res = await space.api(`POST ${base_url}/migrate`, {
body: info,
});
if (res.ok) {
space.log.info(`Created project: ${repo.name}...`);
} else {
space.log.info(`Failed to create project: ${repo.name}...`);
throw res;
}
}
async function update_mirror() {
let res = await space.api(`POST ${base_url}/settings`, {
body: await get_mirror_repository_settings(),
});
if (!res.ok) {
throw res;
}
res = await space.api(`POST ${base_url}/description`, {
body: { description: repo_description },
});
if (!res.ok) {
throw res;
}
}
async function ensure_mirror() {
const res = await space.api(base_url);
if (res.ok) {
await update_mirror();
space.log.info(`Repository Found: ${res.data.name}`);
return;
}
await create_mirror();
}
async function get_remote_url() {
const res = await space.api(`${base_url}/url`);
if (!res.ok) {
throw res;
}
return res.data.httpUrl;
}
await ensure_mirror();
const remote_url = await get_remote_url();
await ensure_git_mirror_remote(repo, "jetbrains-space", remote_url);
}
const REMOTE_SERVICE = {
gitlab: {
init: init_gitlab,
has_auto_sync: false,
},
["jetbrains-space"]: {
init: init_jetbrains_space,
has_auto_sync: true,
},
};
async function init(repo) {
for (const remote of Object.values(REMOTE_SERVICE)) {
await remote.init(repo);
}
}
async function sync(repo) {
$.verbose = true;
for (const remote of Object.keys(REMOTE_SERVICE)) {
if (
!repo.remote[`origin-${remote}`] ||
REMOTE_SERVICE[remote].has_auto_sync
) {
continue;
}
await $`git push origin-${remote} --force --all --follow-tags`;
// push non-annotated tags
await $`git push origin-${remote} --force --tags`;
}
$.verbose = false;
}
const repo = await get_repo_info();
switch (argv._[0]) {
case "init": {
await init(repo);
break;
}
case "sync": {
await sync(repo);
break;
}
default: {
if (
Object.keys(REMOTE_SERVICE).some(
(remote) => !repo.remote[`origin-${remote}`]
)
) {
await init(repo);
}
await sync(repo);
}
}