Skip to content

Commit 0ddc540

Browse files
committed
abstract installation id away from tools, update review events (now runs on draft -> open pr, DOESNT run on all new PR commits)
1 parent 55546d0 commit 0ddc540

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

src/github/client.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,32 @@ export class GitHubClient {
99

1010
constructor(config: Config, installationId?: number) {
1111
this.config = config;
12-
this.installationId = installationId;
12+
13+
// If no installationId provided, try to get from environment
14+
if (installationId === undefined) {
15+
const envId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0', 10);
16+
this.installationId = envId || undefined;
17+
} else {
18+
this.installationId = installationId;
19+
}
20+
21+
// Validate that we have some form of authentication
22+
if (!this.installationId && !config.github.token) {
23+
throw new Error('Missing authentication: set GITHUB_INSTALLATION_ID env var or github.token in config.yml');
24+
}
25+
1326
const githubConfig = config.github;
14-
this.baseUrl = githubConfig.base_url.replace(/\/$/, '');
27+
this.baseUrl = (githubConfig.base_url ?? 'https://api.github.com').replace(/\/$/, '');
1528
}
1629

1730
static forInstallation(config: Config, installationId: number): GitHubClient {
1831
return new GitHubClient(config, installationId);
1932
}
2033

34+
static fromEnv(config: Config): GitHubClient {
35+
return new GitHubClient(config);
36+
}
37+
2138
private async getAuthHeaders(): Promise<Record<string, string>> {
2239
const headers = {
2340
'Accept': 'application/vnd.github.v3+json',

src/mcp/tools/get_pr_comments.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ export async function getPRComments(
1919
try {
2020
const { owner, repo, pr_number } = args;
2121

22-
// Get installation ID from environment
23-
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0', 10);
24-
if (!installationId) {
25-
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
26-
}
27-
28-
const githubClient = GitHubClient.forInstallation(config, installationId);
22+
const githubClient = GitHubClient.fromEnv(config);
2923

3024
const comments = await githubClient.getPRComments(owner, repo, pr_number);
3125

src/mcp/tools/leave_comment.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ export async function leaveGeneralComment(
1515
try {
1616
const { message, owner, repo, pr_number } = args;
1717

18-
// Get installation ID from environment
19-
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0', 10);
20-
if (!installationId) {
21-
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
22-
}
23-
24-
const githubClient = GitHubClient.forInstallation(config, installationId);
18+
const githubClient = GitHubClient.fromEnv(config);
2519
const response = await githubClient.createPRComment(owner, repo, pr_number, message);
2620

2721
return {

src/mcp/tools/leave_inline_comment.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ export async function leaveInlineComment(
1818
try {
1919
const { message, owner, repo, pr_number, path, line, commit_sha } = args;
2020

21-
// Get installation ID from environment
22-
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0', 10);
23-
if (!installationId) {
24-
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
25-
}
26-
27-
const githubClient = GitHubClient.forInstallation(config, installationId);
21+
const githubClient = GitHubClient.fromEnv(config);
2822

2923
console.log('🎯 Creating inline comment via PR review:', { path, line });
3024

@@ -54,7 +48,7 @@ export async function leaveInlineComment(
5448
owner,
5549
repo,
5650
pr_number,
57-
message,
51+
'', // Empty body to avoid duplication
5852
'COMMENT',
5953
[{
6054
path,

src/routes/github.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const github = new Hono();
1111
// Initialize config for webhook route
1212
const config: Config = getConfig();
1313

14+
// PR actions that trigger code reviews
15+
const REVIEW_TRIGGER_ACTIONS = ['opened', 'reopened', 'ready_for_review'];
16+
1417
// We'll need to receive the reviewQueue from the main server
1518
let reviewQueue: ReviewJobQueue | null = null;
1619

@@ -49,9 +52,9 @@ async function handleInstallationEvent(payload: unknown) {
4952
* Handle pull request events
5053
*/
5154
async function handlePullRequestEvent(payload: GitHubPullRequestEvent) {
52-
// Check if this is an action we care about (opened, reopened, synchronize)
55+
// Check if this is an action we care about
5356
const action = payload.action;
54-
if (!['opened', 'reopened', 'synchronize'].includes(action)) {
57+
if (!REVIEW_TRIGGER_ACTIONS.includes(action)) {
5558
return { message: 'Action ignored' };
5659
}
5760

0 commit comments

Comments
 (0)