This commit is contained in:
Ashwin Bhat
2025-07-15 15:51:39 -07:00
parent a9d9ad3612
commit 32a6163092

View File

@@ -8,6 +8,7 @@ import { join } from "path";
import fetch from "node-fetch"; import fetch from "node-fetch";
import { GITHUB_API_URL } from "../github/api/config"; import { GITHUB_API_URL } from "../github/api/config";
import { retryWithBackoff } from "../utils/retry"; import { retryWithBackoff } from "../utils/retry";
import { Octokit } from "@octokit/rest";
type GitHubRef = { type GitHubRef = {
object: { object: {
@@ -59,6 +60,12 @@ async function getOrCreateBranchRef(
branch: string, branch: string,
githubToken: string, githubToken: string,
): Promise<string> { ): Promise<string> {
// Create Octokit instance
const octokit = new Octokit({
auth: githubToken,
baseUrl: GITHUB_API_URL,
});
// Try to get the branch reference // Try to get the branch reference
const refUrl = `${GITHUB_API_URL}/repos/${owner}/${repo}/git/refs/heads/${branch}`; const refUrl = `${GITHUB_API_URL}/repos/${owner}/${repo}/git/refs/heads/${branch}`;
const refResponse = await fetch(refUrl, { const refResponse = await fetch(refUrl, {
@@ -78,9 +85,6 @@ async function getOrCreateBranchRef(
throw new Error(`Failed to get branch reference: ${refResponse.status}`); throw new Error(`Failed to get branch reference: ${refResponse.status}`);
} }
// Branch doesn't exist, need to create it
console.log(`Branch ${branch} does not exist, creating it...`);
// Get base branch from environment or determine it // Get base branch from environment or determine it
const baseBranch = process.env.BASE_BRANCH || "main"; const baseBranch = process.env.BASE_BRANCH || "main";
@@ -139,30 +143,19 @@ async function getOrCreateBranchRef(
baseSha = baseRefData.object.sha; baseSha = baseRefData.object.sha;
} }
// Create the new branch // Create the new branch using Octokit
const createRefUrl = `${GITHUB_API_URL}/repos/${owner}/${repo}/git/refs`; try {
const createRefResponse = await fetch(createRefUrl, { await octokit.rest.git.createRef({
method: "POST", owner,
headers: { repo,
Accept: "application/vnd.github+json",
Authorization: `Bearer ${githubToken}`,
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "application/json",
},
body: JSON.stringify({
ref: `refs/heads/${branch}`, ref: `refs/heads/${branch}`,
sha: baseSha, sha: baseSha,
}), });
}); } catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (!createRefResponse.ok) { throw new Error(`Failed to create branch: ${errorMessage}`);
const errorText = await createRefResponse.text();
throw new Error(
`Failed to create branch: ${createRefResponse.status} - ${errorText}`,
);
} }
console.log(`Successfully created branch ${branch}`);
return baseSha; return baseSha;
} }
@@ -569,7 +562,6 @@ server.tool(
// Only retry on 403 errors - these are the intermittent failures we're targeting // Only retry on 403 errors - these are the intermittent failures we're targeting
if (updateRefResponse.status === 403) { if (updateRefResponse.status === 403) {
console.log("Received 403 error, will retry...");
throw error; throw error;
} }