33 PullRequestOpenedEvent ,
44 PullRequestSynchronizeEvent ,
55} from "@octokit/webhooks-types" ;
6- import { PullRequestState } from "@prisma/client" ;
6+ import { PullRequest , PullRequestState } from "@prisma/client" ;
77import { Job } from "bullmq" ;
88import { addJob , JobPriority , SweetQueue } from "../../../bull-mq/queues" ;
99import { createWorker } from "../../../bull-mq/workers" ;
@@ -15,19 +15,17 @@ import {
1515} from "../../sync-batch/services/sync-batch.service" ;
1616import { syncPullRequest } from "../services/github-pull-request.service" ;
1717import { withDelayedRetryOnRateLimit } from "../services/github-rate-limit.service" ;
18+ import { DataIntegrityException } from "../../errors/exceptions/data-integrity.exception" ;
19+
20+ type JobData = (
21+ | PullRequestSynchronizeEvent
22+ | PullRequestOpenedEvent
23+ | PullRequestClosedEvent
24+ ) & { syncReviews ?: boolean ; syncBatchId ?: number } ;
1825
1926export const syncPullRequestWorker = createWorker (
2027 SweetQueue . GITHUB_SYNC_PULL_REQUEST ,
21- async (
22- job : Job <
23- (
24- | PullRequestSynchronizeEvent
25- | PullRequestOpenedEvent
26- | PullRequestClosedEvent
27- ) & { syncReviews ?: boolean ; syncBatchId ?: number }
28- > ,
29- token ?: string
30- ) => {
28+ async ( job : Job < JobData > , token ?: string ) => {
3129 if ( ! job . data . installation ?. id ) {
3230 throw new InputValidationException (
3331 "Received Pull Request webhook without installation" ,
@@ -66,32 +64,54 @@ export const syncPullRequestWorker = createWorker(
6664 ) ;
6765
6866 if ( pullRequest ) {
69- if ( job . data . syncReviews ) {
70- logger . debug ( "syncPullRequest: Adding job to sync reviews" , {
71- pullRequest,
72- } ) ;
73-
74- await addJob (
75- SweetQueue . GITHUB_SYNC_CODE_REVIEW ,
76- {
77- pull_request : { node_id : pullRequest . gitPullRequestId } ,
78- installation : { id : installationId } ,
79- } ,
80- { priority : JobPriority . LOW }
81- ) ;
82- }
83-
84- await addJob ( SweetQueue . AUTOMATION_PR_SIZE_LABELER , job . data ) ;
85-
86- if ( pullRequest . state === PullRequestState . MERGED ) {
87- await addJob ( SweetQueue . ALERT_MERGED_WITHOUT_APPROVAL , job . data ) ;
88- await addJob ( SweetQueue . DEPLOYMENT_TRIGGERED_BY_PULL_REQUEST_MERGE , {
89- workspaceId : pullRequest . workspaceId ,
90- pullRequestId : pullRequest . id ,
91- installationId,
92- } ) ;
93- }
67+ await handlePostSyncActions ( job . data , pullRequest ) ;
9468 }
9569 } ,
9670 { limiter : { max : 8 , duration : 1000 } }
9771) ;
72+
73+ const handlePostSyncActions = async (
74+ jobData : JobData ,
75+ pullRequest : PullRequest
76+ ) => {
77+ const installationId = jobData . installation ?. id ;
78+ const syncBatchId = jobData . syncBatchId ;
79+
80+ if ( ! installationId ) {
81+ throw new DataIntegrityException (
82+ "handlePostSyncActions: Missing installation ID" ,
83+ { extra : { jobData, pullRequest } }
84+ ) ;
85+ }
86+
87+ if ( jobData . syncReviews ) {
88+ logger . debug ( "syncPullRequest: Adding job to sync reviews" , {
89+ pullRequest,
90+ } ) ;
91+
92+ await addJob (
93+ SweetQueue . GITHUB_SYNC_CODE_REVIEW ,
94+ {
95+ pull_request : { node_id : pullRequest . gitPullRequestId } ,
96+ installation : { id : installationId } ,
97+ } ,
98+ { priority : JobPriority . LOW }
99+ ) ;
100+ }
101+
102+ if ( ! syncBatchId ) {
103+ await addJob ( SweetQueue . AUTOMATION_PR_SIZE_LABELER , jobData ) ;
104+ }
105+
106+ if ( pullRequest . state === PullRequestState . MERGED ) {
107+ if ( ! syncBatchId ) {
108+ await addJob ( SweetQueue . ALERT_MERGED_WITHOUT_APPROVAL , jobData ) ;
109+ }
110+
111+ await addJob ( SweetQueue . DEPLOYMENT_TRIGGERED_BY_PULL_REQUEST_MERGE , {
112+ workspaceId : pullRequest . workspaceId ,
113+ pullRequestId : pullRequest . id ,
114+ installationId,
115+ } ) ;
116+ }
117+ } ;
0 commit comments