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

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