|
1 | | -import type { PullRequestState } from '../../../../git/models/pullRequest'; |
| 1 | +import type { PullRequestRefs, PullRequestState } from '../../../../git/models/pullRequest'; |
2 | 2 | import { PullRequest, PullRequestMergeableState } from '../../../../git/models/pullRequest'; |
3 | 3 | import type { Provider } from '../../../../git/models/remoteProvider'; |
4 | 4 | import type { Integration } from '../../integration'; |
@@ -65,6 +65,24 @@ export interface GitLabMergeRequest { |
65 | 65 | webUrl: string; |
66 | 66 | } |
67 | 67 |
|
| 68 | +export interface GitLabRepositoryStub { |
| 69 | + id: string; |
| 70 | + fullPath: string; |
| 71 | + webUrl: string; |
| 72 | +} |
| 73 | + |
| 74 | +export interface GitLabMergeRequestFull extends GitLabMergeRequest { |
| 75 | + id: string; |
| 76 | + targetBranch: string; |
| 77 | + sourceBranch: string; |
| 78 | + diffRefs: { |
| 79 | + baseSha: string | null; |
| 80 | + headSha: string; |
| 81 | + }; |
| 82 | + project: GitLabRepositoryStub; |
| 83 | + sourceProject: GitLabRepositoryStub; |
| 84 | +} |
| 85 | + |
68 | 86 | export type GitLabMergeRequestState = 'opened' | 'closed' | 'locked' | 'merged'; |
69 | 87 |
|
70 | 88 | export function fromGitLabMergeRequestState(state: GitLabMergeRequestState): PullRequestState { |
@@ -92,6 +110,15 @@ export interface GitLabMergeRequestREST { |
92 | 110 | closed_at: string | null; |
93 | 111 | merged_at: string | null; |
94 | 112 | detailed_merge_status: 'conflict' | 'mergeable' | string; // https://docs.gitlab.com/ee/api/merge_requests.html#merge-status |
| 113 | + diff_refs: { |
| 114 | + base_sha: string; |
| 115 | + head_sha: string; |
| 116 | + start_sha: string; |
| 117 | + }; |
| 118 | + source_branch: string; |
| 119 | + source_project_id: number; |
| 120 | + target_branch: string; |
| 121 | + target_project_id: number; |
95 | 122 | web_url: string; |
96 | 123 | references: { |
97 | 124 | full: string; |
@@ -160,3 +187,61 @@ export function fromGitLabMergeRequestProvidersApi(pr: ProviderPullRequest, prov |
160 | 187 | }; |
161 | 188 | return fromProviderPullRequest(wrappedPr, provider); |
162 | 189 | } |
| 190 | + |
| 191 | +export function fromGitLabMergeRequest(pr: GitLabMergeRequestFull, provider: Provider): PullRequest { |
| 192 | + return new PullRequest( |
| 193 | + provider, |
| 194 | + { |
| 195 | + // author |
| 196 | + id: pr.author?.id ?? '', |
| 197 | + name: pr.author?.name ?? 'Unknown', |
| 198 | + avatarUrl: pr.author?.avatarUrl ?? '', |
| 199 | + url: pr.author?.webUrl ?? '', |
| 200 | + }, |
| 201 | + pr.iid, // id |
| 202 | + pr.id, // nodeId |
| 203 | + pr.title, |
| 204 | + pr.webUrl || '', |
| 205 | + { |
| 206 | + // IssueRepository |
| 207 | + owner: '', |
| 208 | + repo: '', |
| 209 | + url: '', |
| 210 | + }, |
| 211 | + fromGitLabMergeRequestState(pr.state), // PullRequestState |
| 212 | + new Date(pr.createdAt), |
| 213 | + new Date(pr.updatedAt), |
| 214 | + // TODO@eamodio this isn't right, but GitLab doesn't seem to provide a closedAt on merge requests in GraphQL |
| 215 | + pr.state !== 'closed' ? undefined : new Date(pr.updatedAt), |
| 216 | + pr.mergedAt == null ? undefined : new Date(pr.mergedAt), |
| 217 | + PullRequestMergeableState.Unknown, |
| 218 | + undefined, // viewerCanUpdate |
| 219 | + fromGitLabMergeRequestRefs(pr), // PullRequestRefs |
| 220 | + ); |
| 221 | +} |
| 222 | + |
| 223 | +function fromGitLabMergeRequestRefs(pr: GitLabMergeRequestFull): PullRequestRefs | undefined { |
| 224 | + return { |
| 225 | + base: { |
| 226 | + owner: getRepoNamespace(pr.sourceProject.fullPath), |
| 227 | + branch: pr.sourceBranch, |
| 228 | + exists: true, |
| 229 | + url: pr.sourceProject.webUrl, |
| 230 | + repo: pr.sourceProject.fullPath, |
| 231 | + sha: pr.diffRefs.baseSha || '', |
| 232 | + }, |
| 233 | + head: { |
| 234 | + owner: getRepoNamespace(pr.project.fullPath), |
| 235 | + branch: pr.targetBranch, |
| 236 | + exists: true, |
| 237 | + url: pr.project.webUrl, |
| 238 | + repo: pr.project.fullPath, |
| 239 | + sha: pr.diffRefs.headSha, |
| 240 | + }, |
| 241 | + isCrossRepository: pr.sourceProject.id !== pr.project.id, |
| 242 | + }; |
| 243 | +} |
| 244 | + |
| 245 | +function getRepoNamespace(projectFullPath: string) { |
| 246 | + return projectFullPath.split('/').slice(0, -1).join('/'); |
| 247 | +} |
0 commit comments