Skip to content

Commit 7bd8626

Browse files
committed
More fixes
1 parent 251baeb commit 7bd8626

File tree

7 files changed

+47
-73
lines changed

7 files changed

+47
-73
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"scripts": {
99
"setup": "bun install && bun run manage-db init",
10-
"dev": "bunx --bun astro dev --port 4567",
10+
"dev": "bunx --bun astro dev --port 9876",
1111
"dev:clean": "bun run cleanup-db && bun run manage-db init && bunx --bun astro dev",
1212
"build": "bunx --bun astro build",
1313
"cleanup-db": "rm -f gitea-mirror.db data/gitea-mirror.db",

src/lib/db/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const githubConfigSchema = z.object({
2424
includePublic: z.boolean().default(true),
2525
includeOrganizations: z.array(z.string()).default([]),
2626
starredReposOrg: z.string().optional(),
27-
mirrorStrategy: z.enum(["preserve", "single-org", "flat-user"]).default("preserve"),
27+
mirrorStrategy: z.enum(["preserve", "single-org", "flat-user", "mixed"]).default("preserve"),
2828
defaultOrg: z.string().optional(),
2929
});
3030

src/lib/gitea.test.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,13 @@ describe("getGiteaRepoOwner - Organization Override Tests", () => {
312312
skipStarredIssues: false
313313
},
314314
giteaConfig: {
315-
username: "giteauser",
315+
defaultOwner: "giteauser",
316316
url: "https://gitea.example.com",
317317
token: "gitea-token",
318-
organization: "github-mirrors",
318+
defaultOrg: "github-mirrors",
319319
visibility: "public",
320320
starredReposOrg: "starred",
321-
preserveOrgStructure: false,
321+
preserveVisibility: false,
322322
mirrorStrategy: "preserve"
323323
}
324324
};
@@ -354,18 +354,7 @@ describe("getGiteaRepoOwner - Organization Override Tests", () => {
354354
expect(result).toBe("starred");
355355
});
356356

357-
test("preserve strategy: personal repos use personalReposOrg override", () => {
358-
const configWithOverride = {
359-
...baseConfig,
360-
giteaConfig: {
361-
...baseConfig.giteaConfig!,
362-
personalReposOrg: "my-personal-mirrors"
363-
}
364-
};
365-
const repo = { ...baseRepo, organization: undefined };
366-
const result = getGiteaRepoOwner({ config: configWithOverride, repository: repo });
367-
expect(result).toBe("my-personal-mirrors");
368-
});
357+
// Removed test for personalReposOrg as this field no longer exists
369358

370359
test("preserve strategy: personal repos fallback to username when no override", () => {
371360
const repo = { ...baseRepo, organization: undefined };
@@ -379,7 +368,7 @@ describe("getGiteaRepoOwner - Organization Override Tests", () => {
379368
expect(result).toBe("myorg");
380369
});
381370

382-
test("mixed strategy: personal repos go to organization", () => {
371+
test("single-org strategy: personal repos go to defaultOrg", () => {
383372
const configWithMixed = {
384373
...baseConfig,
385374
giteaConfig: {
@@ -393,7 +382,7 @@ describe("getGiteaRepoOwner - Organization Override Tests", () => {
393382
expect(result).toBe("github-mirrors");
394383
});
395384

396-
test("mixed strategy: org repos preserve their structure", () => {
385+
test("single-org strategy: org repos also go to defaultOrg", () => {
397386
const configWithMixed = {
398387
...baseConfig,
399388
giteaConfig: {
@@ -407,18 +396,16 @@ describe("getGiteaRepoOwner - Organization Override Tests", () => {
407396
expect(result).toBe("myorg");
408397
});
409398

410-
test("mixed strategy: fallback to username if no org configs", () => {
411-
const configWithMixed = {
399+
test("flat-user strategy: all repos go to defaultOwner", () => {
400+
const configWithFlatUser = {
412401
...baseConfig,
413402
giteaConfig: {
414403
...baseConfig.giteaConfig!,
415-
mirrorStrategy: "mixed" as const,
416-
organization: undefined,
417-
personalReposOrg: undefined
404+
mirrorStrategy: "flat-user" as const
418405
}
419406
};
420-
const repo = { ...baseRepo, organization: undefined };
421-
const result = getGiteaRepoOwner({ config: configWithMixed, repository: repo });
407+
const repo = { ...baseRepo, organization: "myorg" };
408+
const result = getGiteaRepoOwner({ config: configWithFlatUser, repository: repo });
422409
expect(result).toBe("giteauser");
423410
});
424411
});

src/lib/gitea.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const getGiteaRepoOwnerAsync = async ({
6464
throw new Error("GitHub or Gitea config is required.");
6565
}
6666

67-
if (!config.giteaConfig.username) {
67+
if (!config.giteaConfig.defaultOwner) {
6868
throw new Error("Gitea username is required.");
6969
}
7070

@@ -96,11 +96,7 @@ export const getGiteaRepoOwnerAsync = async ({
9696
}
9797
}
9898

99-
// Check for personal repos override (when it's user's repo, not an organization)
100-
if (!repository.organization && config.giteaConfig.personalReposOrg) {
101-
console.log(`Using personal repos override: ${config.giteaConfig.personalReposOrg}`);
102-
return config.giteaConfig.personalReposOrg;
103-
}
99+
// For personal repos (not organization repos), fall back to the default strategy
104100

105101
// Fall back to existing strategy logic
106102
return getGiteaRepoOwner({ config, repository });
@@ -117,7 +113,7 @@ export const getGiteaRepoOwner = ({
117113
throw new Error("GitHub or Gitea config is required.");
118114
}
119115

120-
if (!config.giteaConfig.username) {
116+
if (!config.giteaConfig.defaultOwner) {
121117
throw new Error("Gitea username is required.");
122118
}
123119

@@ -137,19 +133,19 @@ export const getGiteaRepoOwner = ({
137133
return repository.organization;
138134
}
139135
// Use personal repos override if configured, otherwise use username
140-
return config.giteaConfig.personalReposOrg || config.giteaConfig.username;
136+
return config.giteaConfig.defaultOwner;
141137

142138
case "single-org":
143139
// All non-starred repos go to the destination organization
144140
if (config.giteaConfig.organization) {
145141
return config.giteaConfig.organization;
146142
}
147143
// Fallback to username if no organization specified
148-
return config.giteaConfig.username;
144+
return config.giteaConfig.defaultOwner;
149145

150146
case "flat-user":
151147
// All non-starred repos go under the user account
152-
return config.giteaConfig.username;
148+
return config.giteaConfig.defaultOwner;
153149

154150
case "mixed":
155151
// Mixed mode: personal repos to single org, organization repos preserve structure
@@ -162,11 +158,11 @@ export const getGiteaRepoOwner = ({
162158
return config.giteaConfig.organization;
163159
}
164160
// Fallback to username if no organization specified
165-
return config.giteaConfig.username;
161+
return config.giteaConfig.defaultOwner;
166162

167163
default:
168164
// Default fallback
169-
return config.giteaConfig.username;
165+
return config.giteaConfig.defaultOwner;
170166
}
171167
};
172168

@@ -268,7 +264,7 @@ export const mirrorGithubRepoToGitea = async ({
268264
throw new Error("github config and gitea config are required.");
269265
}
270266

271-
if (!config.giteaConfig.username) {
267+
if (!config.giteaConfig.defaultOwner) {
272268
throw new Error("Gitea username is required.");
273269
}
274270

@@ -357,7 +353,7 @@ export const mirrorGithubRepoToGitea = async ({
357353
const apiUrl = `${config.giteaConfig.url}/api/v1/repos/migrate`;
358354

359355
// Handle organization creation if needed for single-org or preserve strategies
360-
if (repoOwner !== config.giteaConfig.username && !repository.isStarred) {
356+
if (repoOwner !== config.giteaConfig.defaultOwner && !repository.isStarred) {
361357
// Need to create the organization if it doesn't exist
362358
await getOrCreateGiteaOrg({
363359
orgName: repoOwner,
@@ -383,11 +379,13 @@ export const mirrorGithubRepoToGitea = async ({
383379
);
384380

385381
//mirror releases
386-
await mirrorGitHubReleasesToGitea({
387-
config,
388-
octokit,
389-
repository,
390-
});
382+
if (config.githubConfig?.mirrorReleases) {
383+
await mirrorGitHubReleasesToGitea({
384+
config,
385+
octokit,
386+
repository,
387+
});
388+
}
391389

392390
// clone issues
393391
// Skip issues for starred repos if skipStarredIssues is enabled
@@ -738,11 +736,13 @@ export async function mirrorGitHubRepoToGiteaOrg({
738736
);
739737

740738
//mirror releases
741-
await mirrorGitHubReleasesToGitea({
742-
config,
743-
octokit,
744-
repository,
745-
});
739+
if (config.githubConfig?.mirrorReleases) {
740+
await mirrorGitHubReleasesToGitea({
741+
config,
742+
octokit,
743+
repository,
744+
});
745+
}
746746

747747
// Clone issues
748748
// Skip issues for starred repos if skipStarredIssues is enabled
@@ -906,7 +906,7 @@ export async function mirrorGitHubOrgToGitea({
906906
// Determine the target organization based on strategy
907907
if (mirrorStrategy === "single-org" && config.giteaConfig?.organization) {
908908
// For single-org strategy, use the configured destination organization
909-
targetOrgName = config.giteaConfig.organization;
909+
targetOrgName = config.giteaConfig.defaultOrg || config.giteaConfig.defaultOwner;
910910
giteaOrgId = await getOrCreateGiteaOrg({
911911
orgId: organization.id,
912912
orgName: targetOrgName,
@@ -925,7 +925,7 @@ export async function mirrorGitHubOrgToGitea({
925925
// For flat-user strategy, we shouldn't create organizations at all
926926
// Skip organization creation and let individual repos be handled by getGiteaRepoOwner
927927
console.log(`Using flat-user strategy: repos will be placed under user account`);
928-
targetOrgName = config.giteaConfig?.username || "";
928+
targetOrgName = config.giteaConfig?.defaultOwner || "";
929929
}
930930

931931
//query the db with the org name and get the repos
@@ -1082,7 +1082,7 @@ export const syncGiteaRepo = async ({
10821082
!config.userId ||
10831083
!config.giteaConfig?.url ||
10841084
!config.giteaConfig?.token ||
1085-
!config.giteaConfig?.username
1085+
!config.giteaConfig?.defaultOwner
10861086
) {
10871087
throw new Error("Gitea config is required.");
10881088
}
@@ -1405,7 +1405,7 @@ export async function mirrorGitHubReleasesToGitea({
14051405
config: Partial<Config>;
14061406
}) {
14071407
if (
1408-
!config.giteaConfig?.username ||
1408+
!config.giteaConfig?.defaultOwner ||
14091409
!config.giteaConfig?.token ||
14101410
!config.giteaConfig?.url
14111411
) {

src/pages/api/github/organizations.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,17 @@ export const GET: APIRoute = async ({ request }) => {
6666
baseConditions.push(eq(repositories.isStarred, false));
6767
}
6868

69-
// Get total count with all user config filters applied
70-
const totalConditions = [...baseConditions];
71-
if (githubConfig.skipForks) {
72-
totalConditions.push(eq(repositories.isForked, false));
73-
}
74-
if (!githubConfig.privateRepositories) {
75-
totalConditions.push(eq(repositories.isPrivate, false));
76-
}
77-
69+
// Get actual total count (without user config filters)
7870
const [totalCount] = await db
7971
.select({ count: count() })
8072
.from(repositories)
81-
.where(and(...totalConditions));
82-
83-
// Get public count
84-
const publicConditions = [...baseConditions, eq(repositories.isPrivate, false)];
85-
if (githubConfig.skipForks) {
86-
publicConditions.push(eq(repositories.isForked, false));
87-
}
73+
.where(and(...baseConditions));
8874

75+
// Get public count (actual count, not filtered)
8976
const [publicCount] = await db
9077
.select({ count: count() })
9178
.from(repositories)
92-
.where(and(...publicConditions));
79+
.where(and(...baseConditions, eq(repositories.isPrivate, false)));
9380

9481
// Get private count (always show actual count regardless of config)
9582
const [privateCount] = await db

src/pages/api/job/mirror-repo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const POST: APIRoute = async ({ request }) => {
113113
(config.githubConfig?.preserveOrgStructure ? "preserve" : "flat-user");
114114

115115
const shouldUseOrgMirror =
116-
owner !== config.giteaConfig?.username || // Different owner means org
116+
owner !== config.giteaConfig?.defaultOwner || // Different owner means org
117117
mirrorStrategy === "single-org" || // Single-org strategy always uses org
118118
repoData.isStarred; // Starred repos always go to org
119119

src/pages/api/job/retry-repo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export const POST: APIRoute = async ({ request }) => {
147147
(config.githubConfig?.preserveOrgStructure ? "preserve" : "flat-user");
148148

149149
const shouldUseOrgMirror =
150-
owner !== config.giteaConfig?.username || // Different owner means org
150+
owner !== config.giteaConfig?.defaultOwner || // Different owner means org
151151
mirrorStrategy === "single-org" || // Single-org strategy always uses org
152152
repoData.isStarred; // Starred repos always go to org
153153

0 commit comments

Comments
 (0)