1
1
import { HostingIntegrationId } from '../../../../constants.integrations' ;
2
- import type { PullRequestState } from '../../../../git/models/pullRequest' ;
3
- import { PullRequest } from '../../../../git/models/pullRequest' ;
2
+ import type { PullRequestRefs , PullRequestState } from '../../../../git/models/pullRequest' ;
3
+ import { PullRequest , PullRequestMergeableState } from '../../../../git/models/pullRequest' ;
4
4
import type { PullRequestUrlIdentity } from '../../../../git/models/pullRequest.utils' ;
5
5
import type { Provider } from '../../../../git/models/remoteProvider' ;
6
6
import type { Integration } from '../../integration' ;
@@ -67,6 +67,24 @@ export interface GitLabMergeRequest {
67
67
webUrl : string ;
68
68
}
69
69
70
+ export interface GitLabRepositoryStub {
71
+ id : string ;
72
+ fullPath : string ;
73
+ webUrl : string ;
74
+ }
75
+
76
+ export interface GitLabMergeRequestFull extends GitLabMergeRequest {
77
+ id : string ;
78
+ targetBranch : string ;
79
+ sourceBranch : string ;
80
+ diffRefs : {
81
+ baseSha : string | null ;
82
+ headSha : string ;
83
+ } ;
84
+ project : GitLabRepositoryStub ;
85
+ sourceProject : GitLabRepositoryStub ;
86
+ }
87
+
70
88
export type GitLabMergeRequestState = 'opened' | 'closed' | 'locked' | 'merged' ;
71
89
72
90
export function fromGitLabMergeRequestState ( state : GitLabMergeRequestState ) : PullRequestState {
@@ -93,6 +111,15 @@ export interface GitLabMergeRequestREST {
93
111
updated_at : string ;
94
112
closed_at : string | null ;
95
113
merged_at : string | null ;
114
+ diff_refs : {
115
+ base_sha : string ;
116
+ head_sha : string ;
117
+ start_sha : string ;
118
+ } ;
119
+ source_branch : string ;
120
+ source_project_id : number ;
121
+ target_branch : string ;
122
+ target_project_id : number ;
96
123
web_url : string ;
97
124
}
98
125
@@ -167,3 +194,63 @@ export function getGitLabPullRequestIdentityFromMaybeUrl(url: string): RequireSo
167
194
168
195
return { prNumber : match [ 2 ] , ownerAndRepo : match [ 1 ] , provider : HostingIntegrationId . GitLab } ;
169
196
}
197
+
198
+ export function fromGitLabMergeRequest ( pr : GitLabMergeRequestFull , provider : Provider ) : PullRequest {
199
+ const [ owner , repo ] = pr . project . fullPath . split ( '/' ) ;
200
+
201
+ return new PullRequest (
202
+ provider ,
203
+ {
204
+ // author
205
+ id : pr . author ?. id ?? '' ,
206
+ name : pr . author ?. name ?? 'Unknown' ,
207
+ avatarUrl : pr . author ?. avatarUrl ?? '' ,
208
+ url : pr . author ?. webUrl ?? '' ,
209
+ } ,
210
+ pr . iid , // id
211
+ pr . id , // nodeId
212
+ pr . title ,
213
+ pr . webUrl || '' ,
214
+ {
215
+ // IssueRepository
216
+ owner : owner ,
217
+ repo : repo ,
218
+ url : pr . project . webUrl ,
219
+ } ,
220
+ fromGitLabMergeRequestState ( pr . state ) , // PullRequestState
221
+ new Date ( pr . createdAt ) ,
222
+ new Date ( pr . updatedAt ) ,
223
+ // TODO@eamodio this isn't right, but GitLab doesn't seem to provide a closedAt on merge requests in GraphQL
224
+ pr . state !== 'closed' ? undefined : new Date ( pr . updatedAt ) ,
225
+ pr . mergedAt == null ? undefined : new Date ( pr . mergedAt ) ,
226
+ PullRequestMergeableState . Unknown ,
227
+ undefined , // viewerCanUpdate
228
+ fromGitLabMergeRequestRefs ( pr ) , // PullRequestRefs
229
+ ) ;
230
+ }
231
+
232
+ function fromGitLabMergeRequestRefs ( pr : GitLabMergeRequestFull ) : PullRequestRefs | undefined {
233
+ return {
234
+ base : {
235
+ owner : getRepoNamespace ( pr . sourceProject . fullPath ) ,
236
+ branch : pr . sourceBranch ,
237
+ exists : true ,
238
+ url : pr . sourceProject . webUrl ,
239
+ repo : pr . sourceProject . fullPath ,
240
+ sha : pr . diffRefs . baseSha || '' ,
241
+ } ,
242
+ head : {
243
+ owner : getRepoNamespace ( pr . project . fullPath ) ,
244
+ branch : pr . targetBranch ,
245
+ exists : true ,
246
+ url : pr . project . webUrl ,
247
+ repo : pr . project . fullPath ,
248
+ sha : pr . diffRefs . headSha ,
249
+ } ,
250
+ isCrossRepository : pr . sourceProject . id !== pr . project . id ,
251
+ } ;
252
+ }
253
+
254
+ function getRepoNamespace ( projectFullPath : string ) {
255
+ return projectFullPath . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ;
256
+ }
0 commit comments