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 Nov 25, 2024
1 parent 15b928f commit 3ccee05
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 @@ -267,6 +267,29 @@ abstract class GitLabIntegrationBase<
return Promise.resolve(undefined);
}

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,5 +1,5 @@
import type { PullRequestState } from '../../../../git/models/pullRequest';
import { PullRequest } from '../../../../git/models/pullRequest';
import { PullRequest, PullRequestMergeableState } from '../../../../git/models/pullRequest';
import type { Provider } from '../../../../git/models/remoteProvider';
import type { Integration } from '../../integration';
import type { ProviderPullRequest } from '../models';
Expand Down Expand Up @@ -91,7 +91,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 @@ -108,7 +113,7 @@ export function fromGitLabMergeRequestREST(
url: pr.author?.web_url ?? '',
},
String(pr.iid),
undefined,
String(pr.id),
pr.title,
pr.web_url,
repo,
Expand All @@ -117,6 +122,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 3ccee05

Please sign in to comment.