11import { readFile } from 'node:fs/promises'
22
33import { envAsString } from '../../utils/env.mts'
4+ import { spawn } from '../../utils/spawn.mts'
45
56export type CiPullRequestContext = {
67 baseBranch : string | undefined
78 baseCommitHash : string | undefined
89 number : number
910 provider : 'azure' | 'github' | 'gitlab' | undefined
11+ url ?: string | undefined
12+ }
13+
14+ function repositoryUrl ( value : string ) : URL | undefined {
15+ try {
16+ const url = new URL ( value )
17+ if (
18+ ! [ 'https:' , 'http:' ] . includes ( url . protocol ) ||
19+ ! url . hostname ||
20+ url . username ||
21+ url . password ||
22+ url . search ||
23+ url . hash
24+ ) {
25+ return undefined
26+ }
27+ url . pathname = url . pathname . replace ( / \/ + $ / u, '' )
28+ return url
29+ } catch {
30+ return undefined
31+ }
32+ }
33+
34+ function pullRequestUrl ( repository : string , path : string ) : string | undefined {
35+ const url = repositoryUrl ( repository )
36+ if ( ! url ) {
37+ return undefined
38+ }
39+ url . pathname = `${ url . pathname } /${ path } `
40+ return url . href
41+ }
42+
43+ function azurePullRequestUrl (
44+ repository : string ,
45+ number : number ,
46+ ) : string | undefined {
47+ const url = repositoryUrl ( repository )
48+ if ( ! url ) {
49+ return undefined
50+ }
51+ const path =
52+ url . hostname . toLowerCase ( ) === 'github.com' ? 'pull' : 'pullrequest'
53+ return pullRequestUrl ( url . href , `${ path } /${ number } ` )
54+ }
55+
56+ async function azureBaseCommit ( number : number ) : Promise < string | undefined > {
57+ const merge = process . env [ 'BUILD_SOURCEVERSION' ]
58+ const source = process . env [ 'SYSTEM_PULLREQUEST_SOURCECOMMITID' ]
59+ if (
60+ process . env [ 'BUILD_SOURCEBRANCH' ] !== `refs/pull/${ number } /merge` ||
61+ ! merge ||
62+ ! / ^ [ a - f 0 - 9 ] { 40 } $ / iu. test ( merge ) ||
63+ ! source
64+ ) {
65+ return undefined
66+ }
67+ try {
68+ const result = await spawn ( 'git' , [ 'show' , '-s' , '--format=%P' , merge ] )
69+ const parents = result . stdout . trim ( ) . split ( ' ' )
70+ if (
71+ parents . length === 2 &&
72+ parents [ 1 ] === source &&
73+ / ^ [ a - f 0 - 9 ] { 40 } $ / iu. test ( parents [ 0 ] ?? '' )
74+ ) {
75+ return parents [ 0 ]
76+ }
77+ } catch {
78+ // A shallow checkout may lack the validation merge or its parents.
79+ }
80+ return undefined
1081}
1182
1283export async function detectCiPullRequestContext ( ) : Promise < CiPullRequestContext > {
@@ -35,6 +106,12 @@ export async function detectCiPullRequestContext(): Promise<CiPullRequestContext
35106 baseCommitHash,
36107 number : Number ( githubMatch [ 1 ] ) ,
37108 provider : 'github' ,
109+ url : envAsString ( environment [ 'GITHUB_REPOSITORY' ] )
110+ ? pullRequestUrl (
111+ `${ envAsString ( environment [ 'GITHUB_SERVER_URL' ] ) || 'https://github.com' } /${ envAsString ( environment [ 'GITHUB_REPOSITORY' ] ) } ` ,
112+ `pull/${ githubMatch [ 1 ] } ` ,
113+ )
114+ : undefined ,
38115 }
39116 }
40117
@@ -48,6 +125,10 @@ export async function detectCiPullRequestContext(): Promise<CiPullRequestContext
48125 envAsString ( environment [ 'CI_MERGE_REQUEST_DIFF_BASE_SHA' ] ) || undefined ,
49126 number : gitlabNumber ,
50127 provider : 'gitlab' ,
128+ url : pullRequestUrl (
129+ envAsString ( environment [ 'CI_PROJECT_URL' ] ) ,
130+ `-/merge_requests/${ gitlabNumber } ` ,
131+ ) ,
51132 }
52133 }
53134
@@ -62,9 +143,14 @@ export async function detectCiPullRequestContext(): Promise<CiPullRequestContext
62143 / ^ r e f s \/ h e a d s \/ / u,
63144 '' ,
64145 ) || undefined ,
65- baseCommitHash : undefined ,
146+ baseCommitHash : await azureBaseCommit ( azureNumber ) ,
66147 number : azureNumber ,
67148 provider : 'azure' ,
149+ url : azurePullRequestUrl (
150+ envAsString ( environment [ 'BUILD_REPOSITORY_URI' ] ) ||
151+ envAsString ( environment [ 'SYSTEM_PULLREQUEST_TARGETREPOSITORYURI' ] ) ,
152+ azureNumber ,
153+ ) ,
68154 }
69155 }
70156
@@ -73,5 +159,6 @@ export async function detectCiPullRequestContext(): Promise<CiPullRequestContext
73159 baseCommitHash : undefined ,
74160 number : 0 ,
75161 provider : undefined ,
162+ url : undefined ,
76163 }
77164}
0 commit comments