-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5df518
commit 65c0d5f
Showing
6 changed files
with
155 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/graph-core/src/edges/cloudFormation/detectCircularDependencies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { analyzeGraph } from 'graph-cycles'; | ||
|
||
import { CloudformationStackARN } from '@sls-mentor/arn'; | ||
import { findStacksToStacksImports } from '@sls-mentor/aws-api'; | ||
|
||
import { CircularDependenciesWarnings, CloudFormationWarnings } from 'types'; | ||
|
||
type LinkBetweenStacks = { | ||
exportingStack?: CloudformationStackARN; | ||
importingStack?: CloudformationStackARN; | ||
}[]; | ||
|
||
type Graph = { | ||
[key: string]: string[]; | ||
}; | ||
|
||
const detectCircularDependencies = ( | ||
stacksImports: LinkBetweenStacks, | ||
): string[][] | null => { | ||
const graph: Graph = stacksImports.reduce( | ||
(acc: Graph, { exportingStack, importingStack }) => { | ||
if (exportingStack !== undefined && importingStack !== undefined) { | ||
const exportingStackName = exportingStack.toString(); | ||
const importingStackName = importingStack.toString(); | ||
|
||
if (acc[exportingStackName] === undefined) { | ||
acc[exportingStackName] = []; | ||
} | ||
|
||
acc[exportingStackName]?.push(importingStackName); | ||
} | ||
|
||
return acc; | ||
}, | ||
{}, | ||
); | ||
|
||
const { cycles } = analyzeGraph(Object.entries(graph)); | ||
|
||
return cycles; | ||
}; | ||
|
||
type StacksToStacksImportsWithWarnings = { | ||
exportingStack?: CloudformationStackARN; | ||
importingStack?: CloudformationStackARN; | ||
warnings: CircularDependenciesWarnings[]; | ||
}[]; | ||
|
||
export const findStacksToStacksImportsWithCircularDependencies = | ||
async (): Promise<StacksToStacksImportsWithWarnings> => { | ||
const stacksToStacksImports = await findStacksToStacksImports(); | ||
const circularDependencies = detectCircularDependencies( | ||
stacksToStacksImports, | ||
); | ||
|
||
const dictOfCircularDependencies: Record<string, boolean> = {}; | ||
|
||
if (circularDependencies !== null) { | ||
circularDependencies.forEach(([from, to]) => { | ||
if (to === undefined) { | ||
dictOfCircularDependencies[`${from ?? ''}-${from ?? ''}`] = true; | ||
} else if (from === undefined) { | ||
throw new Error('This should not happen'); | ||
} else { | ||
dictOfCircularDependencies[`${from}-${to}`] = true; | ||
dictOfCircularDependencies[`${to}-${from}`] = true; | ||
} | ||
}); | ||
} | ||
|
||
return stacksToStacksImports.map(({ exportingStack, importingStack }) => { | ||
if ( | ||
dictOfCircularDependencies[ | ||
`${exportingStack?.toString() ?? ''}-${ | ||
importingStack?.toString() ?? '' | ||
}` | ||
] === true | ||
) { | ||
return { | ||
exportingStack, | ||
importingStack, | ||
warnings: [CloudFormationWarnings.CircularDependencies], | ||
}; | ||
} | ||
|
||
return { | ||
exportingStack, | ||
importingStack, | ||
warnings: [], | ||
}; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './detectCircularDependencies'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.