11import { 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' ;
44import type { PullRequestUrlIdentity } from '../../../../git/models/pullRequest.utils' ;
55import type { Provider } from '../../../../git/models/remoteProvider' ;
66import type { Integration } from '../../integration' ;
@@ -67,6 +67,24 @@ export interface GitLabMergeRequest {
6767 webUrl : string ;
6868}
6969
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+
7088export type GitLabMergeRequestState = 'opened' | 'closed' | 'locked' | 'merged' ;
7189
7290export function fromGitLabMergeRequestState ( state : GitLabMergeRequestState ) : PullRequestState {
@@ -93,6 +111,15 @@ export interface GitLabMergeRequestREST {
93111 updated_at : string ;
94112 closed_at : string | null ;
95113 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 ;
96123 web_url : string ;
97124}
98125
@@ -167,3 +194,61 @@ export function getGitLabPullRequestIdentityFromMaybeUrl(url: string): RequireSo
167194
168195 return { prNumber : match [ 2 ] , ownerAndRepo : match [ 1 ] , provider : HostingIntegrationId . GitLab } ;
169196}
197+
198+ export function fromGitLabMergeRequest ( pr : GitLabMergeRequestFull , provider : Provider ) : PullRequest {
199+ return new PullRequest (
200+ provider ,
201+ {
202+ // author
203+ id : pr . author ?. id ?? '' ,
204+ name : pr . author ?. name ?? 'Unknown' ,
205+ avatarUrl : pr . author ?. avatarUrl ?? '' ,
206+ url : pr . author ?. webUrl ?? '' ,
207+ } ,
208+ pr . iid , // id
209+ pr . id , // nodeId
210+ pr . title ,
211+ pr . webUrl || '' ,
212+ {
213+ // IssueRepository
214+ owner : '' ,
215+ repo : '' ,
216+ url : '' ,
217+ } ,
218+ fromGitLabMergeRequestState ( pr . state ) , // PullRequestState
219+ new Date ( pr . createdAt ) ,
220+ new Date ( pr . updatedAt ) ,
221+ // TODO@eamodio this isn't right, but GitLab doesn't seem to provide a closedAt on merge requests in GraphQL
222+ pr . state !== 'closed' ? undefined : new Date ( pr . updatedAt ) ,
223+ pr . mergedAt == null ? undefined : new Date ( pr . mergedAt ) ,
224+ PullRequestMergeableState . Unknown ,
225+ undefined , // viewerCanUpdate
226+ fromGitLabMergeRequestRefs ( pr ) , // PullRequestRefs
227+ ) ;
228+ }
229+
230+ function fromGitLabMergeRequestRefs ( pr : GitLabMergeRequestFull ) : PullRequestRefs | undefined {
231+ return {
232+ base : {
233+ owner : getRepoNamespace ( pr . sourceProject . fullPath ) ,
234+ branch : pr . sourceBranch ,
235+ exists : true ,
236+ url : pr . sourceProject . webUrl ,
237+ repo : pr . sourceProject . fullPath ,
238+ sha : pr . diffRefs . baseSha || '' ,
239+ } ,
240+ head : {
241+ owner : getRepoNamespace ( pr . project . fullPath ) ,
242+ branch : pr . targetBranch ,
243+ exists : true ,
244+ url : pr . project . webUrl ,
245+ repo : pr . project . fullPath ,
246+ sha : pr . diffRefs . headSha ,
247+ } ,
248+ isCrossRepository : pr . sourceProject . id !== pr . project . id ,
249+ } ;
250+ }
251+
252+ function getRepoNamespace ( projectFullPath : string ) {
253+ return projectFullPath . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ;
254+ }
0 commit comments