@@ -12,6 +12,8 @@ const mocks = vi.hoisted(() => ({
1212 queueAdd : vi . fn ( ) ,
1313 verify : vi . fn ( ) ,
1414 shouldDeploy : vi . fn ( ) ,
15+ createPreviewDeployment : vi . fn ( ) ,
16+ findPreviewDeploymentByApplicationId : vi . fn ( ) ,
1517} ) ) ;
1618
1719vi . mock ( "drizzle-orm" , ( ) => ( {
@@ -64,10 +66,11 @@ vi.mock("@dokploy/server", () => ({
6466 IS_CLOUD : false ,
6567 shouldDeploy : mocks . shouldDeploy ,
6668 checkUserRepositoryPermissions : vi . fn ( ) ,
67- createPreviewDeployment : vi . fn ( ) ,
69+ createPreviewDeployment : mocks . createPreviewDeployment ,
6870 createSecurityBlockedComment : vi . fn ( ) ,
6971 findGithubById : vi . fn ( ) ,
70- findPreviewDeploymentByApplicationId : vi . fn ( ) ,
72+ findPreviewDeploymentByApplicationId :
73+ mocks . findPreviewDeploymentByApplicationId ,
7174 findPreviewDeploymentsByPullRequestId : vi . fn ( ) ,
7275 getBitbucketHeaders : vi . fn ( ( ) => ( { } ) ) ,
7376 removePreviewDeployment : vi . fn ( ) ,
@@ -321,3 +324,157 @@ describe("GitHub app webhook auto-deploy", () => {
321324 expect ( res . json ) . toHaveBeenCalledWith ( { message : "No apps to deploy" } ) ;
322325 } ) ;
323326} ) ;
327+
328+ describe ( "GitHub app webhook preview deployments" , ( ) => {
329+ const createApplication = (
330+ overrides : Record < string , unknown > = { } ,
331+ ) : Record < string , unknown > => ( {
332+ applicationId : "application-id" ,
333+ name : "my-app" ,
334+ serverId : null ,
335+ previewLabels : [ ] ,
336+ previewLimit : 3 ,
337+ previewDeployments : [ ] ,
338+ previewRequireCollaboratorPermissions : false ,
339+ ...overrides ,
340+ } ) ;
341+
342+ const createPreviewDeployments = ( total : number ) =>
343+ Array . from ( { length : total } , ( _ , index ) => ( {
344+ previewDeploymentId : `existing-preview-${ index } ` ,
345+ } ) ) ;
346+
347+ const createPullRequestRequest = ( action : string ) =>
348+ ( {
349+ headers : {
350+ "x-hub-signature-256" : "sha256=test-signature" ,
351+ "x-github-event" : "pull_request" ,
352+ } ,
353+ body : {
354+ installation : {
355+ id : 12345 ,
356+ } ,
357+ action,
358+ pull_request : {
359+ id : 987 ,
360+ number : 42 ,
361+ title : "feat: add preview" ,
362+ html_url : "https://github.com/agentHits/dokploy/pull/42" ,
363+ labels : [ ] ,
364+ user : {
365+ login : "agentHits" ,
366+ } ,
367+ head : {
368+ ref : "feature" ,
369+ sha : "abc123" ,
370+ } ,
371+ base : {
372+ ref : "main" ,
373+ } ,
374+ } ,
375+ repository : {
376+ name : "dokploy" ,
377+ owner : {
378+ login : "agentHits" ,
379+ } ,
380+ } ,
381+ } ,
382+ } ) as unknown as NextApiRequest ;
383+
384+ beforeEach ( ( ) => {
385+ vi . clearAllMocks ( ) ;
386+ mocks . githubFindFirst . mockResolvedValue ( {
387+ githubId : "github-provider-id" ,
388+ githubInstallationId : 12345 ,
389+ githubWebhookSecret : "webhook-secret" ,
390+ } ) ;
391+ mocks . verify . mockResolvedValue ( true ) ;
392+ mocks . queueAdd . mockResolvedValue ( { id : "job-id" } ) ;
393+ mocks . createPreviewDeployment . mockResolvedValue ( {
394+ previewDeploymentId : "new-preview-id" ,
395+ } ) ;
396+ mocks . findPreviewDeploymentByApplicationId . mockResolvedValue ( undefined ) ;
397+ } ) ;
398+
399+ it ( "redeploys an existing preview even when the limit is reached" , async ( ) => {
400+ mocks . applicationsFindMany . mockResolvedValue ( [
401+ createApplication ( {
402+ previewLimit : 2 ,
403+ previewDeployments : createPreviewDeployments ( 3 ) ,
404+ } ) ,
405+ ] ) ;
406+ mocks . findPreviewDeploymentByApplicationId . mockResolvedValue ( {
407+ previewDeploymentId : "existing-preview-0" ,
408+ } ) ;
409+ const res = createResponse ( ) ;
410+
411+ await handler ( createPullRequestRequest ( "synchronize" ) , res ) ;
412+
413+ expect ( mocks . createPreviewDeployment ) . not . toHaveBeenCalled ( ) ;
414+ expect ( mocks . queueAdd ) . toHaveBeenCalledWith (
415+ "deployments" ,
416+ expect . objectContaining ( {
417+ applicationId : "application-id" ,
418+ applicationType : "application-preview" ,
419+ previewDeploymentId : "existing-preview-0" ,
420+ type : "deploy" ,
421+ } ) ,
422+ expect . objectContaining ( {
423+ removeOnComplete : true ,
424+ removeOnFail : true ,
425+ } ) ,
426+ ) ;
427+ expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
428+ } ) ;
429+
430+ it ( "does not create a new preview once the limit is reached" , async ( ) => {
431+ mocks . applicationsFindMany . mockResolvedValue ( [
432+ createApplication ( {
433+ previewLimit : 2 ,
434+ previewDeployments : createPreviewDeployments ( 2 ) ,
435+ } ) ,
436+ ] ) ;
437+ const res = createResponse ( ) ;
438+
439+ await handler ( createPullRequestRequest ( "opened" ) , res ) ;
440+
441+ expect ( mocks . createPreviewDeployment ) . not . toHaveBeenCalled ( ) ;
442+ expect ( mocks . queueAdd ) . not . toHaveBeenCalled ( ) ;
443+ expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
444+ } ) ;
445+
446+ it ( "falls back to the default limit when none is configured" , async ( ) => {
447+ mocks . applicationsFindMany . mockResolvedValue ( [
448+ createApplication ( {
449+ previewLimit : null ,
450+ previewDeployments : createPreviewDeployments ( 2 ) ,
451+ } ) ,
452+ ] ) ;
453+ const res = createResponse ( ) ;
454+
455+ await handler ( createPullRequestRequest ( "opened" ) , res ) ;
456+
457+ expect ( mocks . createPreviewDeployment ) . toHaveBeenCalledWith (
458+ expect . objectContaining ( {
459+ applicationId : "application-id" ,
460+ branch : "feature" ,
461+ pullRequestId : 987 ,
462+ pullRequestNumber : 42 ,
463+ } ) ,
464+ ) ;
465+ expect ( mocks . queueAdd ) . toHaveBeenCalledWith (
466+ "deployments" ,
467+ expect . objectContaining ( {
468+ applicationId : "application-id" ,
469+ applicationType : "application-preview" ,
470+ previewDeploymentId : "new-preview-id" ,
471+ type : "deploy" ,
472+ } ) ,
473+ expect . objectContaining ( {
474+ removeOnComplete : true ,
475+ removeOnFail : true ,
476+ } ) ,
477+ ) ;
478+ expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
479+ } ) ;
480+ } ) ;
0 commit comments