mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-23 23:14:13 +08:00
* feat: add base_branch input to specify source branch for new Claude branches - Add base_branch input parameter to action.yml allowing users to specify which branch to use as source - Update setupBranch function to accept and use the base branch parameter - Defaults to repository default branch if no base branch is specified - Addresses issue #62 for better branch control Co-authored-by: ashwin-ant <ashwin-ant@users.noreply.github.com> * perf: optimize setupBranch to avoid unnecessary default branch fetch Only fetch repository default branch when actually needed: - Skip initial fetch when baseBranch is provided - Fetch default branch at end only for return value and GitHub Actions output - Eliminates unnecessary API call when users specify base branch Co-authored-by: ashwin-ant <ashwin-ant@users.noreply.github.com> * fix: properly handle base branch throughout the action workflow - Fix TypeScript error where defaultBranch was used before being assigned - Replace DEFAULT_BRANCH with BASE_BRANCH in subsequent workflow steps - Update PR creation and branch comparison to use the actual base branch - Ensure custom base_branch input is respected in all operations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * refactor: move BASE_BRANCH env reading into parseGitHubContext - Move BASE_BRANCH environment variable reading into parseGitHubContext for consistency - Update setupBranch to use context.inputs.baseBranch instead of process.env - Fix test descriptions to correctly reference BASE_BRANCH instead of DEFAULT_BRANCH - Update test environment setup to use BASE_BRANCH 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: ashwin-ant <ashwin-ant@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import type { Octokits } from "../api/client";
|
|
import { GITHUB_SERVER_URL } from "../api/config";
|
|
|
|
export async function checkAndDeleteEmptyBranch(
|
|
octokit: Octokits,
|
|
owner: string,
|
|
repo: string,
|
|
claudeBranch: string | undefined,
|
|
baseBranch: string,
|
|
): Promise<{ shouldDeleteBranch: boolean; branchLink: string }> {
|
|
let branchLink = "";
|
|
let shouldDeleteBranch = false;
|
|
|
|
if (claudeBranch) {
|
|
// Check if Claude made any commits to the branch
|
|
try {
|
|
const { data: comparison } =
|
|
await octokit.rest.repos.compareCommitsWithBasehead({
|
|
owner,
|
|
repo,
|
|
basehead: `${baseBranch}...${claudeBranch}`,
|
|
});
|
|
|
|
// If there are no commits, mark branch for deletion
|
|
if (comparison.total_commits === 0) {
|
|
console.log(
|
|
`Branch ${claudeBranch} has no commits from Claude, will delete it`,
|
|
);
|
|
shouldDeleteBranch = true;
|
|
} else {
|
|
// Only add branch link if there are commits
|
|
const branchUrl = `${GITHUB_SERVER_URL}/${owner}/${repo}/tree/${claudeBranch}`;
|
|
branchLink = `\n[View branch](${branchUrl})`;
|
|
}
|
|
} catch (error) {
|
|
console.error("Error checking for commits on Claude branch:", 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})`;
|
|
}
|
|
}
|
|
|
|
// Delete the branch if it has no commits
|
|
if (shouldDeleteBranch && claudeBranch) {
|
|
try {
|
|
await octokit.rest.git.deleteRef({
|
|
owner,
|
|
repo,
|
|
ref: `heads/${claudeBranch}`,
|
|
});
|
|
console.log(`✅ Deleted empty branch: ${claudeBranch}`);
|
|
} catch (deleteError) {
|
|
console.error(`Failed to delete branch ${claudeBranch}:`, deleteError);
|
|
// Continue even if deletion fails
|
|
}
|
|
}
|
|
|
|
return { shouldDeleteBranch, branchLink };
|
|
}
|