* Lists a single batch of work items. The full ID list is resolved once via WIQL * and cached on the sync context; the cursor is an offset into that list.
( accessToken: string, organization: string, project: string, filters: WorkItemFilters, maxItems: number, cursor: string | undefined, syncContext: Record<string, unknown> | undefined, lastSyncAt: Date | undefined )
| 1291 | * and cached on the sync context; the cursor is an offset into that list. |
| 1292 | */ |
| 1293 | async function listWorkItems( |
| 1294 | accessToken: string, |
| 1295 | organization: string, |
| 1296 | project: string, |
| 1297 | filters: WorkItemFilters, |
| 1298 | maxItems: number, |
| 1299 | cursor: string | undefined, |
| 1300 | syncContext: Record<string, unknown> | undefined, |
| 1301 | lastSyncAt: Date | undefined |
| 1302 | ): Promise<ExternalDocumentList> { |
| 1303 | let ids = syncContext?.workItemIds as number[] | undefined |
| 1304 | if (!ids) { |
| 1305 | const wiql = buildWiql(filters, lastSyncAt) |
| 1306 | ids = await queryWorkItemIds(accessToken, organization, project, wiql) |
| 1307 | if (syncContext) syncContext.workItemIds = ids |
| 1308 | |
| 1309 | if (ids.length >= WIQL_MAX_RESULTS && syncContext) { |
| 1310 | /** |
| 1311 | * The WIQL result filled the 20,000-item cap. Distinguish an exact fit |
| 1312 | * from genuine truncation: for structured filters, probe for any |
| 1313 | * matching item with an id beyond the largest returned one and only |
| 1314 | * flag the listing incomplete when one exists — otherwise deletion |
| 1315 | * reconciliation would be disabled forever for a project with exactly |
| 1316 | * 20,000 matching items. Custom WIQL cannot be probed (no safe clause |
| 1317 | * injection), so it is flagged conservatively. |
| 1318 | */ |
| 1319 | let truncated = true |
| 1320 | if (!filters.customWiql) { |
| 1321 | let maxId = 0 |
| 1322 | for (const id of ids) { |
| 1323 | if (id > maxId) maxId = id |
| 1324 | } |
| 1325 | const probeWiql = buildWiql(filters, lastSyncAt, maxId) |
| 1326 | const beyond = await queryWorkItemIds(accessToken, organization, project, probeWiql, 1) |
| 1327 | truncated = beyond.length > 0 |
| 1328 | } |
| 1329 | if (truncated) { |
| 1330 | syncContext.listingCapped = true |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | if (ids.length === 0) { |
| 1336 | return { documents: [], hasMore: false } |
| 1337 | } |
| 1338 | |
| 1339 | let offset = 0 |
| 1340 | if (cursor) { |
| 1341 | const parts = cursor.split('|') |
| 1342 | offset = Number(parts[1]) || 0 |
| 1343 | } |
| 1344 | |
| 1345 | const chunk = ids.slice(offset, offset + WORK_ITEM_BATCH_SIZE) |
| 1346 | const raw = await fetchWorkItemsBatch(accessToken, organization, project, chunk) |
| 1347 | if (raw.length < chunk.length && syncContext) { |
| 1348 | syncContext.listingCapped = true |
| 1349 | logger.warn( |
| 1350 | 'workitemsbatch omitted ids that WIQL returned; flagging listing as incomplete so reconciliation skips deletion', |
no test coverage detected