mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-23 06:54:13 +08:00
refactor: Use shared configureGitAuth function in agent mode
- Update configureGitAuth to accept GitHubContext instead of ParsedGitHubContext - This allows both tag mode and agent mode to use the same function - Removes code duplication and ensures consistent git configuration
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
import { $ } from "bun";
|
||||
import type { ParsedGitHubContext } from "../context";
|
||||
import type { GitHubContext } from "../context";
|
||||
import { GITHUB_SERVER_URL } from "../api/config";
|
||||
|
||||
type GitUser = {
|
||||
@@ -16,7 +16,7 @@ type GitUser = {
|
||||
|
||||
export async function configureGitAuth(
|
||||
githubToken: string,
|
||||
context: ParsedGitHubContext,
|
||||
context: GitHubContext,
|
||||
user: GitUser | null,
|
||||
) {
|
||||
console.log("Configuring git authentication for non-signing mode");
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { Mode, ModeOptions, ModeResult } from "../types";
|
||||
import type { PreparedContext } from "../../create-prompt/types";
|
||||
import { prepareMcpConfig } from "../../mcp/install-mcp-server";
|
||||
import { parseAllowedTools } from "./parse-tools";
|
||||
import { GITHUB_SERVER_URL } from "../../github/api/config";
|
||||
import { configureGitAuth } from "../../github/operations/git-config";
|
||||
|
||||
/**
|
||||
* Agent mode implementation.
|
||||
@@ -47,40 +47,19 @@ export const agentMode: Mode = {
|
||||
githubToken,
|
||||
octokit,
|
||||
}: ModeOptions): Promise<ModeResult> {
|
||||
// Configure git authentication for agent mode
|
||||
// Since agent mode is for automation contexts, we set up git directly
|
||||
// Configure git authentication for agent mode (same as tag mode)
|
||||
if (!context.inputs.useCommitSigning) {
|
||||
try {
|
||||
const { $ } = await import("bun");
|
||||
|
||||
// Get the authenticated user (will be claude[bot] when using Claude App token)
|
||||
const { data: authenticatedUser } =
|
||||
await octokit.rest.users.getAuthenticated();
|
||||
const user = {
|
||||
login: authenticatedUser.login,
|
||||
id: authenticatedUser.id,
|
||||
};
|
||||
|
||||
// Determine the noreply email domain based on GITHUB_SERVER_URL
|
||||
const serverUrl = new URL(GITHUB_SERVER_URL);
|
||||
const noreplyDomain =
|
||||
serverUrl.hostname === "github.com"
|
||||
? "users.noreply.github.com"
|
||||
: `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.email "${authenticatedUser.id}+${authenticatedUser.login}@${noreplyDomain}"`;
|
||||
|
||||
// Remove existing authentication headers (if any)
|
||||
try {
|
||||
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}`);
|
||||
// Use the shared git configuration function
|
||||
await configureGitAuth(githubToken, context, user);
|
||||
} catch (error) {
|
||||
console.error("Failed to configure git authentication:", error);
|
||||
// Continue anyway - git operations may still work with default config
|
||||
|
||||
Reference in New Issue
Block a user