diff --git a/src/entrypoints/update-comment-link.ts b/src/entrypoints/update-comment-link.ts index 5eb4bbb..3477c10 100644 --- a/src/entrypoints/update-comment-link.ts +++ b/src/entrypoints/update-comment-link.ts @@ -155,19 +155,31 @@ async function run() { `Branch ${claudeBranch} has same SHA as base, no PR link needed`, ); } - } catch (error) { + } catch (error: any) { 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"); - const entityType = context.isPR ? "PR" : "Issue"; - const prTitle = encodeURIComponent( - `${entityType} #${context.entityNumber}: Changes from Claude`, - ); - const prBody = encodeURIComponent( - `This PR addresses ${entityType.toLowerCase()} #${context.entityNumber}\n\nGenerated with [Claude Code](https://claude.ai/code)`, - ); - const prUrl = `${serverUrl}/${owner}/${repo}/compare/${baseBranch}...${claudeBranch}?quick_pull=1&title=${prTitle}&body=${prBody}`; - prLink = `\n[Create a PR](${prUrl})`; + + // 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 prTitle = encodeURIComponent( + `${entityType} #${context.entityNumber}: Changes from Claude`, + ); + const prBody = encodeURIComponent( + `This PR addresses ${entityType.toLowerCase()} #${context.entityNumber}\n\nGenerated with [Claude Code](https://claude.ai/code)`, + ); + const prUrl = `${serverUrl}/${owner}/${repo}/compare/${baseBranch}...${claudeBranch}?quick_pull=1&title=${prTitle}&body=${prBody}`; + prLink = `\n[Create a PR](${prUrl})`; + } } } else { // GitHub environment - use the comparison API diff --git a/src/github/operations/branch-cleanup.ts b/src/github/operations/branch-cleanup.ts index c52e9a2..69e36a5 100644 --- a/src/github/operations/branch-cleanup.ts +++ b/src/github/operations/branch-cleanup.ts @@ -54,11 +54,22 @@ export async function checkAndDeleteEmptyBranch( ); shouldDeleteBranch = true; } - } catch (error) { + } catch (error: any) { console.error("Error checking branch in Gitea:", error); - // If we can't check, assume the branch has commits to be safe - const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${claudeBranch}`; - branchLink = `\n[View branch](${branchUrl})`; + + // 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}`; + branchLink = `\n[View branch](${branchUrl})`; + } } } else { // GitHub environment - use the comparison API diff --git a/src/github/operations/branch.ts b/src/github/operations/branch.ts index 029295a..4cd2df1 100644 --- a/src/github/operations/branch.ts +++ b/src/github/operations/branch.ts @@ -121,21 +121,39 @@ export async function setupBranch( 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 = process.env.GITHUB_API_URL && !process.env.GITHUB_API_URL.includes("api.github.com"); if (isGitea) { - // Gitea doesn't reliably support git.createRef, skip it + // Gitea supports POST /repos/{owner}/{repo}/branches console.log( - `Detected Gitea environment, skipping git.createRef for branch: ${newBranch}`, - ); - console.log( - `Branch ${newBranch} will be created when files are pushed via MCP server`, + `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( + `Branch ${newBranch} will be created when files are pushed via MCP server`, + ); + } } else { - // GitHub environment - try to create branch via API + // GitHub environment - use git.createRef try { await octokits.rest.git.createRef({ owner, @@ -144,12 +162,11 @@ export async function setupBranch( sha: currentSHA, }); - console.log(`Successfully created branch via API: ${newBranch}`); - } catch (createRefError: any) { - // If creation fails on GitHub, log but continue 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( `Branch ${newBranch} will be created when files are pushed`,