Skip to content

Commit 08e72a4

Browse files
committed
fix(cli): preserve permission CSV compatibility and diagnostics
1 parent 57fa18e commit 08e72a4

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎packages/sim-cli/src/commands/source-permissions.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ describe('Search permission CSV upload', () => {
180180
it('stages immutable bounded parts, preserves CSV records, and activates only after both files', async () => {
181181
const requests: { path: string; body: Record<string, unknown> }[] = []
182182
const released: string[] = []
183-
const users = '\ufeffuser_id,email\r\n42,"person@example.com"\r\n'
184-
const grants = `project_path,user_id\n${'team/repo,42\n'.repeat(5001)}`
183+
const users = '\ufeff user_id , email \r\n \t\r\n 42 , "person@example.com" \r\n'
184+
const grants = `project_path,user_id\n${' "team/repo" , 42 \n'.repeat(5001)}`
185185
const result = await runEmbeddedCli(
186186
args,
187187
{
@@ -212,6 +212,7 @@ describe('Search permission CSV upload', () => {
212212
projectPermissions: { rowCount: 5001, parts: 2 },
213213
})
214214
const parts = requests.slice(1, -1)
215+
expect(parts[0].body.content).toBe('user_id,email\n42,person@example.com\n')
215216
expect(parts.map(({ body }) => body.field)).toEqual([
216217
'userMapping',
217218
'projectPermissions',
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { mkdtemp, rm } from 'node:fs/promises'
2+
import { tmpdir } from 'node:os'
3+
import { join } from 'node:path'
4+
import { describe, expect, it, vi } from 'vitest'
5+
import { SimApiError } from '#sim-cli/http/client'
6+
import { stagePermissionCsvs } from '#sim-cli/transfer/permission-csv'
7+
8+
describe('Permission CSV staging errors', () => {
9+
it.each(['input', 'temporary directory'])(
10+
'preserves the cause of a missing %s',
11+
async (missing) => {
12+
const directory = await mkdtemp(join(tmpdir(), 'sim-csv-error-'))
13+
try {
14+
if (missing === 'temporary directory') vi.stubEnv('TMPDIR', join(directory, 'missing'))
15+
const failure = await stagePermissionCsvs('gitlab', {
16+
userMapping: join(directory, 'users.csv'),
17+
projectPermissions: join(directory, 'projects.csv'),
18+
}).catch((error: unknown) => error)
19+
expect(failure).toBeInstanceOf(SimApiError)
20+
expect(failure).toMatchObject({
21+
cause: expect.objectContaining({
22+
code: 'ENOENT',
23+
path: expect.any(String),
24+
syscall: missing === 'input' ? 'open' : 'mkdtemp',
25+
}),
26+
})
27+
} finally {
28+
await rm(directory, { recursive: true, force: true })
29+
}
30+
}
31+
)
32+
})

‎packages/sim-cli/src/transfer/permission-csv.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ export async function stagePermissionCsvs(
5858
signal?: AbortSignal
5959
) {
6060
const directory = await mkdtemp(join(tmpdir(), 'sim-permissions-')).catch((error: unknown) => {
61-
throw new SimApiError(`Cannot stage permission CSVs: ${getErrorMessage(error)}`, 0)
61+
const failure = new SimApiError(`Cannot stage permission CSVs: ${getErrorMessage(error)}`, 0)
62+
failure.cause = error
63+
throw failure
6264
})
6365
const parts: { field: CsvField; path: string }[] = []
6466
const manifest = {} as Record<CsvField, CsvManifest>
@@ -143,7 +145,12 @@ export async function stagePermissionCsvs(
143145
parts.push({ field, path: partPath })
144146
totalBytes += partBytes
145147
}
146-
const parser = parse({ bom: true, skip_empty_lines: true, max_record_size: 8192 })
148+
const parser = parse({
149+
bom: true,
150+
trim: true,
151+
skip_empty_lines: true,
152+
max_record_size: 8192,
153+
})
147154
const decoded = Readable.from(decode())
148155
decoded.on('error', (error) => parser.destroy(error))
149156
decoded.pipe(parser)
@@ -203,8 +210,11 @@ export async function stagePermissionCsvs(
203210
} catch (error) {
204211
await dispose()
205212
if (error instanceof SimApiError) throw error
206-
if (error instanceof Error && 'code' in error && typeof error.code === 'string')
207-
throw new SimApiError(`Cannot stage permission CSVs: ${error.message}`, 0)
213+
if (error instanceof Error && 'code' in error && typeof error.code === 'string') {
214+
const failure = new SimApiError(`Cannot stage permission CSVs: ${error.message}`, 0)
215+
failure.cause = error
216+
throw failure
217+
}
208218
throw error
209219
}
210220
}

0 commit comments

Comments
 (0)