Skip to content

Commit

Permalink
feat(optimize-mr-search): Modify the fetchMergeRequests method to use…
Browse files Browse the repository at this point in the history
… the project path when called with an URL
  • Loading branch information
ThibautManoMano committed Oct 31, 2024
1 parent 678107d commit 09c5d36
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 27 deletions.
13 changes: 12 additions & 1 deletion __tests__/utils/mockBuildReviewMessageCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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
Expand Down
92 changes: 66 additions & 26 deletions src/core/services/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,37 +327,77 @@ export async function searchMergeRequests(
search: string,
projects: DataProject[]
): Promise<GitlabMergeRequest[]> {
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<GitlabMergeRequest[]> {
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<GitlabMergeRequest[]> {
const iid = search.replace('!', '');
return Promise.all(
projects.map(
({ projectId }) =>
callAPI(
`/projects/${projectId}/merge_requests/${iid}`
) as Promise<GitlabMergeRequest>
)
);
}

async function searchMergeRequestsByUrl(
search: string
): Promise<GitlabMergeRequest[]> {
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<GitlabMergeRequest>,
]);
}

async function searchMergeRequestsByText(
search: string,
projects: DataProject[]
): Promise<GitlabMergeRequest[]> {
return Promise.all(
projects.map(
({ projectId }) =>
callAPI(
`/projects/${projectId}/merge_requests?state=opened&search=${encodeURIComponent(
search
)}`
) as Promise<GitlabMergeRequest>
)
);
}

export async function searchProjects(search: string): Promise<GitlabProject[]> {
Expand Down

0 comments on commit 09c5d36

Please sign in to comment.