diff --git a/main.ts b/main.ts index b3e3c87..b952349 100644 --- a/main.ts +++ b/main.ts @@ -17,7 +17,6 @@ import { log } from "./utils/cli.ts"; import { parseRepoContext, type RepoContext, - revokeInstallationToken, setupGitHubInstallationToken, } from "./utils/github.ts"; import { setupGitAuth, setupGitBranch, setupGitConfig } from "./utils/setup.ts"; @@ -48,34 +47,16 @@ export interface MainResult { } export async function main(inputs: Inputs): Promise { - let githubInstallationToken: string | undefined; let pollInterval: NodeJS.Timeout | null = null; try { // parse payload early to extract agent const payload = parsePayload(inputs); - // resolve agent before initializing context - githubInstallationToken = await setupGitHubInstallationToken(); - const repoContext = parseRepoContext(); - const { agentName, agent } = await resolveAgent( - inputs, - payload, - githubInstallationToken, - repoContext - ); - - const partialCtx = await initializeContext( - inputs, - agentName, - agent, - githubInstallationToken, - repoContext - ); + const partialCtx = await initializeContext(inputs, payload); const ctx = partialCtx as MainContext; - ctx.payload = payload; - setupGitAuth(ctx.githubInstallationToken, ctx.repoContext); + setupGitAuth(ctx); await setupTempDirectory(ctx); setupMcpLogPolling(ctx); pollInterval = ctx.pollInterval; @@ -99,9 +80,6 @@ export async function main(inputs: Inputs): Promise { if (pollInterval) { clearInterval(pollInterval); } - if (githubInstallationToken) { - await revokeInstallationToken(githubInstallationToken); - } } } @@ -186,21 +164,31 @@ interface MainContext { async function initializeContext( inputs: Inputs, - agentName: AgentNameType, - agent: (typeof agents)[AgentNameType], - githubInstallationToken: string, - repoContext: RepoContext -): Promise> { + payload: Payload +): Promise> { log.info(`🐸 Running pullfrog/action@${packageJson.version}...`); Inputs.assert(inputs); setupGitConfig(); + const githubInstallationToken = await setupGitHubInstallationToken(); + const repoContext = parseRepoContext(); + + // resolve agent and update payload with resolved agent name + const { agentName, agent } = await resolveAgent( + inputs, + payload, + githubInstallationToken, + repoContext + ); + const resolvedPayload = { ...payload, agent: agentName }; + return { inputs, githubInstallationToken, repoContext, agentName, agent, + payload: resolvedPayload, sharedTempDir: "", mcpLogPath: "", pollInterval: null, diff --git a/mcp/comment.ts b/mcp/comment.ts index 3cc630e..70d8618 100644 --- a/mcp/comment.ts +++ b/mcp/comment.ts @@ -15,7 +15,7 @@ function buildCommentFooter(payload: Payload): string { const agentDisplayName = agentInfo?.displayName || "Unknown Agent"; const agentUrl = agentInfo?.url || "https://pullfrog.ai"; - // build workflow run URL + // build workflow run URL: https://github.com/{owner}/{repo}/actions/runs/{runId} const workflowRunUrl = runId ? `https://github.com/${repoContext.owner}/${repoContext.name}/actions/runs/${runId}` : `https://github.com/${repoContext.owner}/${repoContext.name}`; diff --git a/mcp/config.ts b/mcp/config.ts index 374bb39..e4d5785 100644 --- a/mcp/config.ts +++ b/mcp/config.ts @@ -34,6 +34,7 @@ export function createMcpConfigs( }; // pass through GITHUB_RUN_ID if available (automatically set in GitHub Actions) + // this is used to build the "View workflow run" link in comments if (process.env.GITHUB_RUN_ID) { env.GITHUB_RUN_ID = process.env.GITHUB_RUN_ID; } diff --git a/utils/setup.ts b/utils/setup.ts index 843a5c6..dd64b32 100644 --- a/utils/setup.ts +++ b/utils/setup.ts @@ -72,7 +72,10 @@ export function setupGitConfig(): void { * Setup git authentication using GitHub installation token * Always uses the installation token, scoped to the current repo only */ -export function setupGitAuth(githubToken: string, repoContext: RepoContext): void { +export function setupGitAuth(ctx: { + githubInstallationToken: string; + repoContext: RepoContext; +}): void { const repoDir = process.cwd(); log.info("🔐 Setting up git authentication..."); @@ -91,7 +94,7 @@ export function setupGitAuth(githubToken: string, repoContext: RepoContext): voi // Update remote URL to embed the token // This is scoped to the repo's .git/config, not the user's global config - const remoteUrl = `https://x-access-token:${githubToken}@github.com/${repoContext.owner}/${repoContext.name}.git`; + const remoteUrl = `https://x-access-token:${ctx.githubInstallationToken}@github.com/${ctx.repoContext.owner}/${ctx.repoContext.name}.git`; $("git", ["remote", "set-url", "origin", remoteUrl], { cwd: repoDir }); log.info("✓ Updated remote URL with authentication token (scoped to repo)"); }