Skip to content

Commit db4d049

Browse files
Added support for cleaning up arbitrary PR stacks
1 parent fcd9da1 commit db4d049

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

packages/cdkConstructs/src/stacks/deleteUnusedStacks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ async function getHostedZoneInfo(
189189
}
190190

191191
async function isClosedPullRequest(stackName: string, baseStackName: string, repoName: string): Promise<boolean> {
192-
const match = new RegExp(String.raw`^${baseStackName}-pr-(?<pullRequestId>\d+)(-sandbox)?$`).exec(stackName)
192+
const match = new RegExp(String.raw`^${baseStackName}-pr-(?<pullRequestId>\d+)(-[\w-]+)?$`).exec(stackName)
193193
if (!match?.groups?.pullRequestId) {
194194
return false
195195
}

packages/cdkConstructs/tests/stacks/deleteUnusedStacks.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,48 @@ describe("stack deletion", () => {
858858
})
859859
})
860860

861+
test("deletes PR stacks with multiple suffixes", async () => {
862+
const now = new Date()
863+
const twoDaysAgo = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000)
864+
865+
mockListStacksSend.mockReturnValue({
866+
StackSummaries: [
867+
{
868+
StackName: `${baseStackName}-pr-123-sandbox`,
869+
StackStatus: "CREATE_COMPLETE",
870+
CreationTime: twoDaysAgo
871+
},
872+
{
873+
StackName: `${baseStackName}-pr-123-front-door`,
874+
StackStatus: "CREATE_COMPLETE",
875+
CreationTime: twoDaysAgo
876+
},
877+
{
878+
StackName: `${baseStackName}-pr-123-stateful`,
879+
StackStatus: "CREATE_COMPLETE",
880+
CreationTime: twoDaysAgo
881+
}
882+
]
883+
})
884+
885+
mockGetPRState.mockImplementation((url: string) => {
886+
if (url.endsWith("/repos/NHSDigital/eps-cdk-utils/pulls/123")) {
887+
return "closed"
888+
}
889+
throw new Error(`Unexpected URL: ${url}`)
890+
})
891+
892+
const promise = deleteUnusedPrStacks(baseStackName, repoName, hostedZoneName)
893+
await vi.runAllTimersAsync()
894+
await promise
895+
896+
// One delete stack call for the PR stack
897+
expect(mockDeleteStackSend).toHaveBeenCalledTimes(3)
898+
expect(mockDeleteStackSend).toHaveBeenCalledWith({StackName: `${baseStackName}-pr-123-sandbox`})
899+
expect(mockDeleteStackSend).toHaveBeenCalledWith({StackName: `${baseStackName}-pr-123-front-door`})
900+
expect(mockDeleteStackSend).toHaveBeenCalledWith({StackName: `${baseStackName}-pr-123-stateful`})
901+
})
902+
861903
test("does not delete open PR stacks", async () => {
862904
const now = new Date()
863905
const twoDaysAgo = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000)

0 commit comments

Comments
 (0)