use GITHUB_REPOSITORY for context

This commit is contained in:
ssalbdivad
2025-10-09 17:04:03 -04:00
parent 9459803aaa
commit 3e7122611c
7 changed files with 381 additions and 398 deletions
+6 -12
View File
@@ -86,20 +86,14 @@ export class ClaudeAgent implements Agent {
"bypassPermissions",
];
if (
process.env.GITHUB_INSTALLATION_TOKEN &&
process.env.REPO_OWNER &&
process.env.REPO_NAME
) {
const mcpConfig = createMcpConfig(
process.env.GITHUB_INSTALLATION_TOKEN,
process.env.REPO_OWNER,
process.env.REPO_NAME
);
console.log("📋 MCP Config:", mcpConfig);
args.push("--mcp-config", mcpConfig);
if (!process.env.GITHUB_INSTALLATION_TOKEN) {
throw new Error("GITHUB_INSTALLATION_TOKEN is required for GitHub integration");
}
const mcpConfig = createMcpConfig(process.env.GITHUB_INSTALLATION_TOKEN);
console.log("📋 MCP Config:", mcpConfig);
args.push("--mcp-config", mcpConfig);
const env = {
ANTHROPIC_API_KEY: this.apiKey,
};
+323 -328
View File
File diff suppressed because one or more lines are too long
+1 -7
View File
@@ -3,11 +3,7 @@
*/
const actionPath = process.env.GITHUB_ACTION_PATH || process.cwd();
export function createMcpConfig(
githubInstallationToken: string,
repoOwner: string,
repoName: string
) {
export function createMcpConfig(githubInstallationToken: string) {
return JSON.stringify(
{
mcpServers: {
@@ -16,8 +12,6 @@ export function createMcpConfig(
args: [`${actionPath}/mcp/server.ts`],
env: {
GITHUB_INSTALLATION_TOKEN: githubInstallationToken,
REPO_OWNER: repoOwner,
REPO_NAME: repoName,
},
},
},
+6 -11
View File
@@ -5,15 +5,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
import { Octokit } from "@octokit/rest";
import { type } from "arktype";
import { z } from "zod";
// Get repository information from environment variables
const REPO_OWNER = process.env.REPO_OWNER;
const REPO_NAME = process.env.REPO_NAME;
if (!REPO_OWNER || !REPO_NAME) {
console.error("Error: REPO_OWNER and REPO_NAME environment variables are required");
process.exit(1);
}
import { resolveRepoContext } from "../utils/repo-context.ts";
const server = new McpServer({
name: "Minimal GitHub Issue Comment Server",
@@ -42,13 +34,16 @@ server.tool(
throw new Error("GITHUB_INSTALLATION_TOKEN environment variable is required");
}
// Resolve repository context from environment
const repoContext = resolveRepoContext();
const octokit = new Octokit({
auth: githubInstallationToken,
});
const result = await octokit.rest.issues.createComment({
owner: REPO_OWNER,
repo: REPO_NAME,
owner: repoContext.owner,
repo: repoContext.name,
issue_number: issueNumber,
body: body,
});
+3 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@pullfrog/action",
"version": "0.0.13",
"version": "0.0.14",
"type": "module",
"files": [
"index.js",
@@ -23,7 +23,8 @@
"build:dev": "node esbuild.config.js",
"prepare": "husky",
"play": "node play.ts",
"upDeps": "pnpm up --latest"
"upDeps": "pnpm up --latest",
"createLockfile": "pnpm --ignore-workspace install"
},
"dependencies": {
"@actions/core": "^1.11.1",
+20 -38
View File
@@ -1,5 +1,6 @@
import { createSign } from "node:crypto";
import { config } from "dotenv";
import { resolveRepoContext } from "./repo-context.ts";
config();
@@ -34,26 +35,6 @@ interface RepositoriesResponse {
repositories: Repository[];
}
const validateConfig = (config: GitHubAppConfig): void => {
const { appId, privateKey, repoOwner, repoName } = config;
if (!appId) {
throw new Error("GITHUB_APP_ID environment variable is required");
}
if (!privateKey) {
throw new Error("GITHUB_PRIVATE_KEY environment variable is required");
}
if (!repoOwner || !repoName) {
throw new Error("REPO_OWNER and REPO_NAME environment variables are required");
}
if (!privateKey.includes("BEGIN") || !privateKey.includes("END")) {
throw new Error("GITHUB_PRIVATE_KEY must be in PEM format");
}
};
const base64UrlEncode = (str: string): string => {
return Buffer.from(str)
.toString("base64")
@@ -122,14 +103,15 @@ const githubRequest = async <T>(
return response.json() as T;
};
const checkRepositoryAccess = async (token: string, repoOwner: string, repoName: string): Promise<boolean> => {
const checkRepositoryAccess = async (
token: string,
repoOwner: string,
repoName: string
): Promise<boolean> => {
try {
const response = await githubRequest<RepositoriesResponse>(
"/installation/repositories",
{
headers: { Authorization: `token ${token}` },
}
);
const response = await githubRequest<RepositoriesResponse>("/installation/repositories", {
headers: { Authorization: `token ${token}` },
});
return response.repositories.some(
(repo) => repo.owner.login === repoOwner && repo.name === repoName
@@ -151,7 +133,11 @@ const createInstallationToken = async (jwt: string, installationId: number): Pro
return response.token;
};
const findInstallationId = async (jwt: string, repoOwner: string, repoName: string): Promise<number> => {
const findInstallationId = async (
jwt: string,
repoOwner: string,
repoName: string
): Promise<number> => {
const installations = await githubRequest<Installation[]>("/app/installations", {
headers: { Authorization: `Bearer ${jwt}` },
});
@@ -164,8 +150,7 @@ const findInstallationId = async (jwt: string, repoOwner: string, repoName: stri
if (hasAccess) {
return installation.id;
}
} catch {
}
} catch {}
}
throw new Error(
@@ -174,19 +159,16 @@ const findInstallationId = async (jwt: string, repoOwner: string, repoName: stri
);
};
export const generateInstallationToken = async (
repoOwner?: string,
repoName?: string
): Promise<string> => {
export const generateInstallationToken = async (): Promise<string> => {
const repoContext = resolveRepoContext();
const config: GitHubAppConfig = {
appId: process.env.GITHUB_APP_ID!,
privateKey: process.env.GITHUB_PRIVATE_KEY?.replace(/\\n/g, "\n")!,
repoOwner: repoOwner || process.env.REPO_OWNER || "pullfrogai",
repoName: repoName || process.env.REPO_NAME || "scratch",
repoOwner: repoContext.owner,
repoName: repoContext.name,
};
validateConfig(config);
const jwt = generateJWT(config.appId, config.privateKey);
const installationId = await findInstallationId(jwt, config.repoOwner, config.repoName);
const token = await createInstallationToken(jwt, installationId);
+22
View File
@@ -0,0 +1,22 @@
export interface RepoContext {
owner: string;
name: string;
}
/**
* Resolve repository context from GITHUB_REPOSITORY environment variable.
* Throws if not available.
*/
export function resolveRepoContext(): RepoContext {
const githubRepo = process.env.GITHUB_REPOSITORY;
if (!githubRepo) {
throw new Error('GITHUB_REPOSITORY environment variable is required');
}
const [owner, name] = githubRepo.split('/');
if (!owner || !name) {
throw new Error(`Invalid GITHUB_REPOSITORY format: ${githubRepo}. Expected 'owner/repo'`);
}
return { owner, name };
}