diff --git a/lib/github.ts b/lib/github.ts index e22637e..8ebf360 100644 --- a/lib/github.ts +++ b/lib/github.ts @@ -156,22 +156,34 @@ function parseLfsPointer(content: string): { oid: string; size: number } | null interface GitContentEntry { content?: string name: string - path: string - sha: string - size: number + path?: string + sha?: string + size?: number type?: string } function isGitContentEntry(value: unknown): value is GitContentEntry { return isRecord(value) && typeof value.name === 'string' - && typeof value.path === 'string' - && typeof value.sha === 'string' - && typeof value.size === 'number' + && (value.path === undefined || typeof value.path === 'string') + && (value.sha === undefined || typeof value.sha === 'string') + && (value.size === undefined || typeof value.size === 'number') && (value.content === undefined || typeof value.content === 'string') && (value.type === undefined || typeof value.type === 'string') } +function getContentEntrySize(entry: GitContentEntry) { + return entry.size ?? 0 +} + +function getRequiredContentEntrySha(entry: GitContentEntry, path: string) { + if (!entry.sha) { + throw new Error(`SHA Git manquant pour ${path}`) + } + + return entry.sha +} + function decodeBase64Content(content: string) { return Buffer.from(content.replace(/\s/g, ''), 'base64').toString('utf-8') } @@ -428,7 +440,7 @@ async function pushAllToGitea( content: file.contentBase64, operation: existing ? 'update' : 'create', path: file.path, - sha: existing?.sha, + sha: existing ? getRequiredContentEntrySha(existing, file.path) : undefined, }) } @@ -441,7 +453,7 @@ async function pushAllToGitea( operations.push({ operation: 'delete', path, - sha: existing.sha, + sha: getRequiredContentEntrySha(existing, path), }) } @@ -480,8 +492,10 @@ export async function getRemoteFolder( throw new Error(`Reponse Git invalide pour ${folderPath}`) } - if (!isLfsFile(f.name) || f.size > 1024) { - return { name: f.name, size: f.size } + const size = getContentEntrySize(f) + + if (!isLfsFile(f.name) || size > 1024) { + return { name: f.name, size } } try { @@ -498,7 +512,7 @@ export async function getRemoteFolder( if (!isHttpError(err) || err.status !== 404) throw err } - return { name: f.name, size: f.size } + return { name: f.name, size } }), )