mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-23 06:54:13 +08:00
fix: Align agent mode git config with existing patterns
- Use GITHUB_SERVER_URL from config module consistently - Remove existing headers before setting new ones - Use remote URL with embedded token like git-config.ts does - Match the existing git authentication pattern in the codebase
This commit is contained in:
@@ -4,6 +4,7 @@ import type { Mode, ModeOptions, ModeResult } from "../types";
|
|||||||
import type { PreparedContext } from "../../create-prompt/types";
|
import type { PreparedContext } from "../../create-prompt/types";
|
||||||
import { prepareMcpConfig } from "../../mcp/install-mcp-server";
|
import { prepareMcpConfig } from "../../mcp/install-mcp-server";
|
||||||
import { parseAllowedTools } from "./parse-tools";
|
import { parseAllowedTools } from "./parse-tools";
|
||||||
|
import { GITHUB_SERVER_URL } from "../../github/api/config";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Agent mode implementation.
|
* Agent mode implementation.
|
||||||
@@ -47,29 +48,37 @@ export const agentMode: Mode = {
|
|||||||
octokit,
|
octokit,
|
||||||
}: ModeOptions): Promise<ModeResult> {
|
}: ModeOptions): Promise<ModeResult> {
|
||||||
// Configure git authentication for agent mode
|
// Configure git authentication for agent mode
|
||||||
// Since agent mode is for automation contexts, we need to set up git differently
|
// Since agent mode is for automation contexts, we set up git directly
|
||||||
if (!context.inputs.useCommitSigning) {
|
if (!context.inputs.useCommitSigning) {
|
||||||
try {
|
try {
|
||||||
|
const { $ } = await import("bun");
|
||||||
|
|
||||||
// Get the authenticated user (will be claude[bot] when using Claude App token)
|
// Get the authenticated user (will be claude[bot] when using Claude App token)
|
||||||
const { data: authenticatedUser } =
|
const { data: authenticatedUser } =
|
||||||
await octokit.rest.users.getAuthenticated();
|
await octokit.rest.users.getAuthenticated();
|
||||||
|
|
||||||
// Set up git config directly for automation contexts
|
// Determine the noreply email domain based on GITHUB_SERVER_URL
|
||||||
const { $ } = await import("bun");
|
const serverUrl = new URL(GITHUB_SERVER_URL);
|
||||||
const serverUrl = new URL(
|
|
||||||
process.env.GITHUB_SERVER_URL || "https://github.com",
|
|
||||||
);
|
|
||||||
const noreplyDomain =
|
const noreplyDomain =
|
||||||
serverUrl.hostname === "github.com"
|
serverUrl.hostname === "github.com"
|
||||||
? "users.noreply.github.com"
|
? "users.noreply.github.com"
|
||||||
: `users.noreply.${serverUrl.hostname}`;
|
: `users.noreply.${serverUrl.hostname}`;
|
||||||
|
|
||||||
|
// Configure git user
|
||||||
|
console.log(`Setting git user as ${authenticatedUser.login}...`);
|
||||||
await $`git config user.name "${authenticatedUser.login}"`;
|
await $`git config user.name "${authenticatedUser.login}"`;
|
||||||
await $`git config user.email "${authenticatedUser.id}+${authenticatedUser.login}@${noreplyDomain}"`;
|
await $`git config user.email "${authenticatedUser.id}+${authenticatedUser.login}@${noreplyDomain}"`;
|
||||||
|
|
||||||
// Set up token authentication
|
// Remove existing authentication headers (if any)
|
||||||
const authHeader = `Authorization: token ${githubToken}`;
|
try {
|
||||||
await $`git config http.${serverUrl.origin}/.extraheader "${authHeader}"`;
|
await $`git config --unset-all http.${GITHUB_SERVER_URL}/.extraheader`;
|
||||||
|
} catch (e) {
|
||||||
|
// No existing headers to remove
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the remote URL to include the token for authentication
|
||||||
|
const remoteUrl = `https://x-access-token:${githubToken}@${serverUrl.host}/${context.repository.owner}/${context.repository.repo}.git`;
|
||||||
|
await $`git remote set-url origin ${remoteUrl}`;
|
||||||
|
|
||||||
console.log(`✓ Configured git as ${authenticatedUser.login}`);
|
console.log(`✓ Configured git as ${authenticatedUser.login}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user