diff --git a/backend/src/api/gitea-client.ts b/backend/src/api/gitea-client.ts index 98571fb..6a2e5c8 100644 --- a/backend/src/api/gitea-client.ts +++ b/backend/src/api/gitea-client.ts @@ -58,12 +58,22 @@ export async function getRepos(): Promise { return Array.isArray(data) ? data : []; } -export async function getCommits(owner: string, repo: string, since?: string): Promise { +export interface GiteaBranch { + name: string; +} + +export async function getBranches(owner: string, repo: string): Promise { + const data = await giteaGet(`/repos/${owner}/${repo}/branches?limit=50`); + return Array.isArray(data) ? data : []; +} + +export async function getCommits(owner: string, repo: string, since?: string, branch?: string): Promise { // 分页拉取所有 commit(每页 50,最多 10 页 = 500 条) const all: GiteaCommit[] = []; for (let page = 1; page <= 10; page++) { let path = `/repos/${owner}/${repo}/commits?limit=50&page=${page}`; if (since) path += `&since=${since}`; + if (branch) path += `&sha=${encodeURIComponent(branch)}`; const data = await giteaGet(path); if (!Array.isArray(data) || data.length === 0) break; all.push(...data); diff --git a/backend/src/sync/sync-gitea.ts b/backend/src/sync/sync-gitea.ts index 315ad7d..82b6b9d 100644 --- a/backend/src/sync/sync-gitea.ts +++ b/backend/src/sync/sync-gitea.ts @@ -70,35 +70,43 @@ export async function syncGitea(): Promise { for (const repo of reposToSync) { try { - // Sync commits - const commits = await giteaClient.getCommits(repo.owner, repo.name); - for (const commit of commits) { - const existingCommit = await db.query.gitCommits.findFirst({ - where: eq(gitCommits.sha, commit.sha), - }); + // Sync commits from all branches + const branches = await giteaClient.getBranches(repo.owner, repo.name); + const seenShas = new Set(); - if (!existingCommit) { - const userId = await resolveAuthor( - commit.commit.author.email, - null - ); + for (const branch of branches) { + const commits = await giteaClient.getCommits(repo.owner, repo.name, undefined, branch.name); + for (const commit of commits) { + if (seenShas.has(commit.sha)) continue; + seenShas.add(commit.sha); - const now = new Date(); - await db.insert(gitCommits).values({ - id: uuid(), - repoName: repo.name, - sha: commit.sha, - authorEmail: commit.commit.author.email, - authorName: commit.commit.author.name, - userId, - message: commit.commit.message, - additions: commit.stats?.additions || 0, - deletions: commit.stats?.deletions || 0, - committedAt: new Date(commit.commit.author.date), - createdAt: now, - updatedAt: now, + const existingCommit = await db.query.gitCommits.findFirst({ + where: eq(gitCommits.sha, commit.sha), }); - recordsProcessed++; + + if (!existingCommit) { + const userId = await resolveAuthor( + commit.commit.author.email, + null + ); + + const now = new Date(); + await db.insert(gitCommits).values({ + id: uuid(), + repoName: repo.name, + sha: commit.sha, + authorEmail: commit.commit.author.email, + authorName: commit.commit.author.name, + userId, + message: commit.commit.message, + additions: commit.stats?.additions || 0, + deletions: commit.stats?.deletions || 0, + committedAt: new Date(commit.commit.author.date), + createdAt: now, + updatedAt: now, + }); + recordsProcessed++; + } } } @@ -143,7 +151,7 @@ export async function syncGitea(): Promise { recordsProcessed++; } - console.info(`[SYNC] Repo ${repo.owner}/${repo.name}: synced`); + console.info(`[SYNC] Repo ${repo.owner}/${repo.name}: synced (${branches.length} branches)`); } catch (repoErr) { const msg = repoErr instanceof Error ? repoErr.message : 'Unknown error'; console.error(`[SYNC] Repo ${repo.owner}/${repo.name} failed:`, msg);