From 09c5d361067ce4e70ec10a13380ff092db31b853 Mon Sep 17 00:00:00 2001 From: Thibaut FARCIN Date: Thu, 31 Oct 2024 08:24:50 +0100 Subject: [PATCH] feat(optimize-mr-search): Modify the fetchMergeRequests method to use the project path when called with an URL --- .../utils/mockBuildReviewMessageCalls.ts | 13 ++- src/core/services/gitlab.ts | 92 +++++++++++++------ 2 files changed, 78 insertions(+), 27 deletions(-) diff --git a/__tests__/utils/mockBuildReviewMessageCalls.ts b/__tests__/utils/mockBuildReviewMessageCalls.ts index 01c2b25..4d173b3 100644 --- a/__tests__/utils/mockBuildReviewMessageCalls.ts +++ b/__tests__/utils/mockBuildReviewMessageCalls.ts @@ -6,7 +6,14 @@ import { projectFixture } from '../__fixtures__/projectFixture'; import { mockGitlabCall } from './mockGitlabCall'; export function mockBuildReviewMessageCalls() { - const { iid, project_id } = mergeRequestFixture; + const { iid, project_id, web_url } = mergeRequestFixture; + + const url = new URL(web_url); + const projectPath = url.pathname + .split('/') + .filter(Boolean) + .slice(0, -2) + .join('/'); mockGitlabCall( `/projects/${project_id}/merge_requests/${iid}/approvals`, @@ -16,6 +23,10 @@ export function mockBuildReviewMessageCalls() { `/projects/${project_id}/merge_requests/${iid}`, mergeRequestDetailsFixture ); + mockGitlabCall( + `/projects/${encodeURIComponent(projectPath)}/merge_requests/${iid}`, + mergeRequestDetailsFixture + ); mockGitlabCall( `/projects/${project_id}/merge_requests/${iid}/reviewers`, mergeRequestReviewersFixture diff --git a/src/core/services/gitlab.ts b/src/core/services/gitlab.ts index 0ad13ae..7a92b9f 100644 --- a/src/core/services/gitlab.ts +++ b/src/core/services/gitlab.ts @@ -327,37 +327,77 @@ export async function searchMergeRequests( search: string, projects: DataProject[] ): Promise { - let projectMergeRequestPromises; + const mergeRequests = await fetchMergeRequests(search, projects); - if (MERGE_REQUEST_ID_REGEX.test(search)) { - const iid = search.replace('!', ''); + return mergeRequests + .flat() + .filter((mr) => + MERGE_REQUEST_OPEN_STATES.includes(mr?.state) + ) as GitlabMergeRequest[]; +} - projectMergeRequestPromises = projects.map(async ({ projectId }) => - callAPI(`/projects/${projectId}/merge_requests/${iid}`) - ); - } else if (MERGE_REQUEST_URL_REGEX.test(search)) { - const iid = search.match(MERGE_REQUEST_URL_REGEX)![1]; +async function fetchMergeRequests( + search: string, + projects: DataProject[] +): Promise { + if (MERGE_REQUEST_ID_REGEX.test(search)) { + return searchMergeRequestsById(search, projects); + } - projectMergeRequestPromises = projects.map(async ({ projectId }) => - callAPI(`/projects/${projectId}/merge_requests/${iid}`) - ); - } else { - projectMergeRequestPromises = projects.map(async ({ projectId }) => - callAPI( - `/projects/${projectId}/merge_requests?state=opened&search=${encodeURIComponent( - search - )}` - ) - ); + if (MERGE_REQUEST_URL_REGEX.test(search)) { + return searchMergeRequestsByUrl(search); } - return (await Promise.all(projectMergeRequestPromises)) - .flat() - .filter((mergeRequest) => - MERGE_REQUEST_OPEN_STATES.includes( - (mergeRequest as GitlabMergeRequest)?.state - ) - ) as GitlabMergeRequest[]; + return searchMergeRequestsByText(search, projects); +} + +async function searchMergeRequestsById( + search: string, + projects: DataProject[] +): Promise { + const iid = search.replace('!', ''); + return Promise.all( + projects.map( + ({ projectId }) => + callAPI( + `/projects/${projectId}/merge_requests/${iid}` + ) as Promise + ) + ); +} + +async function searchMergeRequestsByUrl( + search: string +): Promise { + const iid = search.match(MERGE_REQUEST_URL_REGEX)![1]; + const url = new URL(search); + const projectPath = url.pathname + .split('/') + .filter(Boolean) + .slice(0, -2) + .join('/'); + + return Promise.all([ + callAPI( + `/projects/${encodeURIComponent(projectPath)}/merge_requests/${iid}` + ) as Promise, + ]); +} + +async function searchMergeRequestsByText( + search: string, + projects: DataProject[] +): Promise { + return Promise.all( + projects.map( + ({ projectId }) => + callAPI( + `/projects/${projectId}/merge_requests?state=opened&search=${encodeURIComponent( + search + )}` + ) as Promise + ) + ); } export async function searchProjects(search: string): Promise {