Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/github/api/queries/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export const PR_QUERY = `
baseRefName
headRefName
headRefOid
isCrossRepository
headRepository {
owner {
login
}
name
}
createdAt
updatedAt
lastEditedAt
Expand Down
20 changes: 17 additions & 3 deletions src/github/operations/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,23 @@ export async function setupBranch(
// Validate branch names before use to prevent command injection
validateBranchName(branchName);

// Execute git commands to checkout PR branch (dynamic depth based on PR size)
// Using execFileSync instead of shell template literals for security
execGit(["fetch", "origin", `--depth=${fetchDepth}`, branchName]);
// For cross-repository (fork) PRs, fetch via the pull ref since the
// branch only exists on the fork's remote, not on origin.
if (prData.isCrossRepository) {
console.log(
`PR #${entityNumber} is from a fork, fetching via refs/pull/${entityNumber}/head...`,
);
execGit([
"fetch",
"origin",
`--depth=${fetchDepth}`,
`pull/${entityNumber}/head:${branchName}`,
]);
} else {
// Execute git commands to checkout PR branch (dynamic depth based on PR size)
// Using execFileSync instead of shell template literals for security
execGit(["fetch", "origin", `--depth=${fetchDepth}`, branchName]);
}
execGit(["checkout", branchName, "--"]);

console.log(`Successfully checked out PR branch for PR #${entityNumber}`);
Expand Down
7 changes: 7 additions & 0 deletions src/github/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export type GitHubPullRequest = {
baseRefName: string;
headRefName: string;
headRefOid: string;
isCrossRepository: boolean;
headRepository: {
owner: {
login: string;
};
name: string;
} | null;
createdAt: string;
updatedAt?: string;
lastEditedAt?: string;
Expand Down
2 changes: 2 additions & 0 deletions test/create-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe("generatePrompt", () => {
baseRefName: "main",
headRefName: "feature-branch",
headRefOid: "abc123",
isCrossRepository: false,
headRepository: { owner: { login: "testowner" }, name: "testrepo" },
commits: {
totalCount: 2,
nodes: [
Expand Down
2 changes: 2 additions & 0 deletions test/data-fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,8 @@ describe("fetchGitHubData integration with time filtering", () => {
baseRefName: "main",
headRefName: "feature",
headRefOid: "abc123",
isCrossRepository: false,
headRepository: { owner: { login: "testowner" }, name: "testrepo" },
createdAt: "2024-01-15T10:00:00Z",
updatedAt: "2024-01-15T12:30:00Z", // Edited after trigger
lastEditedAt: "2024-01-15T12:30:00Z", // Edited after trigger
Expand Down
2 changes: 2 additions & 0 deletions test/data-formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe("formatContext", () => {
baseRefName: "main",
headRefName: "feature/test",
headRefOid: "abc123",
isCrossRepository: false,
headRepository: { owner: { login: "testowner" }, name: "testrepo" },
createdAt: "2023-01-01T00:00:00Z",
additions: 50,
deletions: 30,
Expand Down
2 changes: 2 additions & 0 deletions test/pull-request-target.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe("pull_request_target event support", () => {
baseRefName: "main",
headRefName: "feature-branch",
headRefOid: "abc123",
isCrossRepository: false,
headRepository: { owner: { login: "testowner" }, name: "testrepo" },
commits: {
totalCount: 2,
nodes: [
Expand Down