@@ -8,7 +8,7 @@ import getCommitRaws from "./parser";
88import { PluginOctokit } from "./pluginOctokit" ;
99import { buildStemDict } from "./stem" ;
1010import { getSummary } from "./summary" ;
11- import type { CSMNode } from "./types" ;
11+ import type { AnalyzeGitResult } from "./types" ;
1212
1313export { buildPaginatedCSMDict } from "./csm" ;
1414
@@ -30,6 +30,12 @@ export class AnalysisEngine {
3030
3131 private baseBranchName ! : string ;
3232
33+ // Cached data
34+ private commitDict ?: ReturnType < typeof buildCommitDict > ;
35+ private pullRequests ?: Awaited < ReturnType < PluginOctokit [ "getPullRequests" ] > > ;
36+ private stemDict ?: ReturnType < typeof buildStemDict > ;
37+ private isPRSuccess : boolean = true ;
38+
3339 constructor ( args : AnalysisEngineArgs ) {
3440 this . insertArgs ( args ) ;
3541 }
@@ -51,76 +57,88 @@ export class AnalysisEngine {
5157 this . octokit = container . resolve ( PluginOctokit ) ;
5258 } ;
5359
54- public analyzeGit = async ( perPage ?: number , lastCommitId ?: string ) => {
55- let isPRSuccess = true ;
60+ public init = async ( ) => {
5661 if ( this . isDebugMode ) console . log ( "baseBranchName: " , this . baseBranchName ) ;
5762
5863 const commitRaws = getCommitRaws ( this . gitLog ) ;
59- if ( this . isDebugMode ) {
60- console . log ( "commitRaws: " , commitRaws ) ;
61- }
64+ if ( this . isDebugMode ) console . log ( "commitRaws: " , commitRaws ) ;
6265
63- const commitDict = buildCommitDict ( commitRaws ) ;
64- if ( this . isDebugMode ) console . log ( "commitDict: " , commitDict ) ;
66+ this . commitDict = buildCommitDict ( commitRaws ) ;
67+ if ( this . isDebugMode ) console . log ( "commitDict: " , this . commitDict ) ;
6568
66- const pullRequests = await this . octokit
69+ this . pullRequests = await this . octokit
6770 . getPullRequests ( )
6871 . catch ( ( err ) => {
6972 console . error ( err ) ;
70- isPRSuccess = false ;
73+ this . isPRSuccess = false ;
7174 return [ ] ;
7275 } )
7376 . then ( ( pullRequests ) => {
7477 console . log ( "success, pr = " , pullRequests ) ;
7578 return pullRequests ;
7679 } ) ;
77- if ( this . isDebugMode ) console . log ( "pullRequests: " , pullRequests ) ;
80+ if ( this . isDebugMode ) console . log ( "pullRequests: " , this . pullRequests ) ;
7881
79- const stemDict = buildStemDict ( commitDict , this . baseBranchName ) ;
80- if ( this . isDebugMode ) console . log ( "stemDict: " , stemDict ) ;
82+ this . stemDict = buildStemDict ( this . commitDict , this . baseBranchName ) ;
83+ if ( this . isDebugMode ) console . log ( "stemDict: " , this . stemDict ) ;
84+ } ;
85+
86+ public analyzeGit = async ( commitCountPerPage ?: number , lastCommitId ?: string ) : Promise < AnalyzeGitResult > => {
87+ if ( ! this . commitDict || ! this . stemDict || ! this . pullRequests ) {
88+ throw new Error ( "AnalysisEngine not initialized. Call init() first." ) ;
89+ }
8190
8291 // Paginated CSM
83- if ( perPage ) {
92+ if ( commitCountPerPage ) {
8493 const csmDict = buildPaginatedCSMDict (
85- commitDict ,
86- stemDict ,
94+ this . commitDict ,
95+ this . stemDict ,
8796 this . baseBranchName ,
88- perPage ,
97+ commitCountPerPage ,
8998 lastCommitId ,
90- pullRequests
99+ this . pullRequests
91100 ) ;
92101 const list = csmDict [ this . baseBranchName ] ?? [ ] ;
93- const lastNode : CSMNode | undefined = list . length > 0 ? list [ list . length - 1 ] : undefined ;
102+ const lastNode = list . length > 0 ? list [ list . length - 1 ] : undefined ;
94103
95- const isLastPage = list . length < perPage ;
96- const nextCommitId = ! isLastPage && lastNode ? lastNode . base . commit . id : null ;
104+ const isLastPage = list . length < commitCountPerPage ;
105+ const nextCommitId = ! isLastPage && lastNode ? lastNode . base . commit . id : undefined ;
97106
98107 return {
99- isPRSuccess,
108+ isPRSuccess : this . isPRSuccess ,
100109 csmDict,
101110 nextCommitId,
102111 isLastPage,
103112 } ;
104113 } else {
105114 // Non-paginated CSM
106- const csmDict = buildCSMDict ( commitDict , stemDict , this . baseBranchName , pullRequests ) ;
115+ const csmDict = buildCSMDict ( this . commitDict , this . stemDict , this . baseBranchName , this . pullRequests ) ;
107116 if ( this . isDebugMode ) console . log ( "csmDict: " , csmDict ) ;
108- const nodes = stemDict . get ( this . baseBranchName ) ?. nodes ?. map ( ( { commit } ) => commit ) ;
117+ const nodes = this . stemDict . get ( this . baseBranchName ) ?. nodes ?. map ( ( { commit } ) => commit ) ;
109118 const geminiCommitSummary = await getSummary ( nodes ? nodes ?. slice ( - 10 ) : [ ] ) ;
110119 if ( this . isDebugMode ) console . log ( "GeminiCommitSummary: " , geminiCommitSummary ) ;
111120
112121 return {
113- isPRSuccess,
122+ isPRSuccess : this . isPRSuccess ,
114123 csmDict,
115- nextCommitId : null ,
124+ nextCommitId : undefined ,
116125 isLastPage : true ,
117126 } ;
118127 }
119128 } ;
120129
130+ public getBaseBranchName = ( ) => {
131+ return this . baseBranchName ;
132+ } ;
133+
121134 public updateArgs = ( args : AnalysisEngineArgs ) => {
122135 if ( container . isRegistered ( "OctokitOptions" ) ) container . clearInstances ( ) ;
123136 this . insertArgs ( args ) ;
137+ // Clear cached data
138+ this . commitDict = undefined ;
139+ this . stemDict = undefined ;
140+ this . pullRequests = undefined ;
141+ this . isPRSuccess = true ;
124142 } ;
125143}
126144
0 commit comments