Skip to content

Commit

Permalink
Implements pull request search to GitLab integration using GitLab's API
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Dec 20, 2024
1 parent 2e57b90 commit 3e049f8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/plus/integrations/providers/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,29 @@ abstract class GitLabIntegrationBase<
.filter((result): result is SearchedIssue => result != null);
}

protected override async searchProviderPullRequests(
{ accessToken }: AuthenticationSession,
searchQuery: string,
repos?: GitLabRepositoryDescriptor[],
cancellation?: CancellationToken,
): Promise<PullRequest[] | undefined> {
const api = await this.container.gitlab;
if (!api) {
return undefined;
}

return api.searchPullRequests(
this,
accessToken,
{
search: searchQuery,
repos: repos?.map(r => `${r.owner}/${r.name}`),
baseUrl: this.apiBaseUrl,
},
cancellation,
);
}

protected override async mergeProviderPullRequest(
_session: AuthenticationSession,
pr: PullRequest,
Expand Down
44 changes: 44 additions & 0 deletions src/plus/integrations/providers/gitlab/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,50 @@ export class GitLabApi implements Disposable {
}
}

@debug<GitLabApi['searchPullRequests']>({ args: { 0: p => p.name, 1: '<token>' } })
async searchPullRequests(
provider: Provider,
token: string,
options?: { search?: string; user?: string; repos?: string[]; baseUrl?: string; avatarSize?: number },
cancellation?: CancellationToken,
): Promise<PullRequest[]> {
const scope = getLogScope();
const search = options?.search;
if (!search) {
return [];
}
try {
const gitlabPRs = await this.request<GitLabMergeRequestREST[]>(
provider,
token,
options?.baseUrl,
`v4/search/?scope=merge_requests&search=${search}`,
{
method: 'GET',
},
cancellation,
scope,
);
if (gitlabPRs.length === 0) {
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;
} catch (ex) {
if (ex instanceof RequestNotFoundError) return [];

throw this.handleException(ex, provider, scope);
}
}

private async findUser(
provider: Provider,
token: string,
Expand Down
14 changes: 12 additions & 2 deletions src/plus/integrations/providers/gitlab/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HostingIntegrationId } from '../../../../constants.integrations';
import type { PullRequestState } from '../../../../git/models/pullRequest';
import { PullRequest } from '../../../../git/models/pullRequest';
import { PullRequest, PullRequestMergeableState } from '../../../../git/models/pullRequest';
import type { PullRequestUrlIdentity } from '../../../../git/models/pullRequest.utils';
import type { Provider } from '../../../../git/models/remoteProvider';
import type { Integration } from '../../integration';
Expand Down Expand Up @@ -93,7 +93,12 @@ export interface GitLabMergeRequestREST {
updated_at: string;
closed_at: string | null;
merged_at: string | null;
detailed_merge_status: 'conflict' | 'mergeable' | string; // https://docs.gitlab.com/ee/api/merge_requests.html#merge-status
web_url: string;
references: {
full: string;
short: string;
};
}

export function fromGitLabMergeRequestREST(
Expand All @@ -110,7 +115,7 @@ export function fromGitLabMergeRequestREST(
url: pr.author?.web_url ?? '',
},
String(pr.iid),
undefined,
String(pr.id),
pr.title,
pr.web_url,
repo,
Expand All @@ -119,6 +124,11 @@ export function fromGitLabMergeRequestREST(
new Date(pr.updated_at),
pr.closed_at == null ? undefined : new Date(pr.closed_at),
pr.merged_at == null ? undefined : new Date(pr.merged_at),
pr.detailed_merge_status === 'mergeable'
? PullRequestMergeableState.Mergeable
: pr.detailed_merge_status === 'conflict'
? PullRequestMergeableState.Conflicting
: PullRequestMergeableState.Unknown,
);
}

Expand Down

0 comments on commit 3e049f8

Please sign in to comment.