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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
// Removed @actions imports for Probot migration
import {octokit} from './octokit'

import {als} from './context'
import pino from 'pino'
import pRetry from 'p-retry'

const logger = pino({ level: process.env.LOG_LEVEL || 'info' })
const info = (msg: string) => logger.info(msg)
const warning = (msg: string) => logger.warn(msg)
const getInput = (name: string) => process.env[name.toUpperCase()] || ''

// Retry configuration for GitHub API calls
const GITHUB_RETRY_OPTIONS = {
  retries: 3,
  minTimeout: 1000,
  maxTimeout: 10000,
  factor: 2,
  onFailedAttempt: (error: any) => {
    logger.warn({
      attemptNumber: error.attemptNumber,
      retriesLeft: error.retriesLeft,
      error: error.message
    }, 'GitHub API call failed, retrying...')
  }
}

// Wrapper for retryable GitHub API calls
async function withRetry<T>(fn: () => Promise<T>): Promise<T> {
  return pRetry(fn, GITHUB_RETRY_OPTIONS)
}

export const context: any = new Proxy({} as any, {
  get(target, prop) {
    return (als.getStore()?.probotContext as any)[prop]
  }
})

export const repo: any = new Proxy({} as any, {
  get(target, prop) {
    return (als.getStore()?.repo as any)[prop]
  }
})

export const setCommenterContext = (newContext: any) => {
  // context and repo are now Proxies
}

export const LOGO_URL = 'https://www.prixai.xyz/logo.png'

export const COMMENT_GREETING = `<div align="center">



<img src="${LOGO_URL}" width="64" height="64" alt="PRIX Logo">



# 🤖 PRIX AI Code Review



</div>



---



Welcome! I'm PRIX, your AI code reviewer. I've analyzed this pull request and here are my findings:



---`

export const COMMENT_TAG =
  '<!-- This is an auto-generated comment by AI PR Reviewer -->'

export const COMMENT_REPLY_TAG =
  '<!-- This is an auto-generated reply by AI PR Reviewer -->'

export const SUMMARIZE_TAG =
  '<!-- This is an auto-generated comment: summarize by AI PR Reviewer -->'

export const IN_PROGRESS_START_TAG =
  '<!-- This is an auto-generated comment: summarize review in progress by AI PR Reviewer -->'

export const IN_PROGRESS_END_TAG =
  '<!-- end of auto-generated comment: summarize review in progress by AI PR Reviewer -->'

export const DESCRIPTION_START_TAG =
  '<!-- This is an auto-generated comment: release notes by AI PR Reviewer -->'
export const DESCRIPTION_END_TAG =
  '<!-- end of auto-generated comment: release notes by AI PR Reviewer -->'

export const RAW_SUMMARY_START_TAG = `<!-- This is an auto-generated comment: raw summary by AI PR Reviewer -->

<!--

`
export const RAW_SUMMARY_END_TAG = `-->

<!-- end of auto-generated comment: raw summary by AI PR Reviewer -->`

export const SHORT_SUMMARY_START_TAG = `<!-- This is an auto-generated comment: short summary by AI PR Reviewer -->

<!--

`

export const SHORT_SUMMARY_END_TAG = `-->

<!-- end of auto-generated comment: short summary by OSS CodeRabbit -->`

export const COMMIT_ID_START_TAG = '<!-- commit_ids_reviewed_start -->'
export const COMMIT_ID_END_TAG = '<!-- commit_ids_reviewed_end -->'

export const VERIFIED_TAG = '<!-- verified_by_prix -->'
export const VERIFICATION_FAILED_TAG = '<!-- verification_failed -->'

export const ISSUE_REPLY_TAG =
  '<!-- This is an auto-generated issue analysis by PRIX -->'

export const PRIX_BRANDING = `



---



<div align="center">



🚀 **[Get PRIX for your team](https://www.prixai.xyz/)** — AI-powered code reviews that catch bugs before they ship



</div>`

export const PLAN_BRANDING = `



---



<div align="center">



🚀 **[Get PRIX for your team](https://www.prixai.xyz/)** — AI-powered code reviews that catch bugs before they ship



</div>`

export class Commenter {
  /**

   * @param mode Can be "create", "replace". Default is "replace".

   */
  async comment(message: string, tag: string, mode: string) {
    let target: number
    if (context.payload.pull_request != null) {
      target = context.payload.pull_request.number
    } else if (context.payload.issue != null) {
      target = context.payload.issue.number
    } else {
      warning(
        'Skipped: context.payload.pull_request and context.payload.issue are both null'
      )
      return
    }

    if (!tag) {
      tag = COMMENT_TAG
    }

    const body = `${COMMENT_GREETING}



${message}



${tag}${PRIX_BRANDING}`

    if (mode === 'create') {
      await this.create(body, target)
    } else if (mode === 'replace') {
      await this.replace(body, tag, target)
    } else {
      warning(`Unknown mode: ${mode}, use "replace" instead`)
      await this.replace(body, tag, target)
    }
  }

  getContentWithinTags(content: string, startTag: string, endTag: string) {
    const start = content.indexOf(startTag)
    const end = content.indexOf(endTag)
    if (start >= 0 && end >= 0) {
      return content.slice(start + startTag.length, end)
    }
    return ''
  }

  removeContentWithinTags(content: string, startTag: string, endTag: string) {
    const start = content.indexOf(startTag)
    const end = content.lastIndexOf(endTag)
    if (start >= 0 && end >= 0) {
      return content.slice(0, start) + content.slice(end + endTag.length)
    }
    return content
  }

  getRawSummary(summary: string) {
    return this.getContentWithinTags(
      summary,
      RAW_SUMMARY_START_TAG,
      RAW_SUMMARY_END_TAG
    )
  }

  getShortSummary(summary: string) {
    return this.getContentWithinTags(
      summary,
      SHORT_SUMMARY_START_TAG,
      SHORT_SUMMARY_END_TAG
    )
  }

  getDescription(description: string) {
    return this.removeContentWithinTags(
      description,
      DESCRIPTION_START_TAG,
      DESCRIPTION_END_TAG
    )
  }

  getReleaseNotes(description: string) {
    const releaseNotes = this.getContentWithinTags(
      description,
      DESCRIPTION_START_TAG,
      DESCRIPTION_END_TAG
    )
    return releaseNotes.replace(/(^|\n)> .*/g, '')
  }

  async updateDescription(pullNumber: number, message: string) {
    // add this response to the description field of the PR as release notes by looking
    // for the tag (marker)
    try {
      // get latest description from PR
      const pr = await octokit.rest.pulls.get({
        owner: repo.owner,
        repo: repo.repo,
        // eslint-disable-next-line camelcase
        pull_number: pullNumber,
        request: { timeout: 10000 }
      })
      let body = ''
      if (pr.data.body) {
        body = pr.data.body
      }
      const description = this.getDescription(body)

      const messageClean = this.removeContentWithinTags(
        message,
        DESCRIPTION_START_TAG,
        DESCRIPTION_END_TAG
      )
      const newDescription = `${description}\n${DESCRIPTION_START_TAG}\n${messageClean}\n${DESCRIPTION_END_TAG}`
      await octokit.rest.pulls.update({
        owner: repo.owner,
        repo: repo.repo,
        // eslint-disable-next-line camelcase
        pull_number: pullNumber,
        body: newDescription,
        request: { timeout: 10000 }
      })
    } catch (e) {
      warning(
        `Failed to get PR: ${e}, skipping adding release notes to description.`
      )
    }
  }

  private readonly reviewCommentsBuffer: Array<{
    path: string
    startLine: number
    endLine: number
    message: string
    verified?: boolean
    verificationFeedback?: string
    level?: string
    type?: string
  }> = []

  async bufferReviewComment(

    path: string,

    startLine: number,

    endLine: number,

    message: string,

    verified?: boolean,

    verificationFeedback?: string,

    testCase?: string

  ) {
    // Premium UI Formatting: Convert "[LEVEL] | [TYPE]" to emoji badges
    const levelTypeRegex = /^(\w+)\s*\|\s*(\w+)/
    const match = message.match(levelTypeRegex)

    let level = 'info'
    let type = 'General'
    let severityEmoji = 'ℹ️'
    let color = 'Info'

    if (match != null) {
      level = match[1].toLowerCase()
      type = match[2]

      if (level === 'critical') {
        severityEmoji = '🚨'
        color = 'Critical'
      } else if (level === 'major') {
        severityEmoji = '🔴'
        color = 'Major'
      } else if (level === 'minor') {
        severityEmoji = '🟠'
        color = 'Minor'
      } else if (level === 'info') {
        severityEmoji = 'ℹ️'
        color = 'Info'
      }

      message = message.replace(levelTypeRegex, '').trim()
    }

    // Generate AI Agent Prompt for actionable issues
    let aiAgentSection = ''
    if (level === 'critical' || level === 'major') {
      const aiPrompt = this.generateAIAgentPrompt(path, startLine, endLine, message)
      aiAgentSection = `



<details>

<summary>🤖 Prompt for AI Agents</summary>



Copy and paste this prompt into your AI IDE (Cursor, Windsurf, etc.) to auto-fix:



\`\`\`

${aiPrompt}

\`\`\`



</details>`
    }

    let verificationBadge = ''
    if (verified === true) {
      verificationBadge =
        '\n---\n✅ **Verified**: This suggestion passed syntax, lint, and AST validation.\n'
    } else if (verified === false && verificationFeedback) {
      verificationBadge = `\n---\n⚠️ **Verification Failed**: ${verificationFeedback}\n`
    }

    // Add test case section if available
    const testCaseSection = testCase ? `\n\n${testCase}` : ''

    // CodeRabbit-style formatted comment
    const formattedMessage = `**${severityEmoji} Potential Issue | ${color} (${type})**



${message}${aiAgentSection}${testCaseSection}${verificationBadge}



${COMMENT_TAG}`

    this.reviewCommentsBuffer.push({
      path,
      startLine,
      endLine,
      message: formattedMessage,
      verified,
      verificationFeedback,
      level,
      type
    })
  }

  private generateAIAgentPrompt(
    path: string,
    startLine: number,
    endLine: number,
    issue: string
  ): string {
    return `Fix the following issue in ${path} around lines ${startLine}-${endLine}:



Issue: ${issue.split('\n')[0]}



Steps:

1. Read the file ${path}

2. Locate lines ${startLine}-${endLine}

3. Fix the issue described above

4. Ensure the fix follows the existing code style

5. Verify no other parts of the code are broken



Provide the complete fixed code block.`
  }

  async deletePendingReview(pullNumber: number) {
    try {
      const reviews = await octokit.rest.pulls.listReviews({
        owner: repo.owner,
        repo: repo.repo,
        // eslint-disable-next-line camelcase
        pull_number: pullNumber,
        request: { timeout: 10000 }
      })

      const pendingReview = reviews.data.find(
        review => review.state === 'PENDING'
      )

      if (pendingReview) {
        info(
          `Deleting pending review for PR #${pullNumber} id: ${pendingReview.id}`
        )
        try {
          await octokit.rest.pulls.deletePendingReview({
            owner: repo.owner,
            repo: repo.repo,
            // eslint-disable-next-line camelcase
            pull_number: pullNumber,
            // eslint-disable-next-line camelcase
            review_id: pendingReview.id,
            request: { timeout: 10000 }
          })
        } catch (e) {
          warning(`Failed to delete pending review: ${e}`)
        }
      }
    } catch (e) {
      warning(`Failed to list reviews: ${e}`)
    }
  }

  async submitReview(pullNumber: number, commitId: string, statusMsg: string) {
    const bodyHeader = `${COMMENT_GREETING}



${statusMsg}



${PRIX_BRANDING}`

    if (this.reviewCommentsBuffer.length === 0) {
      // Submit empty review with statusMsg
      info(`Submitting empty review for PR #${pullNumber}`)
      try {
        await octokit.rest.pulls.createReview({
          owner: repo.owner,
          repo: repo.repo,
          // eslint-disable-next-line camelcase
          pull_number: pullNumber,
          // eslint-disable-next-line camelcase
          commit_id: commitId,
          event: 'COMMENT',
          body: bodyHeader,
          request: { timeout: 10000 }
        })
      } catch (e) {
        warning(`Failed to submit empty review: ${e}`)
      }
      return
    }

    // De-duplicate and clear existing bot comments in the same range
    for (const comment of this.reviewCommentsBuffer) {
      const comments = await this.getCommentsAtRange(
        pullNumber,
        comment.path,
        comment.startLine,
        comment.endLine
      )
      for (const c of comments) {
        if (c.body.includes(COMMENT_TAG)) {
          try {
            await octokit.rest.pulls.deleteReviewComment({
              owner: repo.owner,
              repo: repo.repo,
              // eslint-disable-next-line camelcase
              comment_id: c.id,
              request: { timeout: 10000 }
            })
          } catch (e) {
            warning(`Failed to delete review comment: ${e}`)
          }
        }
      }
    }

    await this.deletePendingReview(pullNumber)

    // Unified Review: All comments are standalone threads to support suggestions
    const pushableComments = this.reviewCommentsBuffer.map(comment =>
      this.generateCommentData(comment)
    )

    try {
      const review = await octokit.rest.pulls.createReview({
        owner: repo.owner,
        repo: repo.repo,
        // eslint-disable-next-line camelcase
        pull_number: pullNumber,
        // eslint-disable-next-line camelcase
        commit_id: commitId,
        comments: pushableComments.map(comment => comment),
        request: { timeout: 10000 }
      })

      info(
        `Submitting review for PR #${pullNumber}, total comments: ${pushableComments.length}, review id: ${review.data.id}`
      )

      await octokit.rest.pulls.submitReview({
        owner: repo.owner,
        repo: repo.repo,
        // eslint-disable-next-line camelcase
        pull_number: pullNumber,
        // eslint-disable-next-line camelcase
        review_id: review.data.id,
        event: 'COMMENT',
        body: bodyHeader,
        request: { timeout: 10000 }
      })
    } catch (e) {
      warning(
        `Failed to create review: ${e}. Falling back to individual comments.`
      )
      await this.deletePendingReview(pullNumber)
      let commentCounter = 0
      for (const comment of pushableComments) {
        info(
          `Creating new review comment for ${comment.path}:${comment.start_line || comment.line}-${comment.line}: ${comment.body}`
        )
        const commentData: any = {
          owner: repo.owner,
          repo: repo.repo,
          // eslint-disable-next-line camelcase
          pull_number: pullNumber,
          // eslint-disable-next-line camelcase
          commit_id: commitId,
          ...comment
        }

        try {
          await octokit.rest.pulls.createReviewComment({...commentData, request: { timeout: 10000 }})
        } catch (ee) {
          warning(`Failed to create review comment: ${ee}`)
        }

        commentCounter++
        info(`Comment ${commentCounter}/${pushableComments.length} posted`)
      }
    }
  }

  private generateCommentData(comment: any) {
    const commentData: any = {
      path: comment.path,
      body: comment.message,
      line: comment.endLine
    }

    if (comment.startLine !== comment.endLine) {
      // eslint-disable-next-line camelcase
      commentData.start_line = comment.startLine
      // eslint-disable-next-line camelcase
      commentData.start_side = 'RIGHT'
    }

    return commentData
  }

  async reviewCommentReply(

    pullNumber: number,

    topLevelComment: any,

    message: string

  ) {
    const reply = `${COMMENT_GREETING}



${message}



${COMMENT_REPLY_TAG}${PRIX_BRANDING}

`
    try {
      // Post the reply to the user comment
      await octokit.rest.pulls.createReplyForReviewComment({
        owner: repo.owner,
        repo: repo.repo,
        // eslint-disable-next-line camelcase
        pull_number: pullNumber,
        body: reply,
        // eslint-disable-next-line camelcase
        comment_id: topLevelComment.id,
        request: { timeout: 10000 }
      })
    } catch (error) {
      warning(`Failed to reply to the top-level comment ${error}`)
      try {
        await octokit.rest.pulls.createReplyForReviewComment({
          owner: repo.owner,
          repo: repo.repo,
          // eslint-disable-next-line camelcase
          pull_number: pullNumber,
          body: `Could not post the reply to the top-level comment due to the following error: ${error}`,
          // eslint-disable-next-line camelcase
          comment_id: topLevelComment.id,
          request: { timeout: 10000 }
        })
      } catch (e) {
        warning(`Failed to reply to the top-level comment ${e}`)
      }
    }
    try {
      if (topLevelComment.body.includes(COMMENT_TAG)) {
        // replace COMMENT_TAG with COMMENT_REPLY_TAG in topLevelComment
        const newBody = topLevelComment.body.replace(
          COMMENT_TAG,
          COMMENT_REPLY_TAG
        )
        await octokit.rest.pulls.updateReviewComment({
          owner: repo.owner,
          repo: repo.repo,
          // eslint-disable-next-line camelcase
          comment_id: topLevelComment.id,
          body: newBody,
          request: { timeout: 10000 }
        })
      }
    } catch (error) {
      warning(`Failed to update the top-level comment ${error}`)
    }
  }

  async getCommentsWithinRange(

    pullNumber: number,

    path: string,

    startLine: number,

    endLine: number

  ) {
    const comments = await this.listReviewComments(pullNumber)
    return comments.filter(
      (comment: any) =>
        comment.path === path &&
        comment.body !== '' &&
        ((comment.start_line !== undefined &&
          comment.start_line >= startLine &&
          comment.line <= endLine) ||
          (startLine === endLine && comment.line === endLine))
    )
  }

  async getCommentsAtRange(

    pullNumber: number,

    path: string,

    startLine: number,

    endLine: number

  ) {
    const comments = await this.listReviewComments(pullNumber)
    return comments.filter(
      (comment: any) =>
        comment.path === path &&
        comment.body !== '' &&
        ((comment.start_line !== undefined &&
          comment.start_line === startLine &&
          comment.line === endLine) ||
          (startLine === endLine && comment.line === endLine))
    )
  }

  async getCommentChainsWithinRange(

    pullNumber: number,

    path: string,

    startLine: number,

    endLine: number,

    tag = ''

  ) {
    const existingComments = await this.getCommentsWithinRange(
      pullNumber,
      path,
      startLine,
      endLine
    )
    // find all top most comments
    const topLevelComments = []
    for (const comment of existingComments) {
      if (!comment.in_reply_to_id) {
        topLevelComments.push(comment)
      }
    }

    let allChains = ''
    let chainNum = 0
    for (const topLevelComment of topLevelComments) {
      // get conversation chain
      const chain = await this.composeCommentChain(
        existingComments,
        topLevelComment
      )
      if (chain && chain.includes(tag)) {
        chainNum += 1
        allChains += `Conversation Chain ${chainNum}:

${chain}

---

`
      }
    }
    return allChains
  }

  async composeCommentChain(reviewComments: any[], topLevelComment: any) {
    const conversationChain = reviewComments
      .filter((cmt: any) => cmt.in_reply_to_id === topLevelComment.id)
      .map((cmt: any) => `${cmt.user.login}: ${cmt.body}`)

    conversationChain.unshift(
      `${topLevelComment.user.login}: ${topLevelComment.body}`
    )

    return conversationChain.join('\n---\n')
  }

  async getCommentChain(pullNumber: number, comment: any) {
    try {
      const reviewComments = await this.listReviewComments(pullNumber)
      const topLevelComment = await this.getTopLevelComment(
        reviewComments,
        comment
      )
      const chain = await this.composeCommentChain(
        reviewComments,
        topLevelComment
      )
      return {chain, topLevelComment}
    } catch (e) {
      warning(`Failed to get conversation chain: ${e}`)
      return {
        chain: '',
        topLevelComment: null
      }
    }
  }

  async getTopLevelComment(reviewComments: any[], comment: any) {
    let topLevelComment = comment

    while (topLevelComment.in_reply_to_id) {
      const parentComment = reviewComments.find(
        (cmt: any) => cmt.id === topLevelComment.in_reply_to_id
      )

      if (parentComment) {
        topLevelComment = parentComment
      } else {
        break
      }
    }

    return topLevelComment
  }

  private reviewCommentsCache: Record<number, any[]> = {}

  async listReviewComments(target: number) {
    if (this.reviewCommentsCache[target]) {
      return this.reviewCommentsCache[target]
    }

    const allComments: any[] = []
    let page = 1
    try {
      for (;;) {
        const {data: comments} = await octokit.rest.pulls.listReviewComments({
          owner: repo.owner,
          repo: repo.repo,
          // eslint-disable-next-line camelcase
          pull_number: target,
          page,
          // eslint-disable-next-line camelcase
          per_page: 100,
          request: { timeout: 10000 }
        })
        allComments.push(...comments)
        page++
        if (!comments || comments.length < 100) {
          break
        }
      }

      this.reviewCommentsCache[target] = allComments
      return allComments
    } catch (e) {
      warning(`Failed to list review comments: ${e}`)
      return allComments
    }
  }

  async create(body: string, target: number) {
    try {
      // get comment ID from the response
      const response = await octokit.rest.issues.createComment({
        owner: repo.owner,
        repo: repo.repo,
        // eslint-disable-next-line camelcase
        issue_number: target,
        body,
        request: { timeout: 10000 }
      })
      // add comment to issueCommentsCache
      if (this.issueCommentsCache[target]) {
        this.issueCommentsCache[target].push(response.data)
      } else {
        this.issueCommentsCache[target] = [response.data]
      }
    } catch (e) {
      warning(`Failed to create comment: ${e}`)
    }
  }

  async replace(body: string, tag: string, target: number) {
    try {
      const cmt = await this.findCommentWithTag(tag, target)
      if (cmt) {
        await octokit.rest.issues.updateComment({
          owner: repo.owner,
          repo: repo.repo,
          // eslint-disable-next-line camelcase
          comment_id: cmt.id,
          body,
          request: { timeout: 10000 }
        })
      } else {
        await this.create(body, target)
      }
    } catch (e) {
      warning(`Failed to replace comment: ${e}`)
    }
  }

  async findCommentWithTag(tag: string, target: number) {
    try {
      const comments = await this.listComments(target)
      for (const cmt of comments) {
        if (cmt.body && cmt.body.includes(tag)) {
          return cmt
        }
      }

      return null
    } catch (e: unknown) {
      warning(`Failed to find comment with tag: ${e}`)
      return null
    }
  }

  private issueCommentsCache: Record<number, any[]> = {}

  async listComments(target: number) {
    if (this.issueCommentsCache[target]) {
      return this.issueCommentsCache[target]
    }

    const allComments: any[] = []
    let page = 1
    try {
      for (;;) {
        const {data: comments} = await octokit.rest.issues.listComments({
          owner: repo.owner,
          repo: repo.repo,
          // eslint-disable-next-line camelcase
          issue_number: target,
          page,
          // eslint-disable-next-line camelcase
          per_page: 100,
          request: { timeout: 10000 }
        })
        allComments.push(...comments)
        page++
        if (!comments || comments.length < 100) {
          break
        }
      }

      this.issueCommentsCache[target] = allComments
      return allComments
    } catch (e: any) {
      warning(`Failed to list comments: ${e}`)
      return allComments
    }
  }

  // function that takes a comment body and returns the list of commit ids that have been reviewed
  // commit ids are comments between the commit_ids_reviewed_start and commit_ids_reviewed_end markers
  // <!-- [commit_id] -->
  getReviewedCommitIds(commentBody: string): string[] {
    const start = commentBody.indexOf(COMMIT_ID_START_TAG)
    const end = commentBody.indexOf(COMMIT_ID_END_TAG)
    if (start === -1 || end === -1) {
      return []
    }
    const ids = commentBody.substring(start + COMMIT_ID_START_TAG.length, end)
    // remove the <!-- and --> markers from each id and extract the id and remove empty strings
    return ids
      .split('<!--')
      .map(id => id.replace('-->', '').trim())
      .filter(id => id !== '')
  }

  // get review commit ids comment block from the body as a string
  // including markers
  getReviewedCommitIdsBlock(commentBody: string): string {
    const start = commentBody.indexOf(COMMIT_ID_START_TAG)
    const end = commentBody.indexOf(COMMIT_ID_END_TAG)
    if (start === -1 || end === -1) {
      return ''
    }
    return commentBody.substring(start, end + COMMIT_ID_END_TAG.length)
  }

  // add a commit id to the list of reviewed commit ids
  // if the marker doesn't exist, add it
  addReviewedCommitId(commentBody: string, commitId: string): string {
    const start = commentBody.indexOf(COMMIT_ID_START_TAG)
    const end = commentBody.indexOf(COMMIT_ID_END_TAG)
    if (start === -1 || end === -1) {
      return `${commentBody}\n${COMMIT_ID_START_TAG}\n<!-- ${commitId} -->\n${COMMIT_ID_END_TAG}`
    }
    const ids = commentBody.substring(start + COMMIT_ID_START_TAG.length, end)
    return `${commentBody.substring(

      0,

      start + COMMIT_ID_START_TAG.length

    )}${ids}<!-- ${commitId} -->\n${commentBody.substring(end)}`
  }

  // given a list of commit ids provide the highest commit id that has been reviewed
  getHighestReviewedCommitId(
    commitIds: string[],
    reviewedCommitIds: string[]
  ): string {
    for (let i = commitIds.length - 1; i >= 0; i--) {
      if (reviewedCommitIds.includes(commitIds[i])) {
        return commitIds[i]
      }
    }
    return ''
  }

  async getAllCommitIds(): Promise<string[]> {
    const allCommits = []
    let page = 1
    let commits
    if (context && context.payload && context.payload.pull_request != null) {
      do {
        commits = await octokit.rest.pulls.listCommits({
          owner: repo.owner,
          repo: repo.repo,
          // eslint-disable-next-line camelcase
          pull_number: context.payload.pull_request.number,
          // eslint-disable-next-line camelcase
          per_page: 100,
          page,
          request: { timeout: 10000 }
        })

        allCommits.push(...commits.data.map(commit => commit.sha))
        page++
      } while (commits.data.length > 0)
    }

    return allCommits
  }

  // add in-progress status to the comment body
  addInProgressStatus(commentBody: string, statusMsg: string): string {
    const start = commentBody.indexOf(IN_PROGRESS_START_TAG)
    const end = commentBody.indexOf(IN_PROGRESS_END_TAG)
    // add to the beginning of the comment body if the marker doesn't exist
    // otherwise do nothing
    if (start === -1 || end === -1) {
      return `${IN_PROGRESS_START_TAG}



Currently reviewing new changes in this PR...



${statusMsg}



${IN_PROGRESS_END_TAG}



---



${commentBody}`
    }
    return commentBody
  }

  // remove in-progress status from the comment body
  removeInProgressStatus(commentBody: string): string {
    const start = commentBody.indexOf(IN_PROGRESS_START_TAG)
    const end = commentBody.indexOf(IN_PROGRESS_END_TAG)
    // remove the in-progress status if the marker exists
    // otherwise do nothing
    if (start !== -1 && end !== -1) {
      return (
        commentBody.substring(0, start) +
        commentBody.substring(end + IN_PROGRESS_END_TAG.length)
      )
    }
    return commentBody
  }
}