feat: 同步全部分支的 Git 提交,不再仅限默认分支
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 46m54s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 46m54s
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
4fa69ca2bc
commit
7cd8bc1b9b
@ -58,12 +58,22 @@ export async function getRepos(): Promise<GiteaRepo[]> {
|
||||
return Array.isArray(data) ? data : [];
|
||||
}
|
||||
|
||||
export async function getCommits(owner: string, repo: string, since?: string): Promise<GiteaCommit[]> {
|
||||
export interface GiteaBranch {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export async function getBranches(owner: string, repo: string): Promise<GiteaBranch[]> {
|
||||
const data = await giteaGet<GiteaBranch[]>(`/repos/${owner}/${repo}/branches?limit=50`);
|
||||
return Array.isArray(data) ? data : [];
|
||||
}
|
||||
|
||||
export async function getCommits(owner: string, repo: string, since?: string, branch?: string): Promise<GiteaCommit[]> {
|
||||
// 分页拉取所有 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<GiteaCommit[]>(path);
|
||||
if (!Array.isArray(data) || data.length === 0) break;
|
||||
all.push(...data);
|
||||
|
||||
@ -70,35 +70,43 @@ export async function syncGitea(): Promise<void> {
|
||||
|
||||
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<string>();
|
||||
|
||||
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<void> {
|
||||
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user