simplify initialization
This commit is contained in:
@@ -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<MainResult> {
|
||||
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<MainResult> {
|
||||
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<Omit<MainContext, "payload" | "mcpServers" | "cliPath" | "apiKey">> {
|
||||
payload: Payload
|
||||
): Promise<Omit<MainContext, "mcpServers" | "cliPath" | "apiKey">> {
|
||||
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,
|
||||
|
||||
+1
-1
@@ -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}`;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+5
-2
@@ -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)");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user