1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
2
// processStream.ts content
3
3
import { singleOrDefault , timeout } from '@crowd/common'
4
- import { GraphQlQueryResponse } from '@crowd/types'
4
+ import { GraphQlQueryResponse , IConcurrentRequestLimiter } from '@crowd/types'
5
5
import { createAppAuth } from '@octokit/auth-app'
6
6
import { AuthInterface } from '@octokit/auth-app/dist-types/types'
7
- import { IProcessStreamContext , ProcessStreamHandler } from '../../types'
7
+ import {
8
+ IProcessStreamContext ,
9
+ ProcessStreamHandler ,
10
+ IProcessWebhookStreamContext ,
11
+ } from '../../types'
8
12
import DiscussionCommentsQuery from './api/graphql/discussionComments'
9
13
import DiscussionsQuery from './api/graphql/discussions'
10
14
import ForksQuery from './api/graphql/forks'
@@ -40,6 +44,7 @@ import {
40
44
const IS_TEST_ENV : boolean = process . env . NODE_ENV === 'test'
41
45
42
46
let githubAuthenticator : AuthInterface | undefined = undefined
47
+ let concurrentRequestLimiter : IConcurrentRequestLimiter | undefined = undefined
43
48
44
49
function getAuth ( ctx : IProcessStreamContext ) : AuthInterface | undefined {
45
50
const GITHUB_CONFIG = ctx . platformSettings as GithubPlatformSettings
@@ -60,6 +65,18 @@ function getAuth(ctx: IProcessStreamContext): AuthInterface | undefined {
60
65
return githubAuthenticator
61
66
}
62
67
68
+ export function getConcurrentRequestLimiter (
69
+ ctx : IProcessStreamContext | IProcessWebhookStreamContext ,
70
+ ) : IConcurrentRequestLimiter {
71
+ if ( concurrentRequestLimiter === undefined ) {
72
+ concurrentRequestLimiter = ctx . getConcurrentRequestLimiter (
73
+ 2 , // max 2 concurrent requests
74
+ 'github-concurrent-request-limiter' ,
75
+ )
76
+ }
77
+ return concurrentRequestLimiter
78
+ }
79
+
63
80
// const getTokenFromCache = async (ctx: IProcessStreamContext) => {
64
81
// const key = 'github-token-cache'
65
82
// const cache = ctx.integrationCache // this cache is tied up with integrationId
@@ -219,7 +236,10 @@ const processRootStream: ProcessStreamHandler = async (ctx) => {
219
236
try {
220
237
// we don't need to get default 100 item per page, just 1 is enough to check if repo is available
221
238
const stargazersQuery = new StargazersQuery ( repo , ctx . integration . token , 1 )
222
- await stargazersQuery . getSinglePage ( '' )
239
+ await stargazersQuery . getSinglePage ( '' , {
240
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
241
+ integrationId : ctx . integration . id ,
242
+ } )
223
243
repos . push ( repo )
224
244
} catch ( e ) {
225
245
if ( e . rateLimitResetSeconds ) {
@@ -263,7 +283,10 @@ const processRootStream: ProcessStreamHandler = async (ctx) => {
263
283
const processStargazersStream : ProcessStreamHandler = async ( ctx ) => {
264
284
const data = ctx . stream . data as GithubBasicStream
265
285
const stargazersQuery = new StargazersQuery ( data . repo , ctx . integration . token )
266
- const result = await stargazersQuery . getSinglePage ( data . page )
286
+ const result = await stargazersQuery . getSinglePage ( data . page , {
287
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
288
+ integrationId : ctx . integration . id ,
289
+ } )
267
290
result . data = result . data . filter ( ( i ) => ( i as any ) . node ?. login )
268
291
269
292
// handle next page
@@ -285,7 +308,10 @@ const processStargazersStream: ProcessStreamHandler = async (ctx) => {
285
308
const processForksStream : ProcessStreamHandler = async ( ctx ) => {
286
309
const data = ctx . stream . data as GithubBasicStream
287
310
const forksQuery = new ForksQuery ( data . repo , ctx . integration . token )
288
- const result = await forksQuery . getSinglePage ( data . page )
311
+ const result = await forksQuery . getSinglePage ( data . page , {
312
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
313
+ integrationId : ctx . integration . id ,
314
+ } )
289
315
290
316
// filter out activities without authors (such as bots) -- may not the case for forks, but filter out anyway
291
317
result . data = result . data . filter ( ( i ) => ( i as any ) . owner ?. login )
@@ -309,7 +335,10 @@ const processForksStream: ProcessStreamHandler = async (ctx) => {
309
335
const processPullsStream : ProcessStreamHandler = async ( ctx ) => {
310
336
const data = ctx . stream . data as GithubBasicStream
311
337
const forksQuery = new PullRequestsQuery ( data . repo , ctx . integration . token )
312
- const result = await forksQuery . getSinglePage ( data . page )
338
+ const result = await forksQuery . getSinglePage ( data . page , {
339
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
340
+ integrationId : ctx . integration . id ,
341
+ } )
313
342
314
343
// filter out activities without authors (such as bots)
315
344
result . data = result . data . filter ( ( i ) => ( i as any ) . author ?. login )
@@ -484,7 +513,10 @@ const processPullCommentsStream: ProcessStreamHandler = async (ctx) => {
484
513
ctx . integration . token ,
485
514
)
486
515
487
- const result = await pullRequestCommentsQuery . getSinglePage ( data . page )
516
+ const result = await pullRequestCommentsQuery . getSinglePage ( data . page , {
517
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
518
+ integrationId : ctx . integration . id ,
519
+ } )
488
520
result . data = result . data . filter ( ( i ) => ( i as any ) . author ?. login )
489
521
490
522
// handle next page
@@ -513,7 +545,10 @@ const processPullReviewThreadsStream: ProcessStreamHandler = async (ctx) => {
513
545
ctx . integration . token ,
514
546
)
515
547
516
- const result = await pullRequestReviewThreadsQuery . getSinglePage ( data . page )
548
+ const result = await pullRequestReviewThreadsQuery . getSinglePage ( data . page , {
549
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
550
+ integrationId : ctx . integration . id ,
551
+ } )
517
552
518
553
// handle next page
519
554
await publishNextPageStream ( ctx , result )
@@ -541,7 +576,10 @@ const processPullReviewThreadCommentsStream: ProcessStreamHandler = async (ctx)
541
576
ctx . integration . token ,
542
577
)
543
578
544
- const result = await pullRequestReviewThreadCommentsQuery . getSinglePage ( data . page )
579
+ const result = await pullRequestReviewThreadCommentsQuery . getSinglePage ( data . page , {
580
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
581
+ integrationId : ctx . integration . id ,
582
+ } )
545
583
546
584
// filter out activities without authors (such as bots)
547
585
result . data = result . data . filter ( ( i ) => ( i as any ) . author ?. login )
@@ -574,7 +612,10 @@ export const processPullCommitsStream: ProcessStreamHandler = async (ctx) => {
574
612
const pullRequestCommitsQuery = new PullRequestCommitsQuery ( data . repo , pullRequestNumber , token )
575
613
576
614
try {
577
- result = await pullRequestCommitsQuery . getSinglePage ( data . page )
615
+ result = await pullRequestCommitsQuery . getSinglePage ( data . page , {
616
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
617
+ integrationId : ctx . integration . id ,
618
+ } )
578
619
} catch ( err ) {
579
620
ctx . log . warn (
580
621
{
@@ -589,7 +630,10 @@ export const processPullCommitsStream: ProcessStreamHandler = async (ctx) => {
589
630
pullRequestNumber ,
590
631
ctx . integration . token ,
591
632
)
592
- result = await pullRequestCommitsQueryNoAdditions . getSinglePage ( data . page )
633
+ result = await pullRequestCommitsQueryNoAdditions . getSinglePage ( data . page , {
634
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
635
+ integrationId : ctx . integration . id ,
636
+ } )
593
637
}
594
638
595
639
// handle next page
@@ -624,7 +668,10 @@ export const processPullCommitsStream: ProcessStreamHandler = async (ctx) => {
624
668
const processIssuesStream : ProcessStreamHandler = async ( ctx ) => {
625
669
const data = ctx . stream . data as GithubBasicStream
626
670
const issuesQuery = new IssuesQuery ( data . repo , ctx . integration . token )
627
- const result = await issuesQuery . getSinglePage ( data . page )
671
+ const result = await issuesQuery . getSinglePage ( data . page , {
672
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
673
+ integrationId : ctx . integration . id ,
674
+ } )
628
675
629
676
// filter out activities without authors (such as bots)
630
677
result . data = result . data . filter ( ( i ) => ( i as any ) . author ?. login )
@@ -683,7 +730,10 @@ const processIssueCommentsStream: ProcessStreamHandler = async (ctx) => {
683
730
const data = ctx . stream . data as GithubBasicStream
684
731
const issueNumber = data . issueNumber
685
732
const issueCommentsQuery = new IssueCommentsQuery ( data . repo , issueNumber , ctx . integration . token )
686
- const result = await issueCommentsQuery . getSinglePage ( data . page )
733
+ const result = await issueCommentsQuery . getSinglePage ( data . page , {
734
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
735
+ integrationId : ctx . integration . id ,
736
+ } )
687
737
result . data = result . data . filter ( ( i ) => ( i as any ) . author ?. login )
688
738
689
739
// handle next page
@@ -706,7 +756,10 @@ const processIssueCommentsStream: ProcessStreamHandler = async (ctx) => {
706
756
const processDiscussionsStream : ProcessStreamHandler = async ( ctx ) => {
707
757
const data = ctx . stream . data as GithubBasicStream
708
758
const discussionsQuery = new DiscussionsQuery ( data . repo , ctx . integration . token )
709
- const result = await discussionsQuery . getSinglePage ( data . page )
759
+ const result = await discussionsQuery . getSinglePage ( data . page , {
760
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
761
+ integrationId : ctx . integration . id ,
762
+ } )
710
763
711
764
result . data = result . data . filter ( ( i ) => ( i as any ) . author ?. login )
712
765
@@ -746,7 +799,10 @@ const processDiscussionCommentsStream: ProcessStreamHandler = async (ctx) => {
746
799
data . discussionNumber ,
747
800
ctx . integration . token ,
748
801
)
749
- const result = await discussionCommentsQuery . getSinglePage ( data . page )
802
+ const result = await discussionCommentsQuery . getSinglePage ( data . page , {
803
+ concurrentRequestLimiter : getConcurrentRequestLimiter ( ctx ) ,
804
+ integrationId : ctx . integration . id ,
805
+ } )
750
806
result . data = result . data . filter ( ( i ) => ( i as any ) . author ?. login )
751
807
752
808
// handle next page
0 commit comments