@@ -276,12 +276,14 @@ func orgDedupChecks(session et.Session, obj *dbt.Entity, o *org.Organization) *d
276276 if org , err := orgExistsAndSharesLocEntity (session , obj , o ); err == nil {
277277 result = org
278278 }
279+ if org , err := orgExistsAndSharesAncestorEntity (session , obj , o ); err == nil {
280+ result = org
281+ }
279282 case * org.Organization :
280283 if org , err := orgExistsAndSharesLocEntity (session , obj , o ); err == nil {
281284 result = org
282285 }
283- default :
284- if org , err := orgExistsAndSharesAncestorEntity (session , o ); err == nil {
286+ if org , err := orgExistsAndSharesAncestorEntity (session , obj , o ); err == nil {
285287 result = org
286288 }
287289 }
@@ -372,7 +374,7 @@ func orgExistsAndSharesLocEntity(session et.Session, obj *dbt.Entity, o *org.Org
372374 return nil , errors .New ("no matching org found" )
373375}
374376
375- func orgExistsAndSharesAncestorEntity (session et.Session , o * org.Organization ) (* dbt.Entity , error ) {
377+ func orgExistsAndSharesAncestorEntity (session et.Session , obj * dbt. Entity , o * org.Organization ) (* dbt.Entity , error ) {
376378 var idents []* dbt.Entity
377379
378380 if assets , err := session .Cache ().FindEntitiesByContent (& general.Identifier {
@@ -399,16 +401,49 @@ func orgExistsAndSharesAncestorEntity(session et.Session, o *org.Organization) (
399401 }
400402 }
401403 }
404+ if len (orgents ) == 0 {
405+ return nil , errors .New ("no matching org found" )
406+ }
402407
408+ assets := []* dbt.Entity {obj }
403409 ancestors := make (map [string ]struct {})
404- for _ , orgent := range orgents {
405- if edges , err := session .Cache ().IncomingEdges (orgent , time.Time {}); err == nil {
406- for _ , edge := range edges {
407- if a , err := session .Cache ().FindEntityById (edge .FromEntity .ID ); err == nil && a != nil {
408- if _ , found := ancestors [a .ID ]; ! found {
409- ancestors [a .ID ] = struct {}{}
410- } else {
411- return orgent , nil
410+ for len (assets ) > 0 {
411+ remaining := assets
412+ assets = []* dbt.Entity {}
413+
414+ for _ , r := range remaining {
415+ if edges , err := session .Cache ().IncomingEdges (r , time.Time {}); err == nil {
416+ for _ , edge := range edges {
417+ if a , err := session .Cache ().FindEntityById (edge .FromEntity .ID ); err == nil && a != nil {
418+ if _ , found := ancestors [a .ID ]; ! found {
419+ if _ , conf := session .Scope ().IsAssetInScope (a .Asset , 0 ); conf > 0 {
420+ ancestors [a .ID ] = struct {}{}
421+ assets = append (assets , a )
422+ }
423+ }
424+ }
425+ }
426+ }
427+ }
428+ }
429+
430+ assets = orgents
431+ for len (assets ) > 0 {
432+ remaining := assets
433+ assets = []* dbt.Entity {}
434+
435+ for _ , orgent := range remaining {
436+ if edges , err := session .Cache ().IncomingEdges (orgent , time.Time {}); err == nil {
437+ for _ , edge := range edges {
438+ if a , err := session .Cache ().FindEntityById (edge .FromEntity .ID ); err == nil && a != nil {
439+ if _ , found := ancestors [a .ID ]; ! found {
440+ if _ , conf := session .Scope ().IsAssetInScope (a .Asset , 0 ); conf > 0 {
441+ ancestors [a .ID ] = struct {}{}
442+ assets = append (assets , a )
443+ }
444+ } else {
445+ return orgent , nil
446+ }
412447 }
413448 }
414449 }
0 commit comments