|
| 1 | +import type { |
| 2 | + IDataObject, |
| 3 | + ILoadOptionsFunctions, |
| 4 | + INodeListSearchItems, |
| 5 | + INodeListSearchResult, |
| 6 | +} from 'n8n-workflow'; |
| 7 | + |
| 8 | +import { extractNextCursor, resolveSpaceKey } from '../actions/common'; |
| 9 | +import { confluenceApiRequest } from '../transport'; |
| 10 | + |
| 11 | +interface SearchPage { |
| 12 | + entries: IDataObject[]; |
| 13 | + base: string; |
| 14 | + next?: string; |
| 15 | +} |
| 16 | + |
| 17 | +const SEARCH_PAGE_SIZE = 50; |
| 18 | +const MAX_FILTERED_SEARCH_PAGES = 10; |
| 19 | +const EMPTY_PAGE: SearchPage = { entries: [], base: '' }; |
| 20 | + |
| 21 | +export async function searchSpaces( |
| 22 | + this: ILoadOptionsFunctions, |
| 23 | + filter?: string, |
| 24 | + paginationToken?: string, |
| 25 | +): Promise<INodeListSearchResult> { |
| 26 | + const filterLower = (filter ?? '').trim().toLowerCase(); |
| 27 | + const results: INodeListSearchItems[] = []; |
| 28 | + let cursor = paginationToken; |
| 29 | + |
| 30 | + // No server-side text filter on the v2 spaces list; fetch ahead so matches |
| 31 | + // beyond the first page stay discoverable |
| 32 | + for (let fetched = 0; fetched < MAX_FILTERED_SEARCH_PAGES; fetched++) { |
| 33 | + const qs: IDataObject = { limit: SEARCH_PAGE_SIZE, sort: 'name', status: 'current' }; |
| 34 | + if (cursor !== undefined) qs.cursor = cursor; |
| 35 | + |
| 36 | + const response = await confluenceApiRequest.call(this, 'GET', '/wiki/api/v2/spaces', {}, qs); |
| 37 | + const entries = Array.isArray(response.results) ? (response.results as IDataObject[]) : []; |
| 38 | + |
| 39 | + for (const space of entries) { |
| 40 | + if (typeof space.id !== 'string' && typeof space.id !== 'number') continue; |
| 41 | + if (typeof space.name !== 'string') continue; |
| 42 | + if (filterLower !== '' && !space.name.toLowerCase().includes(filterLower)) continue; |
| 43 | + const key = typeof space.key === 'string' && space.key !== '' ? ` (${space.key})` : ''; |
| 44 | + results.push({ name: `${space.name}${key}`, value: String(space.id) }); |
| 45 | + } |
| 46 | + |
| 47 | + cursor = extractNextCursor(response); |
| 48 | + if (cursor === undefined || filterLower === '' || results.length > 0) break; |
| 49 | + } |
| 50 | + |
| 51 | + return { results, paginationToken: cursor }; |
| 52 | +} |
| 53 | + |
| 54 | +async function fetchSearchPage( |
| 55 | + this: ILoadOptionsFunctions, |
| 56 | + cql: string, |
| 57 | + start?: number, |
| 58 | +): Promise<SearchPage> { |
| 59 | + const qs: IDataObject = { cql, limit: SEARCH_PAGE_SIZE }; |
| 60 | + if (start !== undefined) qs.start = start; |
| 61 | + |
| 62 | + const response = await confluenceApiRequest.call(this, 'GET', '/wiki/rest/api/search', {}, qs); |
| 63 | + const links = response._links as IDataObject | undefined; |
| 64 | + return { |
| 65 | + entries: Array.isArray(response.results) ? (response.results as IDataObject[]) : [], |
| 66 | + base: typeof links?.base === 'string' ? links.base : '', |
| 67 | + next: typeof links?.next === 'string' && links.next !== '' ? links.next : undefined, |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +function toPageItems( |
| 72 | + entries: IDataObject[], |
| 73 | + base: string, |
| 74 | + withSpaceLabel: boolean, |
| 75 | +): INodeListSearchItems[] { |
| 76 | + const results: INodeListSearchItems[] = []; |
| 77 | + const seenIds = new Set<string>(); |
| 78 | + for (const entry of entries) { |
| 79 | + const content = entry.content as IDataObject | undefined; |
| 80 | + if (content === undefined) continue; |
| 81 | + if (typeof content.id !== 'string' && typeof content.id !== 'number') continue; |
| 82 | + const id = String(content.id); |
| 83 | + if (seenIds.has(id)) continue; |
| 84 | + seenIds.add(id); |
| 85 | + const title = typeof content.title === 'string' && content.title !== '' ? content.title : id; |
| 86 | + // The space name disambiguates same-titled pages; redundant once scoped to one space |
| 87 | + const container = entry.resultGlobalContainer as IDataObject | undefined; |
| 88 | + const space = |
| 89 | + withSpaceLabel && typeof container?.title === 'string' && container.title !== '' |
| 90 | + ? ` (${container.title})` |
| 91 | + : ''; |
| 92 | + const webui = (content._links as IDataObject | undefined)?.webui; |
| 93 | + const url = base !== '' && typeof webui === 'string' ? `${base}${webui}` : undefined; |
| 94 | + results.push({ name: `${title}${space}`, value: id, url }); |
| 95 | + } |
| 96 | + return results; |
| 97 | +} |
| 98 | + |
| 99 | +function nextStartToken( |
| 100 | + next: string | undefined, |
| 101 | + start: number, |
| 102 | + count: number, |
| 103 | +): string | undefined { |
| 104 | + if (next === undefined) return undefined; |
| 105 | + let parsed: string | null = null; |
| 106 | + try { |
| 107 | + parsed = new URL(next, 'https://api.atlassian.com').searchParams.get('start'); |
| 108 | + } catch { |
| 109 | + parsed = null; |
| 110 | + } |
| 111 | + // A page can come back empty while next is still set; never repeat the same offset |
| 112 | + return parsed ?? String(start + Math.max(count, 1)); |
| 113 | +} |
| 114 | + |
| 115 | +function getScopedSpaceId(this: ILoadOptionsFunctions): string { |
| 116 | + try { |
| 117 | + const raw = this.getCurrentNodeParameter('space', { extractValue: true }); |
| 118 | + return typeof raw === 'string' || typeof raw === 'number' ? String(raw).trim() : ''; |
| 119 | + } catch { |
| 120 | + return ''; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +export async function getPages( |
| 125 | + this: ILoadOptionsFunctions, |
| 126 | + filter?: string, |
| 127 | + paginationToken?: string, |
| 128 | +): Promise<INodeListSearchResult> { |
| 129 | + const start = paginationToken === undefined ? 0 : Number(paginationToken); |
| 130 | + const spaceId = getScopedSpaceId.call(this); |
| 131 | + |
| 132 | + let spaceClause = ''; |
| 133 | + if (spaceId !== '') { |
| 134 | + // CQL's space field matches by key, so the selected space ID is resolved first |
| 135 | + const spaceKey = await resolveSpaceKey.call(this, spaceId); |
| 136 | + if (spaceKey !== undefined) spaceClause = ` AND space = "${spaceKey}"`; |
| 137 | + } |
| 138 | + |
| 139 | + const escaped = (filter ?? '').replace(/(["\\])/g, '\\$1'); |
| 140 | + const cql = |
| 141 | + escaped === '' |
| 142 | + ? `type=page${spaceClause} ORDER BY lastmodified DESC` |
| 143 | + : `type=page${spaceClause} AND title ~ "${escaped}*" ORDER BY lastmodified DESC`; |
| 144 | + |
| 145 | + // Exact-title pages can be buried behind newer prefix matches, so page one |
| 146 | + // fetches them separately; toPageItems drops the overlap |
| 147 | + const exact = |
| 148 | + escaped !== '' && paginationToken === undefined |
| 149 | + ? await fetchSearchPage.call(this, `type=page${spaceClause} AND title = "${escaped}"`) |
| 150 | + : EMPTY_PAGE; |
| 151 | + |
| 152 | + const page = await fetchSearchPage.call(this, cql, start); |
| 153 | + |
| 154 | + return { |
| 155 | + results: toPageItems( |
| 156 | + [...exact.entries, ...page.entries], |
| 157 | + page.base || exact.base, |
| 158 | + spaceId === '', |
| 159 | + ), |
| 160 | + paginationToken: nextStartToken(page.next, start, page.entries.length), |
| 161 | + }; |
| 162 | +} |
0 commit comments