Skip to content

Commit 5f64fc3

Browse files
merllsvcAPLBot
andauthored
fix: fail before writing empty yaml files (#758)
* fix: fail before writing empty yaml files * fix: accept empty object, but do not drop empty workload values * feat: sort in yaml output * chore: linter updates --------- Co-authored-by: svcAPLBot <[email protected]>
1 parent 242b743 commit 5f64fc3

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/git.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { rmSync } from 'fs'
55
import { copy, ensureDir, pathExists, readFile, writeFile } from 'fs-extra'
66
import { unlink } from 'fs/promises'
77
import { glob } from 'glob'
8-
import stringifyJson from 'json-stable-stringify'
98
import jsonpath from 'jsonpath'
109
import { cloneDeep, get, isEmpty, merge, set, unset } from 'lodash'
1110
import { basename, dirname, join } from 'path'
@@ -198,8 +197,7 @@ export class Git {
198197
// ok, write new content
199198
const absolutePath = join(this.path, file)
200199
debug(`Writing to file: ${absolutePath}`)
201-
const sortedData = JSON.parse(stringifyJson(data) as string)
202-
const content = isEmpty(sortedData) ? '' : stringifyYaml(sortedData, undefined, 4)
200+
const content = stringifyYaml(data, undefined, { indent: 4, sortMapEntries: true })
203201
const dir = dirname(absolutePath)
204202
await ensureDir(dir)
205203
await writeFile(absolutePath, content, 'utf8')

src/otomi-stack.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,14 @@ export default class OtomiStack {
340340
cleanSecretPaths.push(p)
341341
} else {
342342
teams.forEach((teamId: string) => {
343-
if (p.indexOf(teamProp) === 0)
343+
if (p.indexOf(teamProp) === 0) {
344344
cleanSecretPaths.push(
345345
p
346346
.replace(teamProp, `teamConfig.${teamId}`)
347347
// add spec to the path for v2 endpoints
348348
.replace(`teamConfig.${teamId}.settings`, `teamConfig.${teamId}.settings.spec`),
349349
)
350+
}
350351
})
351352
}
352353
})
@@ -797,7 +798,7 @@ export default class OtomiStack {
797798
const configKey = this.getConfigKey('AplTeamWorkloadValues')
798799
const repo = this.createTeamConfigInRepo(teamId, configKey, [values])
799800
const fileMap = getFileMaps('').find((fm) => fm.kind === 'AplTeamWorkloadValues')!
800-
await this.git.saveConfig(repo, fileMap)
801+
await this.git.saveConfig(repo, fileMap, false)
801802
}
802803

803804
async saveTeamPolicy(teamId: string, data: AplPolicyResponse): Promise<void> {
@@ -1549,11 +1550,12 @@ export default class OtomiStack {
15491550

15501551
async createAplBuild(teamId: string, data: AplBuildRequest): Promise<AplBuildResponse> {
15511552
const buildName = `${data?.spec?.imageName}-${data?.spec?.tag}`
1552-
if (buildName.length > 128)
1553+
if (buildName.length > 128) {
15531554
throw new HttpError(
15541555
400,
15551556
'Invalid container image name, the combined image name and tag must not exceed 128 characters.',
15561557
)
1558+
}
15571559
try {
15581560
const build = this.repoService.getTeamConfigService(teamId).createBuild(data)
15591561
await this.saveTeamConfigItem(build)
@@ -1566,8 +1568,9 @@ export default class OtomiStack {
15661568
)
15671569
return build
15681570
} catch (err) {
1569-
if (err.code === 409)
1571+
if (err.code === 409) {
15701572
err.publicMessage = 'Container image name already exists, the combined image name and tag must be unique.'
1573+
}
15711574
throw err
15721575
}
15731576
}
@@ -1745,8 +1748,9 @@ export default class OtomiStack {
17451748
fileContent = fileContent.replace(regex, variables[key] as string)
17461749
})
17471750
if (file === 'tty_02_Pod.yaml') fileContent = podContentAddTargetTeam(fileContent)
1748-
if (!sessionUser.isPlatformAdmin && file === 'tty_03_Rolebinding.yaml')
1751+
if (!sessionUser.isPlatformAdmin && file === 'tty_03_Rolebinding.yaml') {
17491752
fileContent = rolebindingContentsForUsers(fileContent)
1753+
}
17501754
return fileContent
17511755
}),
17521756
)
@@ -1774,8 +1778,9 @@ export default class OtomiStack {
17741778
const { sub, isPlatformAdmin, teams } = sessionUser as { sub: string; isPlatformAdmin: boolean; teams: string[] }
17751779
const userTeams = teams.map((teamName) => `team-${teamName}`)
17761780
try {
1777-
if (await checkPodExists('team-admin', `tty-${sessionUser.sub}`))
1781+
if (await checkPodExists('team-admin', `tty-${sessionUser.sub}`)) {
17781782
await k8sdelete({ sub, isPlatformAdmin, userTeams })
1783+
}
17791784
} catch (error) {
17801785
debug('Failed to delete cloudtty')
17811786
}

0 commit comments

Comments
 (0)