Skip to content

Commit 8b4775d

Browse files
committed
phase 2
1 parent 7744038 commit 8b4775d

File tree

6 files changed

+33
-17
lines changed

6 files changed

+33
-17
lines changed

src/github/process-review.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { CHECK_STATUS, CHECK_CONCLUSION, PRDetails, GitHubPullRequestEvent } fro
55

66
export async function processReview(
77
jobId: string,
8-
githubClient: GitHubClient,
8+
installationId: number,
99
payload: GitHubPullRequestEvent
1010
): Promise<void> {
1111
const config = await getConfig();
12+
const githubClient = GitHubClient.forInstallation(config, installationId);
1213

1314
try {
1415
// Extract PR information
@@ -61,7 +62,7 @@ export async function processReview(
6162
const prDetailsContent = `Repository: ${payload.repository.full_name}, PR Number: ${prDetails.pr_number}, Commit SHA: ${prDetails.commit_sha}, PR URL: ${prDetails.pr_url}`;
6263

6364
console.log(`Calling reviewDiff() for job ${jobId}`);
64-
const reviewResult = await reviewDiff(diffContent, prDetailsContent);
65+
const reviewResult = await reviewDiff(diffContent, prDetailsContent, installationId);
6566
console.log(`Review completed for job ${jobId}`);
6667

6768
// Update check run with success

src/review/review-queue.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class ReviewJobQueue {
2222
console.log(`ReviewJobQueue initialized with max queue size ${maxQueueSize} and max workers ${maxWorkers}`);
2323
}
2424

25-
enqueueReview(id: number, processReviewCb: (jobId: string) => Promise<void>): string {
25+
enqueueReview(id: number, installationId: number, processReviewCb: (jobId: string) => Promise<void>): string {
2626
// Check if queue is at capacity
2727
if (this.jobs.size >= this.maxQueueSize) {
2828
throw new QueueFullError(`Review queue is full (max: ${this.maxQueueSize})`);
@@ -33,6 +33,7 @@ export class ReviewJobQueue {
3333
status: JOB_STATUS.QUEUED,
3434
created: Date.now(),
3535
id,
36+
installationId,
3637
});
3738

3839
// Store callback and add to waiting queue

src/review/reviewer.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Config, getConfig } from "../config.js";
66
import { newThread, execute } from "../amp.js";
77

88

9-
export const reviewDiff = async (diffContent: string, mrDetailsContent: string) => {
9+
export const reviewDiff = async (diffContent: string, mrDetailsContent: string, installationId: number) => {
1010

1111
// Get config
1212
const config: Config = getConfig();
@@ -42,9 +42,22 @@ export const reviewDiff = async (diffContent: string, mrDetailsContent: string)
4242
// Write prompt to file
4343
writeFileSync(promptFilePath, promptContent, 'utf8');
4444

45-
// Write settings to file
46-
const settings = ampConfig.settings;
47-
writeFileSync(settingsFilePath, JSON.stringify(settings || {}, null, 2), 'utf8');
45+
// Write settings to file with installation ID
46+
const settings = { ...ampConfig.settings };
47+
48+
// Ensure GitHub MCP server environment exists and set installation ID
49+
settings['amp.mcpServers'] = {
50+
...settings['amp.mcpServers'],
51+
github: {
52+
...settings['amp.mcpServers']?.github,
53+
env: {
54+
...settings['amp.mcpServers']?.github?.env,
55+
GITHUB_INSTALLATION_ID: installationId.toString(),
56+
}
57+
}
58+
};
59+
60+
writeFileSync(settingsFilePath, JSON.stringify(settings, null, 2), 'utf8');
4861

4962
const threadId = await newThread(tempDir);
5063
const result = await execute({

src/review/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface JobInfo {
4949
started?: number;
5050
completed?: number;
5151
id?: number;
52+
installationId: number;
5253
result?: ReviewResult;
5354
error?: string;
5455
}

src/routes/github.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Hono } from 'hono';
22
import { v4 as uuidv4 } from 'uuid';
33
import { GitHubPullRequestEvent } from '../github/types.js';
4-
import { GitHubClient } from '../github/client.js';
54
import { QueueFullError, ReviewJobQueue } from '../review/review-queue.js';
65
import { Config, getConfig } from '../config.js';
76
import { processReview } from '../github/process-review.js';
@@ -62,21 +61,20 @@ async function handlePullRequestEvent(payload: GitHubPullRequestEvent) {
6261
const installationId = (payload as any).installation?.id;
6362
console.log('Installation ID from webhook:', installationId);
6463

65-
// Use installation-specific client
66-
const client = installationId
67-
? GitHubClient.forInstallation(config, installationId)
68-
: new GitHubClient(config);
69-
7064
if (!reviewQueue) {
7165
throw new Error('Review queue not initialized');
7266
}
7367

68+
if (!installationId) {
69+
throw new Error('Installation ID is required for review processing');
70+
}
71+
7472
// Enqueue review job
7573
const processReviewCallback = async (jobId: string) => {
76-
await processReview(jobId, client, payload);
74+
await processReview(jobId, installationId, payload);
7775
};
78-
79-
const jobId = reviewQueue.enqueueReview(payload.pull_request.number, processReviewCallback);
76+
77+
const jobId = reviewQueue.enqueueReview(payload.pull_request.number, installationId, processReviewCallback);
8078

8179
// Return immediate response
8280
return {

src/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ app.post('/test/review', async (c) => {
8989

9090
const prDetailsContent = `Repository: ${prDetails.repository_full_name}, PR Number: ${prDetails.pr_number}, Commit SHA: ${prDetails.commit_sha}, PR URL: ${prDetails.pr_url}`;
9191

92-
const result = await reviewDiff(diffContent, prDetailsContent);
92+
// For testing purposes, use a dummy installation ID
93+
const testInstallationId = 12345;
94+
const result = await reviewDiff(diffContent, prDetailsContent, testInstallationId);
9395

9496
return c.json({
9597
success: true,

0 commit comments

Comments
 (0)