in src/github/operations/branch.ts [124:231]
async function prepareWorkingBranchForJunie(context: JunieExecutionContext, octokit: Octokits): Promise<BranchInfo> {
let baseBranch = context.inputs.baseBranch || context.payload.repository.default_branch
let prBaseBranch: string | undefined;
const entityNumber = context.entityNumber;
const isPR = context.isPR;
const createNewBranchForPR = context.inputs.createNewBranchForPR;
const fetchDepth = context.inputs.resolveConflicts || isReviewOrCommentHasResolveConflictsTrigger(context) ? undefined : 20
if (isPR && entityNumber) {
let sourceBranch: string
let state: string;
let prAuthor: string;
if (isPullRequestEvent(context)
|| isPullRequestReviewEvent(context)
|| isPullRequestReviewCommentEvent(context)) {
baseBranch = context.payload.pull_request.base.ref;
sourceBranch = context.payload.pull_request.head.ref;
state = context.payload.pull_request.state;
prAuthor = context.payload.pull_request.user.login;
} else {
try {
const data = (await octokit.rest.pulls.get({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: entityNumber,
})).data;
baseBranch = data.base.ref;
sourceBranch = data.head.ref
state = data.state;
prAuthor = data.user.login;
} catch (error) {
const repoFullName = `${context.payload.repository.owner.login}/${context.payload.repository.name}`;
throw new Error(
`❌ Failed to fetch PR #${entityNumber} information from ${repoFullName}. ` +
`This could be due to:\n` +
`• PR #${entityNumber} does not exist\n` +
`• Insufficient token permissions (needs 'repo' or 'pull_requests:read' scope)\n` +
`• GitHub API rate limits\n` +
`Original error: ${error instanceof Error ? error.message : String(error)}`
);
}
}
console.log(`Base branch: ${baseBranch}`);
console.log(`Target branch: ${sourceBranch}`);
const useExistingBranch = shouldUseExistingPRBranch(
context.inputs.silentMode,
createNewBranchForPR,
context.actor,
prAuthor,
context.tokenOwner.login,
state
);
await ensureBranchHistory(baseBranch, fetchDepth);
await ensureBranchHistory(sourceBranch, fetchDepth);
if (useExistingBranch) {
try {
await $`git checkout -B ${sourceBranch} origin/${sourceBranch}`;
console.log(`✓ Successfully checked out PR branch for PR #${entityNumber}`);
return {
baseBranch: baseBranch,
workingBranch: sourceBranch!,
isNewBranch: false,
};
} catch (error) {
throw new Error(
`❌ Failed to checkout existing PR branch "${sourceBranch}" for PR #${entityNumber}. ` +
`This could be due to:\n` +
`• Branch "${sourceBranch}" does not exist or was deleted\n` +
`• Insufficient permissions to fetch from the repository\n` +
`• Network connectivity issues\n` +
`• Git authentication problems\n` +
`Original error: ${error instanceof Error ? error.message : String(error)}`
);
}
} else {
console.log(`Creating new branch for PR #${entityNumber} based on ${sourceBranch}`);
prBaseBranch = baseBranch;
baseBranch = sourceBranch;
}
}
if (isPushEvent(context)) {
baseBranch = context.payload.ref.replace("refs/heads/", "");
console.log(`Push event detected, base branch: ${baseBranch}`);
}
if (!context.inputs.silentMode) {
const entityType = isPR ? "pr" : entityNumber ? "issue" : "run";
const branchName = `${WORKING_BRANCH_PREFIX}${entityType}${entityNumber ? `-${entityNumber}` : ""}-${context.runId}`;
return await createNewBranch(baseBranch, branchName, prBaseBranch)
}
await $`git checkout -B ${baseBranch} origin/${baseBranch}`;
return {
baseBranch: baseBranch,
workingBranch: baseBranch,
isNewBranch: false,
prBaseBranch
}
}