mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-08-22 03:18:54 +08:00
fix: fall back to repo default_branch instead of hardcoded "main" (#1143)
* fix: fall back to repo default_branch instead of hardcoded "main" When no explicit base_branch input is provided, the action previously fell back to a hardcoded "main", which fails on repositories whose default branch is named differently (e.g. "master", "develop"). This reads repository.default_branch from the GitHub event payload (populated once in parseGitHubContext) and uses it as the fallback in all three callsites: agent/index.ts, run.ts, and update-comment-link.ts. Explicit env/input precedence is preserved; "main" remains only as a last-resort defensive fallback if the payload somehow lacks the field. * test: drop unused BASE_BRANCH env handling from default_branch test agent/index.ts no longer reads process.env.BASE_BRANCH directly (it now goes through context.inputs.baseBranch which is set on the mock context), so saving/clearing/restoring that env var in the regression test is dead code.
This commit is contained in:
@@ -88,7 +88,7 @@ describe("Agent Mode", () => {
|
||||
expect(result.claudeArgs).toBe("--model claude-sonnet-4 --max-turns 10");
|
||||
expect(result.claudeArgs).not.toContain("--mcp-config");
|
||||
|
||||
// Verify return structure - should use "main" as fallback when no env vars set
|
||||
// Verify return structure - should fall back to repository.default_branch when no env vars set
|
||||
expect(result).toEqual({
|
||||
commentId: undefined,
|
||||
branchInfo: {
|
||||
@@ -108,6 +108,60 @@ describe("Agent Mode", () => {
|
||||
process.env.GITHUB_REF_NAME = originalRefName;
|
||||
});
|
||||
|
||||
test("prepare falls back to repository.default_branch when not 'main'", async () => {
|
||||
const contextWithDevelop = createMockAutomationContext({
|
||||
eventName: "workflow_dispatch",
|
||||
repository: {
|
||||
owner: "test-owner",
|
||||
repo: "test-repo",
|
||||
full_name: "test-owner/test-repo",
|
||||
default_branch: "develop",
|
||||
},
|
||||
});
|
||||
|
||||
// Save and clear env vars that would otherwise override the fallback
|
||||
const originalClaudeBranch = process.env.CLAUDE_BRANCH;
|
||||
const originalHeadRef = process.env.GITHUB_HEAD_REF;
|
||||
const originalRefName = process.env.GITHUB_REF_NAME;
|
||||
delete process.env.CLAUDE_BRANCH;
|
||||
delete process.env.GITHUB_HEAD_REF;
|
||||
delete process.env.GITHUB_REF_NAME;
|
||||
|
||||
const mockOctokit = {
|
||||
rest: {
|
||||
users: {
|
||||
getAuthenticated: mock(() =>
|
||||
Promise.resolve({
|
||||
data: { login: "test-user", id: 12345, type: "User" },
|
||||
}),
|
||||
),
|
||||
getByUsername: mock(() =>
|
||||
Promise.resolve({
|
||||
data: { login: "test-user", id: 12345, type: "User" },
|
||||
}),
|
||||
),
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
|
||||
const result = await prepareAgentMode({
|
||||
context: contextWithDevelop,
|
||||
octokit: mockOctokit,
|
||||
githubToken: "test-token",
|
||||
});
|
||||
|
||||
expect(result.branchInfo.baseBranch).toBe("develop");
|
||||
expect(result.branchInfo.currentBranch).toBe("develop");
|
||||
|
||||
// Restore env vars
|
||||
if (originalClaudeBranch !== undefined)
|
||||
process.env.CLAUDE_BRANCH = originalClaudeBranch;
|
||||
if (originalHeadRef !== undefined)
|
||||
process.env.GITHUB_HEAD_REF = originalHeadRef;
|
||||
if (originalRefName !== undefined)
|
||||
process.env.GITHUB_REF_NAME = originalRefName;
|
||||
});
|
||||
|
||||
test("prepare rejects bot actors without allowed_bots", async () => {
|
||||
const contextWithPrompts = createMockAutomationContext({
|
||||
eventName: "workflow_dispatch",
|
||||
|
||||
Reference in New Issue
Block a user