@@ -14,6 +14,9 @@ import {
1414 AddIssueCommentResponse ,
1515 AddPullRequestToProjectResponse ,
1616 EditIssueCommentResponse ,
17+ LatestCommit ,
18+ LatestReviewThread ,
19+ LatestUpdatesResponse ,
1720 TimelineEventsResponse ,
1821 UpdateIssueResponse ,
1922} from './graphql' ;
@@ -351,6 +354,52 @@ export class IssueModel<TItem extends Issue = Issue> {
351354 }
352355 }
353356
357+ protected getUpdatesQuery ( schema : any ) : any {
358+ return schema . LatestIssueUpdates ;
359+ }
360+
361+ async getLastUpdateTime ( time : Date ) : Promise < Date > {
362+ Logger . debug ( `Fetch timeline events of issue #${ this . number } - enter` , IssueModel . ID ) ;
363+ const githubRepository = this . githubRepository ;
364+ const { query, remote, schema } = await githubRepository . ensure ( ) ;
365+ try {
366+ const { data } = await query < LatestUpdatesResponse > ( {
367+ query : this . getUpdatesQuery ( schema ) ,
368+ variables : {
369+ owner : remote . owner ,
370+ name : remote . repositoryName ,
371+ number : this . number ,
372+ since : new Date ( time ) ,
373+ }
374+ } ) ;
375+
376+ const times = [
377+ time ,
378+ new Date ( data . repository . pullRequest . updatedAt ) ,
379+ ...( data . repository . pullRequest . reactions . nodes . map ( node => new Date ( node . createdAt ) ) ) ,
380+ ...( data . repository . pullRequest . comments . nodes . map ( node => new Date ( node . updatedAt ) ) ) ,
381+ ...( data . repository . pullRequest . comments . nodes . flatMap ( node => node . reactions . nodes . map ( reaction => new Date ( reaction . createdAt ) ) ) ) ,
382+ ...( data . repository . pullRequest . timelineItems . nodes . map ( node => {
383+ const latestCommit = node as Partial < LatestCommit > ;
384+ if ( latestCommit . commit ?. authoredDate ) {
385+ return new Date ( latestCommit . commit . authoredDate ) ;
386+ }
387+ const latestReviewThread = node as Partial < LatestReviewThread > ;
388+ if ( ( latestReviewThread . comments ?. nodes . length ?? 0 ) > 0 ) {
389+ return new Date ( latestReviewThread . comments ! . nodes [ 0 ] . createdAt ) ;
390+ }
391+ return new Date ( ( node as { createdAt : string } ) . createdAt ) ;
392+ } ) )
393+ ] ;
394+
395+ // Sort times and return the most recent one
396+ return new Date ( Math . max ( ...times . map ( t => t . getTime ( ) ) ) ) ;
397+ } catch ( e ) {
398+ Logger . error ( `Error fetching timeline events of issue #${ this . number } - ${ formatError ( e ) } ` , IssueModel . ID ) ;
399+ return time ; // Return the original time in case of an error
400+ }
401+ }
402+
354403 async updateMilestone ( id : string ) : Promise < void > {
355404 const { mutate, schema } = await this . githubRepository . ensure ( ) ;
356405 const finalId = id === 'null' ? null : id ;
0 commit comments