From 9b535f621b66a3d7260ea6c50da88913b7a759fc Mon Sep 17 00:00:00 2001 From: Niccolo Borgioli Date: Wed, 5 Apr 2023 20:48:25 +0200 Subject: [PATCH] also update visibility when syncing --- package.json | 2 +- src/api/gitea.ts | 10 ++++++++++ src/core.ts | 15 ++++++++++----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index c9f93fb..ef80703 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "1.0.0", + "version": "1.1.0", "type": "module", "scripts": { "start": "node .", diff --git a/src/api/gitea.ts b/src/api/gitea.ts index b76f55c..6430a3f 100644 --- a/src/api/gitea.ts +++ b/src/api/gitea.ts @@ -3,6 +3,7 @@ import axios, { AxiosError } from 'axios' import { Config } from '../config.js' import { logger } from '../logger.js' import { ListRepositoriesResponse } from './gitea.types.js' +import { Repository } from './github.types.js' const Base = axios.create({ baseURL: new URL('/api/v1', Config.gitea.host).href, @@ -77,3 +78,12 @@ export async function listAllRepositories() { logger.debug('Listed all repositories in Gitea', { data: repos }) return repos } + +export async function updateRepository(owner: string, repo: string, body: Partial) { + logger.debug('Updating repository', { owner, repo, body }) + await Base({ + url: `/repos/${owner}/${repo}`, + method: 'PATCH', + data: body, + }) +} diff --git a/src/core.ts b/src/core.ts index b48562b..14f49cd 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,4 +1,4 @@ -import { listAllRepositories as giteaRepos, mirror, MirrorOptions } from './api/gitea.js' +import { listAllRepositories as giteaRepos, mirror, MirrorOptions, updateRepository } from './api/gitea.js' import { listAllRepositories as githubRepos } from './api/github.js' import { Config } from './config.js' import { logger } from './logger.js' @@ -20,7 +20,12 @@ export async function sync() { const sameName = syncedRepos.find((r) => r.name === repo.name || r.original_url === repo.clone_url) if (sameName) { if (sameName.original_url === repo.clone_url) { - logger.info('Already synced, skipping', { name: repo.name }) + if (sameName.private === repo.private) logger.info('Already synced, skipping', { name: repo.name }) + else { + logger.info('Visibility changed, updating', { name: repo.name }) + const [owner, repository] = sameName.full_name.split('/') + await updateRepository(owner, repository, { private: repo.private }) + } } else { logger.error('Repo with same name but different url', { name: repo.name, @@ -42,9 +47,9 @@ export async function sync() { logger.info('Mirrored repository', { name: repo.name }) } logger.info('Finished sync') - } catch (e) { - logger.error(e) - logger.error('Failed to sync') + } catch (error) { + logger.debug(error) + logger.error('Failed to sync', { error: error instanceof Error ? error.message : 'Unknown error' }) } finally { running = false }