Skip to content

Commit

Permalink
Makes GitLab PRs checkoutable
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Dec 23, 2024
1 parent d71e5d5 commit e16457c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/git/models/pullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export function getRepositoryIdentityForPullRequest(
if (headRepo) {
return {
remote: {
url: pr.refs?.head?.url,
url: pr.repository.url,
domain: pr.provider.domain,
},
name: `${pr.refs?.head?.owner ?? pr.repository.owner}/${pr.refs?.head?.repo ?? pr.repository.repo}`,
Expand Down
2 changes: 1 addition & 1 deletion src/plus/integrations/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ export abstract class IntegrationBase<
const connected = this.maybeConnected ?? (await this.isConnected());
if (!connected) return undefined;

const pr = this.container.cache.getPullRequest(id, resource, this, () => ({
const pr = await this.container.cache.getPullRequest(id, resource, this, () => ({
value: (async () => {
try {
const result = await this.getProviderPullRequest?.(this._session!, resource, id);
Expand Down
2 changes: 2 additions & 0 deletions src/plus/integrations/providers/github/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export function fromGitHubPullRequestLite(pr: GitHubPullRequestLite, provider: P
owner: pr.repository.owner.login,
repo: pr.repository.name,
accessLevel: fromGitHubViewerPermissionToAccessLevel(pr.repository.viewerPermission),
url: pr.repository.url,
},
fromGitHubIssueOrPullRequestState(pr.state),
new Date(pr.createdAt),
Expand Down Expand Up @@ -331,6 +332,7 @@ export function fromGitHubPullRequest(pr: GitHubPullRequest, provider: Provider)
owner: pr.repository.owner.login,
repo: pr.repository.name,
accessLevel: fromGitHubViewerPermissionToAccessLevel(pr.repository.viewerPermission),
url: pr.repository.url,
},
fromGitHubIssueOrPullRequestState(pr.state),
new Date(pr.createdAt),
Expand Down
53 changes: 48 additions & 5 deletions src/plus/integrations/providers/gitlab/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HostingIntegrationId } from '../../../../constants.integrations';
import type { PullRequestState } from '../../../../git/models/pullRequest';
import type { PullRequestRefs, PullRequestState } 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';
Expand Down Expand Up @@ -94,6 +94,15 @@ export interface GitLabMergeRequestREST {
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
diff_refs: {
base_sha: string;
head_sha: string;
start_sha: string;
};
source_branch: string;
source_project_id: number;
target_branch: string;
target_project_id: number;
web_url: string;
references: {
full: string;
Expand All @@ -109,17 +118,23 @@ export function fromGitLabMergeRequestREST(
return new PullRequest(
provider,
{
// author
id: pr.author?.id ?? '',
name: pr.author?.name ?? 'Unknown',
avatarUrl: pr.author?.avatar_url ?? '',
url: pr.author?.web_url ?? '',
},
String(pr.iid),
String(pr.id),
String(pr.iid), // id
String(pr.id), // nodeId
pr.title,
pr.web_url,
repo,
fromGitLabMergeRequestState(pr.state),
{
// IssueRepository
owner: repo.owner,
repo: repo.repo,
url: pr.web_url.replace(/\/-\/merge_requests\/\d+$/, ''),
},
fromGitLabMergeRequestState(pr.state), // PullRequestState
new Date(pr.created_at),
new Date(pr.updated_at),
pr.closed_at == null ? undefined : new Date(pr.closed_at),
Expand All @@ -129,9 +144,37 @@ export function fromGitLabMergeRequestREST(
: pr.detailed_merge_status === 'conflict'
? PullRequestMergeableState.Conflicting
: PullRequestMergeableState.Unknown,
undefined, // viewerCanUpdate
fromGitLabMergeRequestRefs(pr, repo), // PullRequestRefs
);
}

function fromGitLabMergeRequestRefs(
pr: GitLabMergeRequestREST,
repo: { owner: string; repo: string },
): PullRequestRefs {
const repoUrl = pr.web_url.replace(/\/merge_requests\/\d+$/, '');
return {
base: {
owner: repo.owner,
branch: pr.target_branch,
exists: true,
url: `${repoUrl}/tree/${pr.target_branch}`,
repo: repo.repo,
sha: pr.diff_refs?.base_sha,
},
head: {
owner: repo.owner,
branch: pr.source_branch,
exists: true,
url: `${repoUrl}/tree/${pr.source_branch}`,
repo: repo.repo,
sha: pr.diff_refs?.head_sha,
},
isCrossRepository: pr.source_project_id !== pr.target_project_id,
};
}

export interface GitLabProjectREST {
namespace: {
path: string;
Expand Down

0 comments on commit e16457c

Please sign in to comment.