Skip to content

Commit

Permalink
Makes text-searched GitLab PRs checkoutable
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Dec 26, 2024
1 parent b2206de commit 5d901ee
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
86 changes: 76 additions & 10 deletions src/plus/integrations/providers/gitlab/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { fromGitLabMergeRequest, fromGitLabMergeRequestREST, fromGitLabMergeRequ

// drop it as soon as we switch to @gitkraken/providers-api
const gitlabUserIdPrefix = 'gid://gitlab/User/';
const gitlabMergeRequestIdPrefix = 'gid://gitlab/MergeRequest/';

function buildGitLabUserId(id: string | undefined): string | undefined {
return id?.startsWith(gitlabUserIdPrefix) ? id.substring(gitlabUserIdPrefix.length) : id;
}
Expand Down Expand Up @@ -724,11 +726,12 @@ export class GitLabApi implements Disposable {
return [];
}
try {
const perPageLimit = 20; // with bigger amount we exceed the max GraphQL complexity in the next query
const gitlabPRs = await this.request<GitLabMergeRequestREST[]>(
provider,
token,
options?.baseUrl,
`v4/search/?scope=merge_requests&search=${search}`,
`v4/search/?scope=merge_requests&search=${search}&per_page=${perPageLimit}`,
{
method: 'GET',
},
Expand All @@ -739,15 +742,78 @@ export class GitLabApi implements Disposable {
return [];
}

const prs: PullRequest[] = gitlabPRs.map(pr => {
const fullRef = pr.references.full;
const project = {
owner: fullRef.substring(0, fullRef.lastIndexOf('/')),
repo: fullRef.substring(fullRef.lastIndexOf('/') + 1, fullRef.lastIndexOf('!')),
};
return fromGitLabMergeRequestREST(pr, provider, project);
});
return prs;
interface QueryResult {
data: Record<`mergeRequest_${number}`, GitLabMergeRequestFull>;
}

const queryArgs = gitlabPRs.map((_, i) => `$id_${i}: MergeRequestID!`).join('\n');
const queryFields = gitlabPRs
.map((_, i) => `mergeRequest_${i}: mergeRequest(id: $id_${i}) { ...mergeRequestFields }`)
.join('\n');
// Set of fields includes only additional fields that are not included in GitLabMergeRequestREST.
// It's limited as much as possible to reduce complexity of the query.
const queryMrFields = `fragment mergeRequestFields on MergeRequest {
diffRefs {
baseSha
headSha
}
project {
id
fullPath
webUrl
}
sourceProject {
id
fullPath
webUrl
}
}`;
const query = `query getMergeRequests (${queryArgs}) {${queryFields}} ${queryMrFields}`;

const params = gitlabPRs.reduce<Record<`id_${number}`, string>>((ids, gitlabRestPr, i) => {
ids[`id_${i}`] = `${gitlabMergeRequestIdPrefix}${gitlabRestPr.id}`;
return ids;
}, {});
const rsp = await this.graphql<QueryResult>(
provider,
token,
options?.baseUrl,
query,
params,
cancellation,
scope,
);
if (rsp?.data != null) {
const resultPrs = gitlabPRs.reduce<PullRequest[]>((prsAggr, gitLabPr, i) => {
const pr = rsp.data[`mergeRequest_${i}`];
const fullPr: GitLabMergeRequestFull = {
...pr,
iid: String(gitLabPr.iid),
id: String(gitLabPr.id),
state: gitLabPr.state,
author: {
id: buildGitLabUserId(gitLabPr.author?.id) ?? '',
name: gitLabPr.author?.name ?? 'Unknown',
avatarUrl: gitLabPr.author?.avatar_url ?? '',
webUrl: gitLabPr.author?.web_url ?? '',
},
title: gitLabPr.title,
description: gitLabPr.description,
webUrl: gitLabPr.web_url,
createdAt: gitLabPr.created_at,
updatedAt: gitLabPr.updated_at,
mergedAt: gitLabPr.merged_at,
sourceBranch: gitLabPr.source_branch,
targetBranch: gitLabPr.target_branch,
};
if (pr != null) {
prsAggr.push(fromGitLabMergeRequest(fullPr, provider));
}
return prsAggr;
}, []);
return resultPrs;
}
return [];
} catch (ex) {
if (ex instanceof RequestNotFoundError) return [];

Expand Down
6 changes: 3 additions & 3 deletions src/plus/integrations/providers/gitlab/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export interface GitLabMergeRequestFull extends GitLabMergeRequest {
diffRefs: {
baseSha: string | null;
headSha: string;
};
} | null;
project: GitLabRepositoryStub;
sourceProject: GitLabRepositoryStub;
}
Expand Down Expand Up @@ -228,15 +228,15 @@ function fromGitLabMergeRequestRefs(pr: GitLabMergeRequestFull): PullRequestRefs
exists: true,
url: pr.sourceProject.webUrl,
repo: pr.sourceProject.fullPath,
sha: pr.diffRefs.baseSha || '',
sha: pr.diffRefs?.baseSha || '',
},
head: {
owner: getRepoNamespace(pr.project.fullPath),
branch: pr.targetBranch,
exists: true,
url: pr.project.webUrl,
repo: pr.project.fullPath,
sha: pr.diffRefs.headSha,
sha: pr.diffRefs?.headSha || '',
},
isCrossRepository: pr.sourceProject.id !== pr.project.id,
};
Expand Down

0 comments on commit 5d901ee

Please sign in to comment.