@@ -22,6 +22,8 @@ import {
2222 Repository ,
2323 RepositoryInfo ,
2424 WorkspaceConfig ,
25+ PrebuiltWorkspaceState ,
26+ PrebuiltWorkspace ,
2527} from "@gitpod/gitpod-protocol" ;
2628import * as chai from "chai" ;
2729import { Container } from "inversify" ;
@@ -549,16 +551,22 @@ describe("ContextService", async () => {
549551 commitMessage : `commit 69420` ,
550552 } ;
551553 const commit2 = {
552- sha : "69421" ,
554+ sha : "69422" ,
555+ author : "some-dude" ,
556+ commitMessage : `commit 69422` ,
557+ } ;
558+ const commit3 = {
559+ sha : "69423" ,
553560 author : "some-other-dude" ,
554- commitMessage : "commit 69421 " ,
561+ commitMessage : "commit 69423 " ,
555562 } ;
556563 const branchName = "branch-2" ;
557564 mockRepositoryProvider . addBranch (
558565 { name : branchName , htmlUrl : `https://github.com/gitpod-io/empty/tree/${ branchName } ` } ,
559566 [ commit1 ] ,
560567 ) ;
561568 mockRepositoryProvider . pushCommit ( branchName , commit2 ) ;
569+ mockRepositoryProvider . pushCommit ( branchName , commit3 ) ;
562570
563571 // request context for both commits separately
564572 const svc = container . get ( ContextService ) ;
@@ -567,64 +575,85 @@ describe("ContextService", async () => {
567575 organizationId : org . id ,
568576 forceDefaultConfig : false ,
569577 } ) ;
570- let ctx2 = await svc . parseContext ( owner , `https://github.com/gitpod-io/empty/commit/${ commit2 . sha } ` , {
578+ const ctx2 = await svc . parseContext ( owner , `https://github.com/gitpod-io/empty/commit/${ commit2 . sha } ` , {
579+ projectId : project . id ,
580+ organizationId : org . id ,
581+ forceDefaultConfig : false ,
582+ } ) ;
583+ let ctx3 = await svc . parseContext ( owner , `https://github.com/gitpod-io/empty/commit/${ commit3 . sha } ` , {
571584 projectId : project . id ,
572585 organizationId : org . id ,
573586 forceDefaultConfig : false ,
574587 } ) ;
575588
576- // trigger and "await" prebuilds for both commits in push order
589+ // trigger and "await" prebuilds for both commits in crazy order
577590 const prebuildManager = container . get ( PrebuildManager ) ;
578591 const workspaceDb : WorkspaceDB = container . get ( WorkspaceDB ) ;
579- const prebuildForCommit2 = await prebuildManager . startPrebuild (
580- { } ,
581- { user : owner , project, commitInfo : commit2 , context : ctx2 . context as CommitContext } ,
582- ) ;
583- const prebuild1 = await workspaceDb . findPrebuildByID ( prebuildForCommit2 . prebuildId ) ;
584- await workspaceDb . storePrebuiltWorkspace ( {
585- ...prebuild1 ! ,
586- state : "available" ,
587- } ) ;
588- const wsAndI1 = await workspaceDb . findWorkspaceAndInstance ( prebuild1 ! . buildWorkspaceId ) ;
589- await workspaceDb . updateInstancePartial ( wsAndI1 ! . instanceId , { status : { phase : "stopped" } } ) ;
590592
591- const prebuildForCommit1 = await prebuildManager . startPrebuild (
592- { } ,
593- { user : owner , project, commitInfo : commit1 , context : ctx1 . context as CommitContext } ,
594- ) ;
595- const prebuild2 = await workspaceDb . findPrebuildByID ( prebuildForCommit1 . prebuildId ) ;
596- await workspaceDb . storePrebuiltWorkspace ( {
597- ...prebuild2 ! ,
598- state : "available" ,
599- } ) ;
600- const wsAndI2 = await workspaceDb . findWorkspaceAndInstance ( prebuild2 ! . buildWorkspaceId ) ;
601- await workspaceDb . updateInstancePartial ( wsAndI2 ! . instanceId , { status : { phase : "stopped" } } ) ;
593+ async function runPrebuild (
594+ commitInfo : CommitInfo ,
595+ context : CommitContext ,
596+ state : PrebuiltWorkspaceState ,
597+ ) : Promise < PrebuiltWorkspace > {
598+ const prebuildResult = await prebuildManager . startPrebuild (
599+ { } ,
600+ { user : owner , project, commitInfo, context } ,
601+ ) ;
602+ const prebuild = await workspaceDb . findPrebuildByID ( prebuildResult . prebuildId ) ;
603+ await workspaceDb . storePrebuiltWorkspace ( {
604+ ...prebuild ! ,
605+ state,
606+ } ) ;
607+ const wsAndI = await workspaceDb . findWorkspaceAndInstance ( prebuild ! . buildWorkspaceId ) ;
608+ await workspaceDb . updateInstancePartial ( wsAndI ! . instanceId , { status : { phase : "stopped" } } ) ;
609+
610+ return prebuild ! ;
611+ }
602612
603- // after everything has settled, request context for both commits again
613+ const prebuild3 = await runPrebuild ( commit3 , ctx3 . context as CommitContext , "available" ) ;
614+ const prebuild1 = await runPrebuild ( commit1 , ctx1 . context as CommitContext , "available" ) ;
615+ await runPrebuild ( commit2 , ctx2 . context as CommitContext , "available" ) ;
616+
617+ // should point to commit1, as
604618 ctx1 = await svc . parseContext ( owner , `https://github.com/gitpod-io/empty/commit/${ commit1 . sha } ` , {
605619 projectId : project . id ,
606620 organizationId : org . id ,
607621 forceDefaultConfig : false ,
608622 } ) ;
609- ctx2 = await svc . parseContext ( owner , `https://github.com/gitpod-io/empty/commit/${ commit2 . sha } ` , {
623+ ctx3 = await svc . parseContext ( owner , `https://github.com/gitpod-io/empty/commit/${ commit3 . sha } ` , {
624+ projectId : project . id ,
625+ organizationId : org . id ,
626+ forceDefaultConfig : false ,
627+ } ) ;
628+ const ctxBranch = await svc . parseContext ( owner , `https://github.com/gitpod-io/empty/tree/branch-2` , {
610629 projectId : project . id ,
611630 organizationId : org . id ,
612631 forceDefaultConfig : false ,
613632 } ) ;
614633
615634 expect ( ctx1 . project ?. id ) . to . equal ( project . id ) ;
616635 expect ( PrebuiltWorkspaceContext . is ( ctx1 . context ) ) . to . equal ( true ) ;
617- expect ( ( ctx1 . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . id ) . to . equal (
618- prebuildForCommit1 . prebuildId ,
619- ) ;
620- expect ( ( ctx1 . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . commit ) . to . equal ( commit1 . sha ) ;
621-
622- expect ( ctx2 . project ?. id ) . to . equal ( project . id ) ;
623- expect ( PrebuiltWorkspaceContext . is ( ctx2 . context ) ) . to . equal ( true ) ;
624- expect ( ( ctx2 . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . id ) . to . equal (
625- prebuildForCommit2 . prebuildId ,
626- ) ;
627- expect ( ( ctx2 . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . commit ) . to . equal ( commit2 . sha ) ;
636+ expect ( ( ctx1 . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . id ) . to . equal ( prebuild1 . id ) ;
637+ expect (
638+ ( ctx1 . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . commit ,
639+ "should point to commit1, ignoring others due to history" ,
640+ ) . to . equal ( commit1 . sha ) ;
641+
642+ expect ( ctx3 . project ?. id ) . to . equal ( project . id ) ;
643+ expect ( PrebuiltWorkspaceContext . is ( ctx3 . context ) ) . to . equal ( true ) ;
644+ expect ( ( ctx3 . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . id ) . to . equal ( prebuild3 . id ) ;
645+ expect (
646+ ( ctx3 . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . commit ,
647+ "should point to commit3, ignoring more recent prebuilds (1 + 2)" ,
648+ ) . to . equal ( commit3 . sha ) ;
649+
650+ expect ( ctxBranch . project ?. id ) . to . equal ( project . id ) ;
651+ expect ( PrebuiltWorkspaceContext . is ( ctxBranch . context ) ) . to . equal ( true ) ;
652+ expect ( ( ctxBranch . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . id ) . to . equal ( prebuild3 . id ) ;
653+ expect (
654+ ( ctxBranch . context as any as PrebuiltWorkspaceContext ) . prebuiltWorkspace . commit ,
655+ "should point to commit3, ingoring more the more recent incremental match prebuild2" ,
656+ ) . to . equal ( commit3 . sha ) ;
628657 } ) ;
629658
630659 it ( "should parse snapshot context" , async ( ) => {
0 commit comments