diff --git a/src/gitlab_service.ts b/src/gitlab_service.ts index c2361d55ef0f060e8474239e07087b4eb1ab1587..17f43f6e32774dc6e81faf04710c0f2fb46fe3b8 100644 --- a/src/gitlab_service.ts +++ b/src/gitlab_service.ts @@ -482,8 +482,15 @@ export async function validateCIConfig(content: string) { return validCIConfig; } -export async function fetchLabelEvents(issuable: RestIssuable) { - let labelEvents: { body: string }[] = []; +interface LabelEvent { + label: unknown; + body: string; + // eslint-disable-next-line camelcase + created_at: string; +} + +export async function fetchLabelEvents(issuable: RestIssuable): Promise { + let labelEvents: LabelEvent[] = []; try { const type = issuable.sha ? 'merge_requests' : 'issues'; @@ -503,8 +510,15 @@ export async function fetchLabelEvents(issuable: RestIssuable) { return labelEvents; } -export async function fetchDiscussions(issuable: RestIssuable, page = 1) { - let discussions: unknown[] = []; +interface Discussion { + notes: { + // eslint-disable-next-line camelcase + created_at: string; + }[]; +} + +export async function fetchDiscussions(issuable: RestIssuable, page = 1): Promise { + let discussions: Discussion[] = []; try { const type = issuable.sha ? 'merge_requests' : 'issues'; @@ -579,3 +593,25 @@ export async function saveNote(params: { return { success: false }; } + +type note = Discussion | LabelEvent; + +function isLabelEvent(object: any): object is LabelEvent { + return Boolean(object.label); +} + +export async function fetchDiscussionsAndLabelEvents(issuable: RestIssuable): Promise { + const [discussions, labelEvents] = await Promise.all([ + fetchDiscussions(issuable), + fetchLabelEvents(issuable), + ]); + + const combinedEvents: note[] = [...discussions, ...labelEvents]; + combinedEvents.sort((a: note, b: note) => { + const aCreatedAt = isLabelEvent(a) ? a.created_at : a.notes[0].created_at; + const bCreatedAt = isLabelEvent(b) ? b.created_at : b.notes[0].created_at; + return aCreatedAt < bCreatedAt ? -1 : 1; + }); + + return combinedEvents; +} diff --git a/src/webview_controller.js b/src/webview_controller.js index e7c9fed97f21a26fa69d778bf3aaab137e70798d..05446eda3b496cc0300587df39ea94186ba6b367 100644 --- a/src/webview_controller.js +++ b/src/webview_controller.js @@ -84,8 +84,8 @@ const createMessageHandler = (panel, issuable, workspaceFolder) => async message }); if (response.success !== false) { - const newDiscussions = await gitLabService.fetchDiscussions(issuable); - panel.webview.postMessage({ type: 'issuableFetch', issuable, discussions: newDiscussions }); + const discussions = await gitLabService.fetchDiscussionsAndLabelEvents(issuable); + panel.webview.postMessage({ type: 'issuableFetch', issuable, discussions }); panel.webview.postMessage({ type: 'noteSaved' }); } else { panel.webview.postMessage({ type: 'noteSaved', status: false }); @@ -105,19 +105,9 @@ async function handleChangeViewState(panel, issuable) { }); }); - const [discussions, labelEvents] = await Promise.all([ - gitLabService.fetchDiscussions(issuable), - gitLabService.fetchLabelEvents(issuable), - ]); - - const combinedEvents = discussions.concat(labelEvents); - combinedEvents.sort((a, b) => { - const aCreatedAt = a.label ? a.created_at : a.notes[0].created_at; - const bCreatedAt = b.label ? b.created_at : b.notes[0].created_at; - return aCreatedAt < bCreatedAt ? -1 : 1; - }); + const discussonsAndLabels = await gitLabService.fetchDiscussionsAndLabelEvents(issuable); await appReadyPromise; - panel.webview.postMessage({ type: 'issuableFetch', issuable, discussions: combinedEvents }); + panel.webview.postMessage({ type: 'issuableFetch', issuable, discussions: discussonsAndLabels }); } const getIconPathForIssuable = issuable => {