( state: SyncState, repoSlug: string, entries: Record<string, string>, ifMatchChecksum?: string | null, )
| 460 | } |
| 461 | |
| 462 | async function uploadTeamMemory( |
| 463 | state: SyncState, |
| 464 | repoSlug: string, |
| 465 | entries: Record<string, string>, |
| 466 | ifMatchChecksum?: string | null, |
| 467 | ): Promise<TeamMemorySyncUploadResult> { |
| 468 | try { |
| 469 | await checkAndRefreshOAuthTokenIfNeeded() |
| 470 | |
| 471 | const auth = getAuthHeaders() |
| 472 | if (auth.error) { |
| 473 | return { success: false, error: auth.error, errorType: 'auth' } |
| 474 | } |
| 475 | |
| 476 | const headers: Record<string, string> = { |
| 477 | ...auth.headers, |
| 478 | 'Content-Type': 'application/json', |
| 479 | } |
| 480 | if (ifMatchChecksum) { |
| 481 | headers['If-Match'] = `"${ifMatchChecksum.replace(/"/g, '')}"` |
| 482 | } |
| 483 | |
| 484 | const endpoint = getTeamMemorySyncEndpoint(repoSlug) |
| 485 | const response = await axios.put( |
| 486 | endpoint, |
| 487 | { entries }, |
| 488 | { |
| 489 | headers, |
| 490 | timeout: TEAM_MEMORY_SYNC_TIMEOUT_MS, |
| 491 | validateStatus: status => status === 200 || status === 412, |
| 492 | }, |
| 493 | ) |
| 494 | |
| 495 | if (response.status === 412) { |
| 496 | logForDebugging('team-memory-sync: conflict (412 Precondition Failed)', { |
| 497 | level: 'info', |
| 498 | }) |
| 499 | return { success: false, conflict: true, error: 'ETag mismatch' } |
| 500 | } |
| 501 | |
| 502 | const responseChecksum = response.data?.checksum |
| 503 | if (responseChecksum) { |
| 504 | state.lastKnownChecksum = responseChecksum |
| 505 | } |
| 506 | |
| 507 | logForDebugging( |
| 508 | `team-memory-sync: uploaded ${Object.keys(entries).length} entries (checksum: ${responseChecksum ?? 'none'})`, |
| 509 | { level: 'debug' }, |
| 510 | ) |
| 511 | return { |
| 512 | success: true, |
| 513 | checksum: responseChecksum, |
| 514 | lastModified: response.data?.lastModified, |
| 515 | } |
| 516 | } catch (error) { |
| 517 | const body = axios.isAxiosError(error) |
| 518 | ? JSON.stringify(error.response?.data ?? '') |
| 519 | : '' |
no test coverage detected