From 2e34bf8ca0e9f32a607d3bcf25716660b9172b02 Mon Sep 17 00:00:00 2001 From: Niccolo Borgioli Date: Mon, 16 Dec 2024 12:24:25 +0100 Subject: [PATCH] add run_once and warning about tangling repos --- README.md | 6 ++++++ src/config.ts | 1 + src/core.ts | 7 +++++++ src/index.ts | 10 +++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 621e631..64b0efd 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,12 @@ GITEA_TOKEN= # Cron schedule CRON="0 */2 * * *" + +# Debug level +DEBUG_LEVEL=debug + +# Only run one, skip cron and exit +RUN_ONCE=1 ``` ## Known limitations diff --git a/src/config.ts b/src/config.ts index 3642334..f887607 100644 --- a/src/config.ts +++ b/src/config.ts @@ -35,5 +35,6 @@ export const Config = { token: simple('GITEA_TOKEN'), }, cron: getEnv('CRON', '0 */2 * * *'), + runOnce: getEnv('RUN_ONCE', false, Boolean), version: getEnv('npm_package_version', 'unknown'), } diff --git a/src/core.ts b/src/core.ts index 619061f..7482c6d 100644 --- a/src/core.ts +++ b/src/core.ts @@ -18,10 +18,14 @@ export async function sync() { const toSync = await githubRepos() l.debug('loaded repos', { remote: toSync.length, local: syncedRepos.length }) + // List of all the repos in gitea, that are not on github + const notInSource = new Set(syncedRepos.map((r) => r.name)) + for (const repo of toSync) { const lr = l.child({ repo: repo.name }) const sameName = syncedRepos.find((r) => r.name === repo.name || r.original_url === repo.clone_url) if (sameName) { + notInSource.delete(sameName.name) if (sameName.original_url === repo.clone_url) { if (sameName.private === repo.private) logger.info('Already synced, skipping', { name: repo.name }) else { @@ -52,6 +56,9 @@ export async function sync() { await mirror(options) lr.info('mirrored repository') } + if (notInSource.size) { + l.info(`Found ${notInSource.size} surplus repositories in gitea`, { repos: [...notInSource] }) + } l.info('Finished sync') } catch (error) { l.debug(error) diff --git a/src/index.ts b/src/index.ts index 4793f83..3a5d3dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,5 +6,13 @@ import { logger } from './logger.ts' logger.info(`Mirror manager - ${Config.version}`, { version: Config.version }) +Deno.addSignalListener('SIGINT', () => { + console.log('exiting...') + Deno.exit() +}) + +// Run on startup once, then delegate to cron await sync() -cron.schedule(Config.cron, sync) +if (!Config.runOnce) { + cron.schedule(Config.cron, sync) +}