-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeleteProxygenDeployments.ts
More file actions
120 lines (106 loc) · 3.74 KB
/
deleteProxygenDeployments.ts
File metadata and controls
120 lines (106 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {LambdaClient} from "@aws-sdk/client-lambda"
import {getCFConfigValue, getCloudFormationExports} from "../config/index.js"
import {invokeLambda} from "./invokeLambda.js"
interface ProxygenInstance {
name: string
}
async function isClosedPullRequest(instanceName: string, apigeeApi: string, repoName: string): Promise<boolean> {
const match = new RegExp(String.raw`^${apigeeApi}-pr-(?<pullRequestId>\d+)$`).exec(instanceName)
if (!match?.groups?.pullRequestId) {
return false
}
const pullRequestId = match.groups.pullRequestId
console.log(`Checking pull request id ${pullRequestId}`)
const url = `https://api.github.com/repos/NHSDigital/${repoName}/pulls/${pullRequestId}`
const headers: Record<string, string> = {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
}
const response = await fetch(url, {headers})
if (!response.ok) {
console.log(`Failed to fetch PR ${pullRequestId}: ${response.status} ${await response.text()}`)
return false
}
const data = (await response.json()) as {state?: string}
if (data.state !== "closed") {
console.log(`not going to delete instance ${instanceName} as PR state is ${data.state}`)
return false
}
console.log(`** going to delete instance ${instanceName} as PR state is ${data.state} **`)
return true
}
async function deleteEnvProxygenDeployments(
apigeeEnvironment: string,
apigeeApi: string,
repoName: string,
proxygenPrivateKeyName: string,
proxygenKid: string
): Promise<void> {
const lambda = new LambdaClient({})
const exports = await getCloudFormationExports()
const proxygenPrivateKeyArn = getCFConfigValue(exports, `secrets-cdk:Secrets:${proxygenPrivateKeyName}:Arn`)
console.log(`Checking Apigee deployments of ${apigeeApi} on ${apigeeEnvironment}`)
const instances = JSON.parse(await invokeLambda(
lambda,
false,
"lambda-resources-ProxygenPTLInstanceGet",
{
apiName: apigeeApi,
environment: apigeeEnvironment,
kid: proxygenKid,
proxygenSecretName: proxygenPrivateKeyArn
}
)) as Array<ProxygenInstance>
for (const instance of instances) {
const name = instance.name
if (!(await isClosedPullRequest(name, apigeeApi, repoName))) {
continue
}
await invokeLambda(
lambda,
false,
"lambda-resources-ProxygenPTLInstanceDelete",
{
apiName: apigeeApi,
environment: apigeeEnvironment,
instance: name,
kid: proxygenKid,
proxygenSecretName: proxygenPrivateKeyArn
}
)
}
}
/**
* Deletes Proxygen PTL deployments for closed pull requests across internal-dev and internal-dev-sandbox.
*
* For each supported Apigee environment, this function queries existing Proxygen instances
* for the given API and deletes those whose instance name corresponds to a closed GitHub PR
* in the specified repository.
*
* @param apigeeApi - The Apigee API name whose Proxygen deployments should be cleaned up.
* @param repoName - The GitHub repository name used to look up pull request state.
* @param proxygenPrivateKeyName - The CloudFormation export key for the Proxygen private key secret.
* @param proxygenKid - The key ID (kid) used when invoking the Proxygen Lambda functions.
* @returns A promise that resolves when all eligible deployments have been processed.
*/
export async function deleteProxygenDeployments(
apigeeApi: string,
repoName: string,
proxygenPrivateKeyName: string,
proxygenKid: string
): Promise<void> {
await deleteEnvProxygenDeployments(
"internal-dev",
apigeeApi,
repoName,
proxygenPrivateKeyName,
proxygenKid
)
await deleteEnvProxygenDeployments(
"internal-dev-sandbox",
apigeeApi,
repoName,
proxygenPrivateKeyName,
proxygenKid
)
}