mirror of
https://github.com/jaedle/mirror-to-gitea.git
synced 2026-01-09 12:57:55 -05:00
Merge pull request #18 from jdevera/skip_forks
Allow skipping forks (and a dry-run bonus)
This commit is contained in:
12
README.md
12
README.md
@@ -48,14 +48,16 @@ version: "3.3"
|
||||
services:
|
||||
mirror-to-gitea:
|
||||
image: jaedle/mirror-to-gitea:latest
|
||||
restart: always
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
- 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)
|
||||
# - SKIP_FORKS=true # Optional, set to skip forks
|
||||
# - DRY_RUN=true # Optional, set to only log what would be done
|
||||
container_name: mirror-to-gitea
|
||||
```
|
||||
## Building from Source
|
||||
@@ -97,6 +99,8 @@ In your Docker Compose file, replace `jaedle/mirror-to-gitea:latest` with `build
|
||||
|
||||
### 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!**
|
||||
- `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).
|
||||
- `DRY_RUN`: If set to `true` or `1`, the script will only log what would be done, but not actually create any mirror.
|
||||
|
||||
|
||||
52
src/index.js
52
src/index.js
@@ -3,30 +3,36 @@ const request = require('superagent');
|
||||
const {default: PQueue} = require('p-queue');
|
||||
|
||||
|
||||
async function getGithubRepositories(username, token, mirrorPrivateRepositories) {
|
||||
async function getGithubRepositories(username, token, mirrorPrivateRepositories, mirrorForks) {
|
||||
const octokit = new Octokit({
|
||||
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));
|
||||
|
||||
let allRepositoriesWithoutForks;
|
||||
if(mirrorPrivateRepositories === 'true'){
|
||||
allRepositoriesWithoutForks = await octokit.paginate('GET /user/repos?visibility=public&affiliation=owner&visibility=private')
|
||||
let allOwnedRepositories;
|
||||
if(mirrorPrivateRepositories){
|
||||
allOwnedRepositories = await octokit.paginate('GET /user/repos?visibility=public&affiliation=owner&visibility=private')
|
||||
.then(repositories => toRepositoryList(repositories));
|
||||
}
|
||||
|
||||
if(mirrorPrivateRepositories === 'true'){
|
||||
return filterDuplicates(allRepositoriesWithoutForks.concat(publicRepositoriesWithForks));
|
||||
}else{
|
||||
return publicRepositoriesWithForks;
|
||||
let repositories = publicRepositories;
|
||||
|
||||
if(mirrorPrivateRepositories) {
|
||||
repositories = filterDuplicates(allOwnedRepositories.concat(publicRepositories));
|
||||
}
|
||||
|
||||
if(!mirrorForks){
|
||||
repositories = repositories.filter(repository => !repository.fork);
|
||||
}
|
||||
|
||||
return repositories;
|
||||
}
|
||||
|
||||
function toRepositoryList(repositories) {
|
||||
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};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -80,13 +86,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);
|
||||
}
|
||||
@@ -97,6 +107,7 @@ async function main() {
|
||||
console.error('No GITHUB_USERNAME specified, please specify! Exiting..');
|
||||
return;
|
||||
}
|
||||
const mirrorForks = ! ['1', 'true'].includes(process.env.SKIP_FORKS);
|
||||
const githubToken = process.env.GITHUB_TOKEN;
|
||||
const giteaUrl = process.env.GITEA_URL;
|
||||
|
||||
@@ -111,14 +122,25 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
const mirrorPrivateRepositories = process.env.MIRROR_PRIVATE_REPOSITORIES;
|
||||
if(mirrorPrivateRepositories === 'true' && !githubToken){
|
||||
const mirrorPrivateRepositories = ['1', 'true'].includes(process.env.MIRROR_PRIVATE_REPOSITORIES)
|
||||
if(mirrorPrivateRepositories && !githubToken){
|
||||
console.error('MIRROR_PRIVATE_REPOSITORIES was set to true but no GITHUB_TOKEN was specified, please specify! Exiting..')
|
||||
return;
|
||||
}
|
||||
|
||||
const dryRun = ['1', 'true'].includes(process.env.DRY_RUN);
|
||||
|
||||
const githubRepositories = await getGithubRepositories(githubUsername, githubToken, mirrorPrivateRepositories);
|
||||
console.log("Starting with the following configuration:")
|
||||
console.log(` - GITHUB_USERNAME: ${githubUsername}`);
|
||||
console.log(` - GITHUB_TOKEN: ${githubToken ? '****' : ''}`);
|
||||
console.log(` - GITEA_URL: ${giteaUrl}`);
|
||||
console.log(` - GITEA_TOKEN: ${giteaToken ? '****' : ''}`);
|
||||
console.log(` - MIRROR_PRIVATE_REPOSITORIES: ${mirrorPrivateRepositories}`);
|
||||
console.log(` - SKIP_FORKS: ${!mirrorForks}`);
|
||||
console.log(` - DRY_RUN: ${dryRun}`);
|
||||
|
||||
|
||||
const githubRepositories = await getGithubRepositories(githubUsername, githubToken, mirrorPrivateRepositories, mirrorForks);
|
||||
console.log(`Found ${githubRepositories.length} repositories on github`);
|
||||
|
||||
const gitea = {
|
||||
@@ -130,7 +152,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);
|
||||
};
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user