Use remote update instead of re-mirroring

This commit is contained in:
Morten Olsen
2021-07-23 11:58:57 +02:00
parent 5c5160437c
commit f3e3eff019
2 changed files with 19 additions and 11 deletions

View File

@@ -4,6 +4,7 @@
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"lint": "docker run --rm -i hadolint/hadolint < Dockerfile",
"start": "ts-node src/index.ts", "start": "ts-node src/index.ts",
"bundle": "ncc build src/index.ts -o dist" "bundle": "ncc build src/index.ts -o dist"
}, },

View File

@@ -15,22 +15,24 @@ const token = process.env.GITHUB_TOKEN;
const backupLocation = process.env.GITHUB_BACKUP_LOCATION || '/backup'; const backupLocation = process.env.GITHUB_BACKUP_LOCATION || '/backup';
const mirror = async (target: string, repo: string) => { const mirror = async (target: string, repo: string) => {
const git = simpleGit();
const authRemote = `https://foo:${token}@github.com/${repo}` const authRemote = `https://foo:${token}@github.com/${repo}`
if (fs.existsSync(target)) { if (fs.existsSync(target)) {
const backup = path.join(os.tmpdir(), nanoid()); const git = simpleGit(target);
await fs.move(target, backup); const remotes = await git.getRemotes();
try { const origin = remotes.find(r => r.name === 'origin');
await git.mirror(authRemote, target); if (origin) {
await rimraf(backup); await git.remote(['set-url', 'origin', authRemote]);
} catch(err) { } else {
await rimraf(target); await git.addRemote('origin', authRemote);
await fs.move(backup, target);
throw err;
} }
await git.remote(['update']);
await git.remote(['set-url', 'origin', `https://github.com/${repo}`]);
} else { } else {
await fs.mkdirp(target);
const git = simpleGit(target);
await git.mirror(authRemote, target); await git.mirror(authRemote, target);
await git.remote(['set-url', 'origin', `https://github.com/${repo}`]);
} }
}; };
@@ -43,14 +45,19 @@ const run = async () => {
const errors: any[] = []; const errors: any[] = [];
for await (const repos of github.paginate.iterator(action, { visibility: 'all' })) { for await (const repos of github.paginate.iterator(action, { visibility: 'all' })) {
for (const repo of repos.data) { for (const repo of repos.data) {
const loader = ora(repo.full_name).start(); const loader = ora('preparing');
loader.prefixText = repo.full_name;
loader.start();
try { try {
const repoBackupLocation = path.join(backupLocation, repo.full_name); const repoBackupLocation = path.join(backupLocation, repo.full_name);
const infoLocation = path.join(repoBackupLocation, 'info.json'); const infoLocation = path.join(repoBackupLocation, 'info.json');
const gitLocation = path.join(repoBackupLocation, 'git'); const gitLocation = path.join(repoBackupLocation, 'git');
await fs.mkdirp(repoBackupLocation); await fs.mkdirp(repoBackupLocation);
loader.text = 'fething info';
await fs.writeFile(infoLocation, JSON.stringify(repo, null, ' '), 'utf-8'); await fs.writeFile(infoLocation, JSON.stringify(repo, null, ' '), 'utf-8');
loader.text = 'mirroring';
await mirror(gitLocation, repo.full_name); await mirror(gitLocation, repo.full_name);
loader.text = '';
loader.succeed(); loader.succeed();
} catch (err) { } catch (err) {
loader.fail(err.toString()); loader.fail(err.toString());