Skip to content

Commit 05fbdb6

Browse files
authored
Merge pull request #22 from RobertoBochet/filters
Add repository filters
2 parents f47d7b0 + 9a2702f commit 05fbdb6

File tree

5 files changed

+153
-37
lines changed

5 files changed

+153
-37
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ private repos, they will be created as private repos on your gitea server.
3535
All configuration is performed through environment variables. Flags are considered `true` on `true`, `TRUE` or `1`.
3636

3737
| Parameter | Required | Type | Default | Description |
38-
|-----------------------------|----------|--------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
39-
| GITHUB_USERNAME | yes | string | - | The name of the GitHub user or organisation to mirror. |
40-
| GITEA_URL | yes | string | - | The url of your Gitea server. |
41-
| 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!** |
42-
| GITHUB_TOKEN | no* | string | - | GitHub token (PAT). Is mandatory in combination with `MIRROR_PRIVATE_REPOSITORIES`. |
43-
| MIRROR_PRIVATE_REPOSITORIES | no | bool | FALSE | If set to `true` your private GitHub Repositories will be mirrored to Gitea. Requires `GITHUB_TOKEN`. |
44-
| SKIP_FORKS | no | bool | FALSE | If set to `true` will disable the mirroring of forks from your GitHub User / Organisation. |
45-
| 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. |
46-
| DRY_RUN | no | bool | FALSE | If set to `true` will perform no writing changes to your Gitea instance, but log the planned actions. |
38+
|-----------------------------|----------|--------|--------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
39+
| GITHUB_USERNAME | yes | string | - | The name of the GitHub user or organisation to mirror. |
40+
| GITEA_URL | yes | string | - | The url of your Gitea server. |
41+
| 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!** |
42+
| GITHUB_TOKEN | no* | string | - | GitHub token (PAT). Is mandatory in combination with `MIRROR_PRIVATE_REPOSITORIES`. |
43+
| MIRROR_PRIVATE_REPOSITORIES | no | bool | FALSE | If set to `true` your private GitHub Repositories will be mirrored to Gitea. Requires `GITHUB_TOKEN`. |
44+
| SKIP_FORKS | no | bool | FALSE | If set to `true` will disable the mirroring of forks from your GitHub User / Organisation. |
45+
| 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. |
46+
| DRY_RUN | no | bool | FALSE | If set to `true` will perform no writing changes to your Gitea instance, but log the planned actions. |
47+
| INCLUDE | no | string | "*" | It defines which repos have to be mirrored. It supports glob format, multiple filters can be separated with commas (`,`) |
48+
| 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. |
4749

4850
### Docker
4951

package-lock.json

Lines changed: 117 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@babel/plugin-transform-runtime": "^7.25.4",
2626
"@octokit/rest": "^21.0.2",
2727
"@types/jest": "^29.5.13",
28+
"minimatch": "^10.0.1",
2829
"p-queue": "^8.0.1",
2930
"superagent": "^10.1.0"
3031
},

src/configuration.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
const readEnv = (variable) => {
2+
const val = process.env[variable];
3+
if (val === undefined || val.length === 0) return undefined;
4+
return val;
5+
};
6+
17
const mustReadEnv = (variable) => {
28
const val = process.env[variable];
39
if (val === undefined || val.length === 0) {
@@ -21,6 +27,8 @@ function readInt(variable) {
2127

2228
export function configuration() {
2329
const defaultDelay = 3600;
30+
const defaultInclude = "*";
31+
const defaultExclude = "";
2432
const config = {
2533
github: {
2634
username: mustReadEnv("GITHUB_USERNAME"),
@@ -34,6 +42,8 @@ export function configuration() {
3442
},
3543
dryRun: readBoolean("DRY_RUN"),
3644
delay: readInt("DELAY") ?? defaultDelay,
45+
include: (readEnv("INCLUDE") ?? defaultInclude).split(",").map(f => f.trim()),
46+
exclude: (readEnv("EXCLUDE") ?? defaultExclude).split(",").map(f => f.trim()),
3747
};
3848

3949
if (config.github.privateRepositories && config.github.token === undefined) {

src/index.mjs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { Octokit } from "@octokit/rest";
2+
import { minimatch } from "minimatch";
23
import PQueue from "p-queue";
3-
import * as request from "superagent";
4+
import request from "superagent";
45
import { configuration } from "./configuration.mjs";
56

67
async function getGithubRepositories(
78
username,
89
token,
910
mirrorPrivateRepositories,
1011
mirrorForks,
12+
include,
13+
exclude,
1114
) {
1215
const octokit = new Octokit({
1316
auth: token || null,
@@ -38,6 +41,12 @@ async function getGithubRepositories(
3841
repositories = repositories.filter((repository) => !repository.fork);
3942
}
4043

44+
repositories = repositories.filter(
45+
(repository) =>
46+
include.some((f) => minimatch(repository.name, f)) &&
47+
!exclude.some((f) => minimatch(repository.name, f)),
48+
);
49+
4150
return repositories;
4251
}
4352

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

140151
const githubRepositories = await getGithubRepositories(
141152
config.github.username,
142153
config.github.token,
143154
config.github.privateRepositories,
144155
!config.github.skipForks,
156+
config.include,
157+
config.exclude,
145158
);
146159

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

0 commit comments

Comments
 (0)