1
1
import { config } from 'dotenv'
2
- import { Probot , type Context } from 'probot'
2
+ import * as fs from 'fs/promises'
3
+ import * as path from 'path'
4
+ import { Probot } from 'probot'
5
+ import { getCommentHandler } from './comment-handlers/index.ts'
3
6
import { sendToAnthropic } from './send-to-anthropic.ts'
4
7
5
8
// Load environment variables
6
9
config ( )
7
10
8
- // Marker to identify our AI analysis comments
9
- const COMMENT_MARKER = '<!-- REVU-AI-ANALYSIS -->'
10
-
11
11
export default async ( app : Probot , { getRouter } ) => {
12
12
app . log . info ( 'Revu GitHub App started!' )
13
13
14
14
// Container health check route
15
15
getRouter ( '/healthz' ) . get ( '/' , ( req , res ) => res . end ( 'OK' ) )
16
16
17
- /**
18
- * Find existing AI analysis comment by looking for the unique marker
19
- */
20
- async function findExistingAnalysisComment ( context : Context , prNumber ) {
21
- const repo = context . repo ( )
22
-
23
- // Get all comments on the PR
24
- const { data : comments } = await context . octokit . issues . listComments ( {
25
- ...repo ,
26
- issue_number : prNumber
27
- } )
28
-
29
- // Find the comment with our marker
30
- return comments . find ( ( comment ) => comment . body . includes ( COMMENT_MARKER ) )
31
- }
32
-
33
17
// Listen for PR opens and updates
34
18
app . on (
35
19
[ 'pull_request.opened' , 'pull_request.synchronize' ] ,
@@ -52,44 +36,43 @@ export default async (app: Probot, { getRouter }) => {
52
36
} )
53
37
. then ( ( response ) => response . data . token )
54
38
39
+ // Get the current strategy from configuration
40
+ const strategyName = await getStrategyNameFromConfig ( )
41
+
55
42
// Get the analysis from Anthropic
56
43
const analysis = await sendToAnthropic ( {
57
44
repositoryUrl,
58
45
branch,
59
- token : installationAccessToken
46
+ token : installationAccessToken ,
47
+ strategyName
60
48
} )
61
49
62
- // Format the analysis with our marker
63
- const formattedAnalysis = ` ${ COMMENT_MARKER } \n\n ${ analysis } `
50
+ // Get the appropriate comment handler based on the strategy
51
+ const commentHandler = getCommentHandler ( strategyName )
64
52
65
- // Check if we already have an analysis comment
66
- const existingComment = await findExistingAnalysisComment (
67
- context ,
68
- pr . number
69
- )
70
-
71
- if ( existingComment ) {
72
- // Update the existing comment
73
- await context . octokit . issues . updateComment ( {
74
- ...repo ,
75
- comment_id : existingComment . id ,
76
- body : formattedAnalysis
77
- } )
78
- app . log . info ( `Updated existing analysis comment on PR #${ pr . number } ` )
79
- } else {
80
- // Post a new comment
81
- await context . octokit . issues . createComment ( {
82
- ...repo ,
83
- issue_number : pr . number ,
84
- body : formattedAnalysis
85
- } )
86
- app . log . info ( `Created new analysis comment on PR #${ pr . number } ` )
87
- }
53
+ // Handle the analysis with the appropriate handler
54
+ const result = await commentHandler ( context , pr . number , analysis )
88
55
56
+ app . log . info ( result )
89
57
app . log . info ( `Successfully analyzed PR #${ pr . number } ` )
90
58
} catch ( error ) {
91
59
app . log . error ( `Error processing PR #${ pr . number } : ${ error } ` )
92
60
}
93
61
}
94
62
)
63
+
64
+ /**
65
+ * Gets the strategy name from the configuration file
66
+ */
67
+ async function getStrategyNameFromConfig ( ) {
68
+ try {
69
+ const configPath = path . join ( process . cwd ( ) , 'config.json' )
70
+ const configContent = await fs . readFile ( configPath , 'utf-8' )
71
+ const config = JSON . parse ( configContent )
72
+ return config . promptStrategy || 'default'
73
+ } catch ( error ) {
74
+ console . error ( 'Error reading configuration:' , error )
75
+ return 'default'
76
+ }
77
+ }
95
78
}
0 commit comments