Allow filtering out forks when mirroring

main
Jacobo de Vera 2024-07-20 01:11:41 +01:00
parent 81e11cde9b
commit 3e8eab3655
No known key found for this signature in database
GPG Key ID: A0978FF8800D5BD7
2 changed files with 21 additions and 12 deletions

View File

@ -56,6 +56,7 @@ services:
# - GITHUB_TOKEN=please-exchange-with-token # 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 # - MIRROR_PRIVATE_REPOSITORIES=true # Optional, set to mirror private repos
# - DELAY=3600 # Optional, set to change the delay between checks (in seconds) # - DELAY=3600 # Optional, set to change the delay between checks (in seconds)
# - SKIP_FORKS=true # Optional, set to skip forks
# - DRY_RUN=true # Optional, set to only log what would be done # - DRY_RUN=true # Optional, set to only log what would be done
container_name: mirror-to-gitea container_name: mirror-to-gitea
``` ```
@ -98,7 +99,8 @@ In your Docker Compose file, replace `jaedle/mirror-to-gitea:latest` with `build
### Optional ### Optional
- `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!** - `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. - `MIRROR_PRIVATE_REPOSITORIES`: If set to `true` or `1`, your private GitHub repositories will also be mirrored to gitea. The `GITHUB_TOKEN` parameter must be set for this to work.
- `SKIP_FORKS`: If set to `true` or `1`, forks will NOT be mirrored.
- `DELAY`: How often to check for new repositories in seconds. Default is 3600 (1 hour). - `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. - `DRY_RUN`: If set to `true` or `1`, the script will only log what would be done, but not actually create any mirror.

View File

@ -3,30 +3,36 @@ const request = require('superagent');
const {default: PQueue} = require('p-queue'); const {default: PQueue} = require('p-queue');
async function getGithubRepositories(username, token, mirrorPrivateRepositories) { async function getGithubRepositories(username, token, mirrorPrivateRepositories, mirrorForks) {
const octokit = new Octokit({ const octokit = new Octokit({
auth: token || null, auth: token || null,
}); });
const publicRepositoriesWithForks = await octokit.paginate('GET /users/:username/repos', { username: username }) const publicRepositories = await octokit.paginate('GET /users/:username/repos', { username: username })
.then(repositories => toRepositoryList(repositories)); .then(repositories => toRepositoryList(repositories));
let allRepositoriesWithoutForks; let allOwnedRepositories;
if(mirrorPrivateRepositories === 'true'){ if(mirrorPrivateRepositories){
allRepositoriesWithoutForks = await octokit.paginate('GET /user/repos?visibility=public&affiliation=owner&visibility=private') allOwnedRepositories = await octokit.paginate('GET /user/repos?visibility=public&affiliation=owner&visibility=private')
.then(repositories => toRepositoryList(repositories)); .then(repositories => toRepositoryList(repositories));
} }
if(mirrorPrivateRepositories === 'true'){ let repositories = publicRepositories;
return filterDuplicates(allRepositoriesWithoutForks.concat(publicRepositoriesWithForks));
}else{ if(mirrorPrivateRepositories) {
return publicRepositoriesWithForks; repositories = filterDuplicates(allOwnedRepositories.concat(publicRepositories));
} }
if(!mirrorForks){
repositories = repositories.filter(repository => !repository.fork);
}
return repositories;
} }
function toRepositoryList(repositories) { function toRepositoryList(repositories) {
return repositories.map(repository => { return repositories.map(repository => {
return { name: repository.name, url: repository.clone_url, private: repository.private }; return { name: repository.name, url: repository.clone_url, private: repository.private, fork: repository.fork};
}); });
} }
@ -101,6 +107,7 @@ async function main() {
console.error('No GITHUB_USERNAME specified, please specify! Exiting..'); console.error('No GITHUB_USERNAME specified, please specify! Exiting..');
return; return;
} }
const mirrorForks = ! ['1', 'true'].includes(process.env.SKIP_FORKS);
const githubToken = process.env.GITHUB_TOKEN; const githubToken = process.env.GITHUB_TOKEN;
const giteaUrl = process.env.GITEA_URL; const giteaUrl = process.env.GITEA_URL;
@ -123,7 +130,7 @@ async function main() {
const dryRun = ['1', 'true'].includes(process.env.DRY_RUN); const dryRun = ['1', 'true'].includes(process.env.DRY_RUN);
const githubRepositories = await getGithubRepositories(githubUsername, githubToken, mirrorPrivateRepositories); const githubRepositories = await getGithubRepositories(githubUsername, githubToken, mirrorPrivateRepositories, mirrorForks);
console.log(`Found ${githubRepositories.length} repositories on github`); console.log(`Found ${githubRepositories.length} repositories on github`);
const gitea = { const gitea = {