Add new DRY_RUN parameter that prevents writing to gitea

This is so that the script can be tested more easily without introducing
changes in the destination Gitea instance.
main
Jacobo de Vera 2024-07-20 01:11:13 +01:00
parent 2bc1e5e027
commit 60ae549d13
No known key found for this signature in database
GPG Key ID: A0978FF8800D5BD7
2 changed files with 11 additions and 4 deletions

View File

@ -53,9 +53,10 @@ services:
- GITHUB_USERNAME=github-user
- GITEA_URL=https://your-gitea.url
- GITEA_TOKEN=please-exchange-with-token
#- GITHUB_TOKEN=please-exchange-with-token # Optional, set to mirror private repos
#- MIRROR_PRIVATE_REPOSITORIES=true # Optional, set to mirror private repos
# - GITHUB_TOKEN=please-exchange-with-token # Optional, set to mirror private repos
# - MIRROR_PRIVATE_REPOSITORIES=true # Optional, set to mirror private repos
# - DELAY=3600 # Optional, set to change the delay between checks (in seconds)
# - DRY_RUN=true # Optional, set to only log what would be done
container_name: mirror-to-gitea
```
## Building from Source
@ -99,6 +100,7 @@ In your Docker Compose file, replace `jaedle/mirror-to-gitea:latest` with `build
- `GITHUB_TOKEN`: [GitHub personal access token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token). **Attention: if this is set, the token will be transmitted to your specified Gitea instance!**
- `MIRROR_PRIVATE_REPOSITORIES`: If set to `true`, your private GitHub repositories will also be mirrored to gitea. The `GITHUB_TOKEN` parameter must be set for this to work.
- `DELAY`: How often to check for new repositories in seconds. Default is 3600 (1 hour).
- `DRY_RUN`: If set to `true` or `1`, the script will only log what would be done, but not actually create any mirror.
## Things to do

View File

@ -80,13 +80,17 @@ function mirrorOnGitea(repository, gitea, giteaUser, githubToken) {
}
async function mirror(repository, gitea, giteaUser, githubToken) {
async function mirror(repository, gitea, giteaUser, githubToken, dryRun) {
if (await isAlreadyMirroredOnGitea(repository.name,
gitea,
giteaUser)) {
console.log('Repository is already mirrored; doing nothing: ', repository.name);
return;
}
if (dryRun) {
console.log('DRY RUN: Would mirror repository to gitea: ', repository);
return;
}
console.log('Mirroring repository to gitea: ', repository.name);
await mirrorOnGitea(repository, gitea, giteaUser, githubToken);
}
@ -117,6 +121,7 @@ async function main() {
return;
}
const dryRun = ['1', 'true'].includes(process.env.DRY_RUN);
const githubRepositories = await getGithubRepositories(githubUsername, githubToken, mirrorPrivateRepositories);
console.log(`Found ${githubRepositories.length} repositories on github`);
@ -130,7 +135,7 @@ async function main() {
const queue = new PQueue({ concurrency: 4 });
await queue.addAll(githubRepositories.map(repository => {
return async () => {
await mirror(repository, gitea, giteaUser, githubToken);
await mirror(repository, gitea, giteaUser, githubToken, dryRun);
};
}));
}