Skip to content

Commit 59a1124

Browse files
committed
Implements pull request search to GitLab integration using GitLab's API
(#3788, #3795)
1 parent 2b3c887 commit 59a1124

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

src/plus/integrations/providers/gitlab.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,29 @@ abstract class GitLabIntegrationBase<
312312
.filter((result): result is SearchedIssue => result != null);
313313
}
314314

315+
protected override async searchProviderPullRequests(
316+
{ accessToken }: AuthenticationSession,
317+
searchQuery: string,
318+
repos?: GitLabRepositoryDescriptor[],
319+
cancellation?: CancellationToken,
320+
): Promise<PullRequest[] | undefined> {
321+
const api = await this.container.gitlab;
322+
if (!api) {
323+
return undefined;
324+
}
325+
326+
return api.searchPullRequests(
327+
this,
328+
accessToken,
329+
{
330+
search: searchQuery,
331+
repos: repos?.map(r => `${r.owner}/${r.name}`),
332+
baseUrl: this.apiBaseUrl,
333+
},
334+
cancellation,
335+
);
336+
}
337+
315338
protected override async mergeProviderPullRequest(
316339
_session: AuthenticationSession,
317340
pr: PullRequest,

src/plus/integrations/providers/gitlab/gitlab.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,50 @@ export class GitLabApi implements Disposable {
661661
}
662662
}
663663

664+
@debug<GitLabApi['searchPullRequests']>({ args: { 0: p => p.name, 1: '<token>' } })
665+
async searchPullRequests(
666+
provider: Provider,
667+
token: string,
668+
options?: { search?: string; user?: string; repos?: string[]; baseUrl?: string; avatarSize?: number },
669+
cancellation?: CancellationToken,
670+
): Promise<PullRequest[]> {
671+
const scope = getLogScope();
672+
const search = options?.search;
673+
if (!search) {
674+
return [];
675+
}
676+
try {
677+
const gitlabPRs = await this.request<GitLabMergeRequestREST[]>(
678+
provider,
679+
token,
680+
options?.baseUrl,
681+
`v4/search/?scope=merge_requests&search=${search}`,
682+
{
683+
method: 'GET',
684+
},
685+
cancellation,
686+
scope,
687+
);
688+
if (gitlabPRs.length === 0) {
689+
return [];
690+
}
691+
692+
const prs: PullRequest[] = gitlabPRs.map(pr => {
693+
const fullRef = pr.references.full;
694+
const project = {
695+
owner: fullRef.substring(0, fullRef.lastIndexOf('/')),
696+
repo: fullRef.substring(fullRef.lastIndexOf('/') + 1, fullRef.lastIndexOf('!')),
697+
};
698+
return fromGitLabMergeRequestREST(pr, provider, project);
699+
});
700+
return prs;
701+
} catch (ex) {
702+
if (ex instanceof RequestNotFoundError) return [];
703+
704+
throw this.handleException(ex, provider, scope);
705+
}
706+
}
707+
664708
private async findUser(
665709
provider: Provider,
666710
token: string,

src/plus/integrations/providers/gitlab/models.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HostingIntegrationId } from '../../../../constants.integrations';
22
import type { PullRequestState } from '../../../../git/models/pullRequest';
3-
import { PullRequest } from '../../../../git/models/pullRequest';
3+
import { PullRequest, PullRequestMergeableState } from '../../../../git/models/pullRequest';
44
import type { PullRequestUrlIdentity } from '../../../../git/models/pullRequest.utils';
55
import type { Provider } from '../../../../git/models/remoteProvider';
66
import type { Integration } from '../../integration';
@@ -93,7 +93,12 @@ export interface GitLabMergeRequestREST {
9393
updated_at: string;
9494
closed_at: string | null;
9595
merged_at: string | null;
96+
detailed_merge_status: 'conflict' | 'mergeable' | string; // https://docs.gitlab.com/ee/api/merge_requests.html#merge-status
9697
web_url: string;
98+
references: {
99+
full: string;
100+
short: string;
101+
};
97102
}
98103

99104
export function fromGitLabMergeRequestREST(
@@ -110,7 +115,7 @@ export function fromGitLabMergeRequestREST(
110115
url: pr.author?.web_url ?? '',
111116
},
112117
String(pr.iid),
113-
undefined,
118+
String(pr.id),
114119
pr.title,
115120
pr.web_url,
116121
repo,
@@ -119,6 +124,11 @@ export function fromGitLabMergeRequestREST(
119124
new Date(pr.updated_at),
120125
pr.closed_at == null ? undefined : new Date(pr.closed_at),
121126
pr.merged_at == null ? undefined : new Date(pr.merged_at),
127+
pr.detailed_merge_status === 'mergeable'
128+
? PullRequestMergeableState.Mergeable
129+
: pr.detailed_merge_status === 'conflict'
130+
? PullRequestMergeableState.Conflicting
131+
: PullRequestMergeableState.Unknown,
122132
);
123133
}
124134

0 commit comments

Comments
 (0)