-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
217 lines (174 loc) · 7.18 KB
/
index.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const Base64 = require("js-base64").Base64;
const { AWS_ECR_URL, closePRs, createPR, getContents, getHeadSha } = require("./github")
// Images to update ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const PROJECTS = [
{
repoName: "notification-api",
helmfileOverride: "helmfile/overrides/production.env",
helmfileTagKey: "API_DOCKER_TAG",
ecrUrl: AWS_ECR_URL,
ecrName: "notify-api",
},
{
repoName: "notification-admin",
helmfileOverride: "helmfile/overrides/production.env",
helmfileTagKey: "ADMIN_DOCKER_TAG",
ecrUrl: AWS_ECR_URL,
ecrName: "notify-admin",
},
{
repoName: "notification-document-download-api",
helmfileOverride: "helmfile/overrides/production.env",
helmfileTagKey: "DOCUMENT_DOWNLOAD_DOCKER_TAG",
ecrUrl: AWS_ECR_URL,
ecrName: "notify-document-download-api",
},
{
repoName: "notification-documentation",
helmfileOverride: "helmfile/overrides/production.env",
helmfileTagKey: "DOCUMENTATION_DOCKER_TAG",
ecrUrl: AWS_ECR_URL,
ecrName: "notify-documentation",
},
];
const PROJECTS_LAMBDAS = [
{
repoName: "notification-api",
manifestFile: ".github/workflows/merge_to_main_production.yaml",
ecrUrl: "${PRODUCTION_ECR_ACCOUNT}.dkr.ecr.ca-central-1.amazonaws.com/notify",
ecrName: "api-lambda",
},
// {
// repoName: "notification-lambdas",
// manifestFile: ".github/workflows/merge_to_main_production.yaml",
// ecrUrl: "${PRODUCTION_ECR_ACCOUNT}.dkr.ecr.ca-central-1.amazonaws.com/notify",
// ecrName: "system_status",
// },
// {
// repoName: "notification-lambdas",
// manifestFile: ".github/workflows/merge_to_main_production.yaml",
// ecrUrl: "${PRODUCTION_ECR_ACCOUNT}.dkr.ecr.ca-central-1.amazonaws.com/notify",
// ecrName: "heartbeat",
// },
]
// Shas ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function shortSha(fullSha) {
return fullSha.slice(0, 7);
}
function getLatestImageUrl(projects, projectName, headSha) {
const ecrUrl = projects.filter(project => project["ecrName"] == projectName)[0].ecrUrl
return `${ecrUrl}/${projectName}:${shortSha(headSha)}`;
}
function getSha(project, content) {
const helmfileRe = new RegExp(`${project.helmfileTagKey}: "(.*?)"`, "g")
const result = content.match(helmfileRe)[0]
const tagShaRe = new RegExp(`"(.*?)"`, "g")
var tag = result.match(tagShaRe)[0]
tag = tag.replaceAll("\"","");
return tag;
}
function getLambdaSha(imageName) {
return imageName.split(":").slice(-1)[0];
}
async function hydrateWithSHAs(projects) {
return await Promise.all(
projects.map(async (project) => {
project.headSha = await getHeadSha(project.repoName);
project.shortSha = shortSha(project.headSha)
project.headUrl = getLatestImageUrl(
projects,
project.ecrName,
project.headSha
);
// Patch the helmfile tags
const helmfileContent = await getContents(
"notification-manifests",
project.helmfileOverride
);
const helmfileContents = Base64.decode(helmfileContent.content)
project.oldSha = getSha(project, helmfileContents);
return project;
})
);
}
async function hydrateLambdasWithSHAs(projects) {
return await Promise.all(
projects.map(async (project) => {
project.headSha = await getHeadSha(project.repoName);
project.shortSha = shortSha(project.headSha)
project.headUrl = getLatestImageUrl(
projects,
project.ecrName,
project.headSha
);
const releaseContent = await getContents(
"notification-manifests",
project.manifestFile
);
const originalFileContents = Base64.decode(releaseContent.content)
const re = new RegExp(`${project.ecrName}:\\S*`, "g");
project.oldUrl = originalFileContents.match(re)[0]
project.oldSha = getLambdaSha(project.oldUrl);
return project;
})
);
}
function updateHelmfileSha(content,project) {
let re = new RegExp(String.raw`${project.helmfileTagKey}: "(.*?)"`, "g");
return content.replace(re, `${project.helmfileTagKey}: "${shortSha(project.headSha)}"`);
}
function updateLambdaSha(content, project) {
return content.replace(`${project.ecrName}:${project.oldSha}`, `${project.ecrName}:${shortSha(project.headSha)}`)
}
// HELMFILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
async function main(closePRsFirst, titlePrefix, projects, projects_lambdas) {
const prTemplate = await getContents(
"notification-manifests",
".github/PULL_REQUEST_TEMPLATE.md"
);
const issueContent = Base64.decode(prTemplate.content);
await hydrateWithSHAs(projects);
await hydrateLambdasWithSHAs(projects_lambdas);
const reducer = (previous, project) => ({ ...previous, [project.helmfileOverride]: (previous[project.helmfileOverride] || []).concat(project) })
const projectsForFiles = projects.reduce(reducer, {})
var changesToHelmfile = Object.entries(projectsForFiles).map(async ([helmfileOverride, projectsForFile]) => {
const releaseContent = await getContents(
"notification-manifests",
helmfileOverride
);
var fileContents = Base64.decode(releaseContent.content)
projectsForFile.forEach((project) => {
fileContents = updateHelmfileSha(fileContents, project)
})
const newReleaseContentBlob = Base64.encode(fileContents);
const fileHasChanged = newReleaseContentBlob.trim() != releaseContent.content.trim()
return { helmfileOverride, newReleaseContentBlob, releaseContent, fileHasChanged }
})
const lambdaReducer = (previous, project) => ({ ...previous, [project.manifestFile]: (previous[project.manifestFile] || []).concat(project) })
const lambdaProjectsForFiles = projects_lambdas.reduce(lambdaReducer, {})
var changesToLambdaFiles = Object.entries(lambdaProjectsForFiles).map(async ([manifestFile, projectsForFile]) => {
const releaseContent = await getContents(
"notification-manifests",
manifestFile
);
var fileContents = Base64.decode(releaseContent.content)
projectsForFile.forEach((project) => {
fileContents = updateLambdaSha(fileContents, project)
})
const newReleaseContentBlob = Base64.encode(fileContents);
const fileHasChanged = newReleaseContentBlob.trim() != releaseContent.content.trim()
return { manifestFile, newReleaseContentBlob, releaseContent, fileHasChanged }
})
changesToHelmfile = await Promise.all(changesToHelmfile)
changesToLambdaFiles = await Promise.all(changesToLambdaFiles)
const helmFilesHaveChanged = changesToHelmfile.some(({ fileHasChanged }) => fileHasChanged)
const lambdaFilesHaveChanged = changesToLambdaFiles.some(({ fileHasChanged }) => fileHasChanged)
if (helmFilesHaveChanged || lambdaFilesHaveChanged) {
if (closePRsFirst) {
await closePRs(titlePrefix);
}
await createPR(titlePrefix, projects, projects_lambdas, issueContent, changesToHelmfile, changesToLambdaFiles);
}
}
// Main execute ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main(true, "[AUTO-PR]", PROJECTS, PROJECTS_LAMBDAS);