Skip to content

Commit

Permalink
Merge pull request #22 from RobertoBochet/filters
Browse files Browse the repository at this point in the history
Add repository filters
  • Loading branch information
jaedle authored Nov 9, 2024
2 parents f47d7b0 + 9a2702f commit 05fbdb6
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 37 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ private repos, they will be created as private repos on your gitea server.
All configuration is performed through environment variables. Flags are considered `true` on `true`, `TRUE` or `1`.

| Parameter | Required | Type | Default | Description |
|-----------------------------|----------|--------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| GITHUB_USERNAME | yes | string | - | The name of the GitHub user or organisation to mirror. |
| GITEA_URL | yes | string | - | The url of your Gitea server. |
| GITEA_TOKEN | yes | string | - | The token for your gitea user (Settings -> Applications -> Generate New Token). **Attention: if this is set, the token will be transmitted to your specified Gitea instance!** |
| GITHUB_TOKEN | no* | string | - | GitHub token (PAT). Is mandatory in combination with `MIRROR_PRIVATE_REPOSITORIES`. |
| MIRROR_PRIVATE_REPOSITORIES | no | bool | FALSE | If set to `true` your private GitHub Repositories will be mirrored to Gitea. Requires `GITHUB_TOKEN`. |
| SKIP_FORKS | no | bool | FALSE | If set to `true` will disable the mirroring of forks from your GitHub User / Organisation. |
| DELAY | no | int | 3600 | Number of seconds between program executions. Setting this will only affect how soon after a new repo was created a mirror may appar on Gitea, but has no affect on the ongoing replication. |
| DRY_RUN | no | bool | FALSE | If set to `true` will perform no writing changes to your Gitea instance, but log the planned actions. |
|-----------------------------|----------|--------|--------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| GITHUB_USERNAME | yes | string | - | The name of the GitHub user or organisation to mirror. |
| GITEA_URL | yes | string | - | The url of your Gitea server. |
| GITEA_TOKEN | yes | string | - | The token for your gitea user (Settings -> Applications -> Generate New Token). **Attention: if this is set, the token will be transmitted to your specified Gitea instance!** |
| GITHUB_TOKEN | no* | string | - | GitHub token (PAT). Is mandatory in combination with `MIRROR_PRIVATE_REPOSITORIES`. |
| MIRROR_PRIVATE_REPOSITORIES | no | bool | FALSE | If set to `true` your private GitHub Repositories will be mirrored to Gitea. Requires `GITHUB_TOKEN`. |
| SKIP_FORKS | no | bool | FALSE | If set to `true` will disable the mirroring of forks from your GitHub User / Organisation. |
| DELAY | no | int | 3600 | Number of seconds between program executions. Setting this will only affect how soon after a new repo was created a mirror may appar on Gitea, but has no affect on the ongoing replication. |
| DRY_RUN | no | bool | FALSE | If set to `true` will perform no writing changes to your Gitea instance, but log the planned actions. |
| INCLUDE | no | string | "*" | It defines which repos have to be mirrored. It supports glob format, multiple filters can be separated with commas (`,`) |
| EXCLUDE | no | string | "" | It defines which repos are not to be mirrored. It supports glob format, multiple filters can be separated with commas (`,`). `EXCLUDE` filters are applied after `INCLUDE` ones. |

### Docker

Expand Down
144 changes: 117 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@babel/plugin-transform-runtime": "^7.25.4",
"@octokit/rest": "^21.0.2",
"@types/jest": "^29.5.13",
"minimatch": "^10.0.1",
"p-queue": "^8.0.1",
"superagent": "^10.1.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/configuration.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const readEnv = (variable) => {
const val = process.env[variable];
if (val === undefined || val.length === 0) return undefined;
return val;
};

const mustReadEnv = (variable) => {
const val = process.env[variable];
if (val === undefined || val.length === 0) {
Expand All @@ -21,6 +27,8 @@ function readInt(variable) {

export function configuration() {
const defaultDelay = 3600;
const defaultInclude = "*";
const defaultExclude = "";
const config = {
github: {
username: mustReadEnv("GITHUB_USERNAME"),
Expand All @@ -34,6 +42,8 @@ export function configuration() {
},
dryRun: readBoolean("DRY_RUN"),
delay: readInt("DELAY") ?? defaultDelay,
include: (readEnv("INCLUDE") ?? defaultInclude).split(",").map(f => f.trim()),
exclude: (readEnv("EXCLUDE") ?? defaultExclude).split(",").map(f => f.trim()),
};

if (config.github.privateRepositories && config.github.token === undefined) {
Expand Down
15 changes: 14 additions & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Octokit } from "@octokit/rest";
import { minimatch } from "minimatch";
import PQueue from "p-queue";
import * as request from "superagent";
import request from "superagent";
import { configuration } from "./configuration.mjs";

async function getGithubRepositories(
username,
token,
mirrorPrivateRepositories,
mirrorForks,
include,
exclude,
) {
const octokit = new Octokit({
auth: token || null,
Expand Down Expand Up @@ -38,6 +41,12 @@ async function getGithubRepositories(
repositories = repositories.filter((repository) => !repository.fork);
}

repositories = repositories.filter(
(repository) =>
include.some((f) => minimatch(repository.name, f)) &&
!exclude.some((f) => minimatch(repository.name, f)),
);

return repositories;
}

Expand Down Expand Up @@ -136,12 +145,16 @@ async function main() {
console.log(` - GITEA_TOKEN: ${config.gitea.token ? "****" : ""}`);
console.log(` - SKIP_FORKS: ${config.github.skipForks}`);
console.log(` - DRY_RUN: ${config.dryRun}`);
console.log(` - INCLUDE: ${config.include}`);
console.log(` - EXCLUDE: ${config.exclude}`);

const githubRepositories = await getGithubRepositories(
config.github.username,
config.github.token,
config.github.privateRepositories,
!config.github.skipForks,
config.include,
config.exclude,
);

console.log(`Found ${githubRepositories.length} repositories on github`);
Expand Down

0 comments on commit 05fbdb6

Please sign in to comment.