Attempt to make this work

This commit is contained in:
Mark Wylde
2025-05-30 21:20:59 +01:00
parent 01602be052
commit c77bb0e4b3
3 changed files with 67 additions and 27 deletions

View File

@@ -155,10 +155,21 @@ async function run() {
`Branch ${claudeBranch} has same SHA as base, no PR link needed`, `Branch ${claudeBranch} has same SHA as base, no PR link needed`,
); );
} }
} catch (error) { } catch (error: any) {
console.error("Error checking branch in Gitea:", error); console.error("Error checking branch in Gitea:", error);
// If we can't check in Gitea, add PR link to be safe
console.log("Adding PR link as fallback for Gitea"); // Handle 404 specifically - branch doesn't exist
if (error.status === 404) {
console.log(
`Branch ${claudeBranch} does not exist yet - no PR link needed`,
);
// Don't add PR link since branch doesn't exist
prLink = "";
} else {
// For other errors, add PR link to be safe
console.log(
"Adding PR link as fallback for Gitea due to non-404 error",
);
const entityType = context.isPR ? "PR" : "Issue"; const entityType = context.isPR ? "PR" : "Issue";
const prTitle = encodeURIComponent( const prTitle = encodeURIComponent(
`${entityType} #${context.entityNumber}: Changes from Claude`, `${entityType} #${context.entityNumber}: Changes from Claude`,
@@ -169,6 +180,7 @@ async function run() {
const prUrl = `${serverUrl}/${owner}/${repo}/compare/${baseBranch}...${claudeBranch}?quick_pull=1&title=${prTitle}&body=${prBody}`; const prUrl = `${serverUrl}/${owner}/${repo}/compare/${baseBranch}...${claudeBranch}?quick_pull=1&title=${prTitle}&body=${prBody}`;
prLink = `\n[Create a PR](${prUrl})`; prLink = `\n[Create a PR](${prUrl})`;
} }
}
} else { } else {
// GitHub environment - use the comparison API // GitHub environment - use the comparison API
try { try {

View File

@@ -54,12 +54,23 @@ export async function checkAndDeleteEmptyBranch(
); );
shouldDeleteBranch = true; shouldDeleteBranch = true;
} }
} catch (error) { } catch (error: any) {
console.error("Error checking branch in Gitea:", error); console.error("Error checking branch in Gitea:", error);
// If we can't check, assume the branch has commits to be safe
// Handle 404 specifically - branch doesn't exist
if (error.status === 404) {
console.log(
`Branch ${claudeBranch} does not exist yet - this is normal during workflow`,
);
// Don't add branch link since branch doesn't exist
branchLink = "";
} else {
// For other errors, assume the branch has commits to be safe
console.log("Assuming branch exists due to non-404 error");
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${claudeBranch}`; const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${claudeBranch}`;
branchLink = `\n[View branch](${branchUrl})`; branchLink = `\n[View branch](${branchUrl})`;
} }
}
} else { } else {
// GitHub environment - use the comparison API // GitHub environment - use the comparison API
try { try {

View File

@@ -121,21 +121,39 @@ export async function setupBranch(
console.log(`Current SHA: ${currentSHA}`); console.log(`Current SHA: ${currentSHA}`);
// Check if we're in a Gitea environment // Try to create branch using the appropriate method for each platform
const isGitea = const isGitea =
process.env.GITHUB_API_URL && process.env.GITHUB_API_URL &&
!process.env.GITHUB_API_URL.includes("api.github.com"); !process.env.GITHUB_API_URL.includes("api.github.com");
if (isGitea) { if (isGitea) {
// Gitea doesn't reliably support git.createRef, skip it // Gitea supports POST /repos/{owner}/{repo}/branches
console.log( console.log(
`Detected Gitea environment, skipping git.createRef for branch: ${newBranch}`, `Detected Gitea environment, using branches API for: ${newBranch}`,
); );
try {
// Use the raw Gitea API since Octokit might not have the createBranch method
await octokits.rest.request("POST /repos/{owner}/{repo}/branches", {
owner,
repo,
new_branch_name: newBranch,
old_branch_name: sourceBranch,
});
console.log(
`Successfully created branch via Gitea branches API: ${newBranch}`,
);
} catch (createBranchError: any) {
console.log(
`Gitea branch creation failed: ${createBranchError.message}`,
);
console.log(`Error status: ${createBranchError.status}`);
console.log( console.log(
`Branch ${newBranch} will be created when files are pushed via MCP server`, `Branch ${newBranch} will be created when files are pushed via MCP server`,
); );
}
} else { } else {
// GitHub environment - try to create branch via API // GitHub environment - use git.createRef
try { try {
await octokits.rest.git.createRef({ await octokits.rest.git.createRef({
owner, owner,
@@ -144,12 +162,11 @@ export async function setupBranch(
sha: currentSHA, sha: currentSHA,
}); });
console.log(`Successfully created branch via API: ${newBranch}`);
} catch (createRefError: any) {
// If creation fails on GitHub, log but continue
console.log( console.log(
`git createRef failed on GitHub: ${createRefError.message}`, `Successfully created branch via GitHub git.createRef: ${newBranch}`,
); );
} catch (createRefError: any) {
console.log(`GitHub git.createRef failed: ${createRefError.message}`);
console.log(`Error status: ${createRefError.status}`); console.log(`Error status: ${createRefError.status}`);
console.log( console.log(
`Branch ${newBranch} will be created when files are pushed`, `Branch ${newBranch} will be created when files are pushed`,