Skip to content

Commit

Permalink
Merge pull request #18 from jdevera/skip_forks
Browse files Browse the repository at this point in the history
Allow skipping forks (and a dry-run bonus)
  • Loading branch information
jaedle authored Sep 24, 2024
2 parents b56d594 + 9217135 commit db7655a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 37 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
});
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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;

Expand All @@ -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);

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);
const githubRepositories = await getGithubRepositories(githubUsername, githubToken, mirrorPrivateRepositories, mirrorForks);
console.log(`Found ${githubRepositories.length} repositories on github`);

const gitea = {
Expand All @@ -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);
};
}));
}
Expand Down

0 comments on commit db7655a

Please sign in to comment.