From 0cac771ef02ce017d1aea2ad4b6c622153251014 Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Wed, 2 Apr 2025 17:07:39 +0530 Subject: [PATCH] add support for mirroring starred repositories to a dedicated Gitea organization and skip issues for starred repos --- README.md | 20 ++++++++++++++++++++ run-local.sh | 2 ++ src/configuration.mjs | 2 ++ src/index.mjs | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 120d850..0701ddc 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ All configuration is performed through environment variables. Flags are consider | SINGLE_REPO | no | string | - | URL of a single GitHub repository to mirror (e.g., https://github.com/username/repo or username/repo). When specified, only this repository will be mirrored. Requires `GITHUB_TOKEN`. | | GITEA_ORGANIZATION | no | string | - | Name of a Gitea organization to mirror repositories to. If doesn't exist, will be created. | | GITEA_ORG_VISIBILITY | no | string | public | Visibility of the Gitea organization to create. Can be "public" or "private". | +| GITEA_STARRED_ORGANIZATION | no | string | github | Name of a Gitea organization to mirror starred repositories to. If doesn't exist, will be created. Defaults to "github". | +| SKIP_STARRED_ISSUES | no | bool | FALSE | If set to `true` will not mirror issues for starred repositories, even if `MIRROR_ISSUES` is enabled. | | 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 appear 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. | @@ -143,6 +145,24 @@ docker container run \ jaedle/mirror-to-gitea:latest ``` +### Mirror Starred Repositories to a Dedicated Organization + +```sh +docker container run \ + -d \ + --restart always \ + -e GITHUB_USERNAME=github-user \ + -e GITEA_URL=https://your-gitea.url \ + -e GITEA_TOKEN=please-exchange-with-token \ + -e GITHUB_TOKEN=your-github-token \ + -e MIRROR_STARRED=true \ + -e GITEA_STARRED_ORGANIZATION=github \ + -e SKIP_STARRED_ISSUES=true \ + jaedle/mirror-to-gitea:latest +``` + +This configuration will mirror all starred repositories to a Gitea organization named "github" and will not mirror issues for these starred repositories. + ### Docker Compose ```yaml diff --git a/run-local.sh b/run-local.sh index 29d97e1..2ab79aa 100755 --- a/run-local.sh +++ b/run-local.sh @@ -26,5 +26,7 @@ docker container run \ -e INCLUDE_ORGS="$INCLUDE_ORGS" \ -e EXCLUDE_ORGS="$EXCLUDE_ORGS" \ -e PRESERVE_ORG_STRUCTURE="$PRESERVE_ORG_STRUCTURE" \ + -e GITEA_STARRED_ORGANIZATION="$GITEA_STARRED_ORGANIZATION" \ + -e SKIP_STARRED_ISSUES="$SKIP_STARRED_ISSUES" \ -e DRY_RUN="true" \ jaedle/mirror-to-gitea:development diff --git a/src/configuration.mjs b/src/configuration.mjs index 39acd55..c95918d 100644 --- a/src/configuration.mjs +++ b/src/configuration.mjs @@ -49,12 +49,14 @@ export function configuration() { .map((o) => o.trim()) .filter((o) => o.length > 0), preserveOrgStructure: readBoolean("PRESERVE_ORG_STRUCTURE"), + skipStarredIssues: readBoolean("SKIP_STARRED_ISSUES"), }, gitea: { url: mustReadEnv("GITEA_URL"), token: mustReadEnv("GITEA_TOKEN"), organization: readEnv("GITEA_ORGANIZATION"), visibility: readEnv("GITEA_ORG_VISIBILITY") || "public", + starredReposOrg: readEnv("GITEA_STARRED_ORGANIZATION") || "github", }, dryRun: readBoolean("DRY_RUN"), delay: readInt("DELAY") ?? defaultDelay, diff --git a/src/index.mjs b/src/index.mjs index 1243339..f4d1885 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -30,6 +30,19 @@ async function main() { config.dryRun ); } + + // Create the starred repositories organization if mirror starred is enabled + if (config.github.mirrorStarred && config.gitea.starredReposOrg) { + await createGiteaOrganization( + { + url: config.gitea.url, + token: config.gitea.token, + }, + config.gitea.starredReposOrg, + config.gitea.visibility, + config.dryRun + ); + } const octokit = new Octokit({ auth: config.github.token || null, @@ -127,7 +140,12 @@ async function main() { await mirror( repository, - gitea, + { + url: config.gitea.url, + token: config.gitea.token, + skipStarredIssues: config.github.skipStarredIssues, + starredReposOrg: config.gitea.starredReposOrg + }, giteaTarget, config.github.token, config.github.mirrorIssues, @@ -365,6 +383,18 @@ async function mirrorIssues(repository, gitea, giteaTarget, githubToken, dryRun) // Mirror a repository async function mirror(repository, gitea, giteaTarget, githubToken, mirrorIssuesFlag, dryRun) { + // For starred repositories, use the starred repos organization if configured + if (repository.starred && gitea.starredReposOrg) { + // Get the starred repos organization + const starredOrg = await getGiteaOrganization(gitea, gitea.starredReposOrg); + if (starredOrg) { + console.log(`Using organization "${gitea.starredReposOrg}" for starred repository: ${repository.name}`); + giteaTarget = starredOrg; + } else { + console.log(`Could not find organization "${gitea.starredReposOrg}" for starred repositories, using default target`); + } + } + const isAlreadyMirrored = await isAlreadyMirroredOnGitea(repository, gitea, giteaTarget); // Special handling for starred repositories @@ -395,8 +425,14 @@ async function mirror(repository, gitea, giteaTarget, githubToken, mirrorIssuesF } // Mirror issues if requested and not in dry run mode - if (mirrorIssuesFlag && !dryRun) { + // Skip issues for starred repos if the skipStarredIssues option is enabled + const shouldMirrorIssues = mirrorIssuesFlag && + !(repository.starred && gitea.skipStarredIssues); + + if (shouldMirrorIssues && !dryRun) { await mirrorIssues(repository, gitea, giteaTarget, githubToken, dryRun); + } else if (repository.starred && gitea.skipStarredIssues) { + console.log(`Skipping issues for starred repository: ${repository.name}`); } } catch (error) { console.error(`Error during mirroring of ${repository.name}:`, error.message);