Skip to content

Commit 899869a

Browse files
Copilotalexr00
andcommitted
Limit branch association to 3 most recently used branches
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent da01773 commit 899869a

1 file changed

Lines changed: 57 additions & 60 deletions

File tree

src/github/folderRepositoryManager.ts

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -981,81 +981,78 @@ export class FolderRepositoryManager extends Disposable {
981981
}
982982

983983
try {
984-
const localBranches = (await this.repository.getRefs({ pattern: 'refs/heads/' }))
984+
// Only check the 3 most recently used branches to minimize API calls
985+
const localBranches = (await this.repository.getRefs({
986+
pattern: 'refs/heads/',
987+
sort: 'committerdate',
988+
count: 3
989+
}))
985990
.filter(r => r.name !== undefined)
986991
.map(r => r.name!);
987992

988993
Logger.debug(`Found ${localBranches.length} local branches to check`, this.id);
989994

990-
// Process branches in chunks to avoid overwhelming the system
991-
// Using a smaller chunk size (10) compared to getLocalPullRequests (100) because:
992-
// - This runs on first activation when we don't know how many branches need API calls
993-
// - We want to be conservative to avoid rate limiting
994-
// - The operation is async and non-blocking, so lower throughput is acceptable
995-
const chunkSize = 10;
996995
const associationResults: boolean[] = [];
997996

998-
for (let i = 0; i < localBranches.length; i += chunkSize) {
999-
const chunk = localBranches.slice(i, i + chunkSize);
1000-
const chunkResults = await Promise.all(chunk.map(async branchName => {
1001-
try {
1002-
// Check if this branch already has PR metadata
1003-
const existingMetadata = await PullRequestGitHelper.getMatchingPullRequestMetadataForBranch(
1004-
this.repository,
1005-
branchName,
1006-
);
1007-
1008-
if (existingMetadata) {
1009-
// Branch already has PR metadata, skip
1010-
return false;
1011-
}
1012-
1013-
// Get the branch to check its upstream
1014-
const branch = await this.repository.getBranch(branchName);
1015-
if (!branch.upstream) {
1016-
// No upstream, can't match to a PR
1017-
return false;
1018-
}
997+
// Process all branches (max 3) in parallel
998+
const chunkResults = await Promise.all(localBranches.map(async branchName => {
999+
try {
1000+
// Check if this branch already has PR metadata
1001+
const existingMetadata = await PullRequestGitHelper.getMatchingPullRequestMetadataForBranch(
1002+
this.repository,
1003+
branchName,
1004+
);
10191005

1020-
// Try to find a matching PR on GitHub
1021-
const remoteName = branch.upstream.remote;
1022-
const upstreamBranchName = branch.upstream.name;
1006+
if (existingMetadata) {
1007+
// Branch already has PR metadata, skip
1008+
return false;
1009+
}
10231010

1024-
const githubRepo = githubRepositories.find(
1025-
repo => repo.remote.remoteName === remoteName,
1026-
);
1011+
// Get the branch to check its upstream
1012+
const branch = await this.repository.getBranch(branchName);
1013+
if (!branch.upstream) {
1014+
// No upstream, can't match to a PR
1015+
return false;
1016+
}
10271017

1028-
if (!githubRepo) {
1029-
return false;
1030-
}
1018+
// Try to find a matching PR on GitHub
1019+
const remoteName = branch.upstream.remote;
1020+
const upstreamBranchName = branch.upstream.name;
10311021

1032-
// Get the metadata of the GitHub repository to find owner
1033-
const metadata = await githubRepo.getMetadata();
1034-
if (!metadata?.owner) {
1035-
return false;
1036-
}
1022+
const githubRepo = githubRepositories.find(
1023+
repo => repo.remote.remoteName === remoteName,
1024+
);
10371025

1038-
// Search for a PR with this head branch
1039-
const matchingPR = await githubRepo.getPullRequestForBranch(upstreamBranchName, metadata.owner.login);
1040-
1041-
if (matchingPR) {
1042-
Logger.appendLine(`Found PR #${matchingPR.number} for branch ${branchName}, associating...`, this.id);
1043-
await PullRequestGitHelper.associateBranchWithPullRequest(
1044-
this.repository,
1045-
matchingPR,
1046-
branchName,
1047-
);
1048-
return true;
1049-
}
1026+
if (!githubRepo) {
10501027
return false;
1051-
} catch (e) {
1052-
Logger.debug(`Error checking branch ${branchName}: ${e}`, this.id);
1053-
// Continue with other branches even if one fails
1028+
}
1029+
1030+
// Get the metadata of the GitHub repository to find owner
1031+
const metadata = await githubRepo.getMetadata();
1032+
if (!metadata?.owner) {
10541033
return false;
10551034
}
1056-
}));
1057-
associationResults.push(...chunkResults);
1058-
}
1035+
1036+
// Search for a PR with this head branch
1037+
const matchingPR = await githubRepo.getPullRequestForBranch(upstreamBranchName, metadata.owner.login);
1038+
1039+
if (matchingPR) {
1040+
Logger.appendLine(`Found PR #${matchingPR.number} for branch ${branchName}, associating...`, this.id);
1041+
await PullRequestGitHelper.associateBranchWithPullRequest(
1042+
this.repository,
1043+
matchingPR,
1044+
branchName,
1045+
);
1046+
return true;
1047+
}
1048+
return false;
1049+
} catch (e) {
1050+
Logger.debug(`Error checking branch ${branchName}: ${e}`, this.id);
1051+
// Continue with other branches even if one fails
1052+
return false;
1053+
}
1054+
}));
1055+
associationResults.push(...chunkResults);
10591056

10601057
const associatedCount = associationResults.filter(r => r).length;
10611058
Logger.appendLine(`Branch association complete: ${associatedCount} branches associated with PRs`, this.id);

0 commit comments

Comments
 (0)