simplify initialization
This commit is contained in:
@@ -17,7 +17,6 @@ import { log } from "./utils/cli.ts";
|
|||||||
import {
|
import {
|
||||||
parseRepoContext,
|
parseRepoContext,
|
||||||
type RepoContext,
|
type RepoContext,
|
||||||
revokeInstallationToken,
|
|
||||||
setupGitHubInstallationToken,
|
setupGitHubInstallationToken,
|
||||||
} from "./utils/github.ts";
|
} from "./utils/github.ts";
|
||||||
import { setupGitAuth, setupGitBranch, setupGitConfig } from "./utils/setup.ts";
|
import { setupGitAuth, setupGitBranch, setupGitConfig } from "./utils/setup.ts";
|
||||||
@@ -48,34 +47,16 @@ export interface MainResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function main(inputs: Inputs): Promise<MainResult> {
|
export async function main(inputs: Inputs): Promise<MainResult> {
|
||||||
let githubInstallationToken: string | undefined;
|
|
||||||
let pollInterval: NodeJS.Timeout | null = null;
|
let pollInterval: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// parse payload early to extract agent
|
// parse payload early to extract agent
|
||||||
const payload = parsePayload(inputs);
|
const payload = parsePayload(inputs);
|
||||||
|
|
||||||
// resolve agent before initializing context
|
const partialCtx = await initializeContext(inputs, payload);
|
||||||
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 ctx = partialCtx as MainContext;
|
const ctx = partialCtx as MainContext;
|
||||||
ctx.payload = payload;
|
|
||||||
|
|
||||||
setupGitAuth(ctx.githubInstallationToken, ctx.repoContext);
|
setupGitAuth(ctx);
|
||||||
await setupTempDirectory(ctx);
|
await setupTempDirectory(ctx);
|
||||||
setupMcpLogPolling(ctx);
|
setupMcpLogPolling(ctx);
|
||||||
pollInterval = ctx.pollInterval;
|
pollInterval = ctx.pollInterval;
|
||||||
@@ -99,9 +80,6 @@ export async function main(inputs: Inputs): Promise<MainResult> {
|
|||||||
if (pollInterval) {
|
if (pollInterval) {
|
||||||
clearInterval(pollInterval);
|
clearInterval(pollInterval);
|
||||||
}
|
}
|
||||||
if (githubInstallationToken) {
|
|
||||||
await revokeInstallationToken(githubInstallationToken);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,21 +164,31 @@ interface MainContext {
|
|||||||
|
|
||||||
async function initializeContext(
|
async function initializeContext(
|
||||||
inputs: Inputs,
|
inputs: Inputs,
|
||||||
agentName: AgentNameType,
|
payload: Payload
|
||||||
agent: (typeof agents)[AgentNameType],
|
): Promise<Omit<MainContext, "mcpServers" | "cliPath" | "apiKey">> {
|
||||||
githubInstallationToken: string,
|
|
||||||
repoContext: RepoContext
|
|
||||||
): Promise<Omit<MainContext, "payload" | "mcpServers" | "cliPath" | "apiKey">> {
|
|
||||||
log.info(`🐸 Running pullfrog/action@${packageJson.version}...`);
|
log.info(`🐸 Running pullfrog/action@${packageJson.version}...`);
|
||||||
Inputs.assert(inputs);
|
Inputs.assert(inputs);
|
||||||
setupGitConfig();
|
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 {
|
return {
|
||||||
inputs,
|
inputs,
|
||||||
githubInstallationToken,
|
githubInstallationToken,
|
||||||
repoContext,
|
repoContext,
|
||||||
agentName,
|
agentName,
|
||||||
agent,
|
agent,
|
||||||
|
payload: resolvedPayload,
|
||||||
sharedTempDir: "",
|
sharedTempDir: "",
|
||||||
mcpLogPath: "",
|
mcpLogPath: "",
|
||||||
pollInterval: null,
|
pollInterval: null,
|
||||||
|
|||||||
+1
-1
@@ -15,7 +15,7 @@ function buildCommentFooter(payload: Payload): string {
|
|||||||
const agentDisplayName = agentInfo?.displayName || "Unknown Agent";
|
const agentDisplayName = agentInfo?.displayName || "Unknown Agent";
|
||||||
const agentUrl = agentInfo?.url || "https://pullfrog.ai";
|
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
|
const workflowRunUrl = runId
|
||||||
? `https://github.com/${repoContext.owner}/${repoContext.name}/actions/runs/${runId}`
|
? `https://github.com/${repoContext.owner}/${repoContext.name}/actions/runs/${runId}`
|
||||||
: `https://github.com/${repoContext.owner}/${repoContext.name}`;
|
: `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)
|
// 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) {
|
if (process.env.GITHUB_RUN_ID) {
|
||||||
env.GITHUB_RUN_ID = 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
|
* Setup git authentication using GitHub installation token
|
||||||
* Always uses the installation token, scoped to the current repo only
|
* 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();
|
const repoDir = process.cwd();
|
||||||
|
|
||||||
log.info("🔐 Setting up git authentication...");
|
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
|
// Update remote URL to embed the token
|
||||||
// This is scoped to the repo's .git/config, not the user's global config
|
// 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 });
|
$("git", ["remote", "set-url", "origin", remoteUrl], { cwd: repoDir });
|
||||||
log.info("✓ Updated remote URL with authentication token (scoped to repo)");
|
log.info("✓ Updated remote URL with authentication token (scoped to repo)");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user