@@ -21,6 +21,17 @@ const AzureDevOpsPullRequestSchema = Schema.Struct({
2121 pullRequestId : PositiveInt ,
2222 title : TrimmedNonEmptyString ,
2323 url : Schema . optional ( Schema . String ) ,
24+ repository : Schema . optional (
25+ Schema . Struct ( {
26+ name : Schema . optional ( Schema . String ) ,
27+ webUrl : Schema . optional ( Schema . String ) ,
28+ project : Schema . optional (
29+ Schema . Struct ( {
30+ name : Schema . optional ( Schema . String ) ,
31+ } ) ,
32+ ) ,
33+ } ) ,
34+ ) ,
2435 sourceRefName : TrimmedNonEmptyString ,
2536 targetRefName : TrimmedNonEmptyString ,
2637 status : Schema . String ,
@@ -57,13 +68,74 @@ function normalizeAzureDevOpsPullRequestState(status: string): "open" | "closed"
5768 }
5869}
5970
71+ function encodeAzureDevOpsPathSegment ( segment : string ) : string {
72+ return encodeURIComponent ( segment ) ;
73+ }
74+
75+ function azureDevOpsOrganizationBaseFromRestApiUrl (
76+ value : string | null | undefined ,
77+ ) : string | null {
78+ const rawUrl = trimOptionalString ( value ) ;
79+ if ( ! rawUrl ) {
80+ return null ;
81+ }
82+
83+ try {
84+ const url = new URL ( rawUrl ) ;
85+ const hostname = url . hostname . toLowerCase ( ) ;
86+ const pathSegments = url . pathname . split ( "/" ) . filter ( ( segment ) => segment . length > 0 ) ;
87+ const isAzureRestUrl = pathSegments . some ( ( segment ) => segment . toLowerCase ( ) === "_apis" ) ;
88+ if ( ! isAzureRestUrl ) {
89+ return null ;
90+ }
91+
92+ if ( hostname === "dev.azure.com" ) {
93+ const organization = pathSegments [ 0 ] ;
94+ return organization ? `${ url . origin } /${ organization } ` : null ;
95+ }
96+
97+ if ( hostname . endsWith ( ".visualstudio.com" ) ) {
98+ return url . origin ;
99+ }
100+
101+ return null ;
102+ } catch {
103+ return null ;
104+ }
105+ }
106+
107+ function normalizeAzureDevOpsPullRequestUrl (
108+ raw : Schema . Schema . Type < typeof AzureDevOpsPullRequestSchema > ,
109+ ) : string {
110+ const webLink = trimOptionalString ( raw . _links ?. web ?. href ) ;
111+ if ( webLink ) {
112+ return webLink ;
113+ }
114+
115+ const repositoryWebUrl = trimOptionalString ( raw . repository ?. webUrl ) ;
116+ if ( repositoryWebUrl ) {
117+ return `${ repositoryWebUrl . replace ( / \/ + $ / , "" ) } /pullrequest/${ raw . pullRequestId } ` ;
118+ }
119+
120+ const organizationBase = azureDevOpsOrganizationBaseFromRestApiUrl ( raw . url ) ;
121+ const projectName = trimOptionalString ( raw . repository ?. project ?. name ) ;
122+ const repositoryName = trimOptionalString ( raw . repository ?. name ) ;
123+ if ( organizationBase && projectName && repositoryName ) {
124+ const encodedProjectName = encodeAzureDevOpsPathSegment ( projectName ) ;
125+ const encodedRepositoryName = encodeAzureDevOpsPathSegment ( repositoryName ) ;
126+ return `${ organizationBase } /${ encodedProjectName } /_git/${ encodedRepositoryName } /pullrequest/${ raw . pullRequestId } ` ;
127+ }
128+
129+ return trimOptionalString ( raw . url ) ?? "" ;
130+ }
131+
60132function normalizeAzureDevOpsPullRequestRecord (
61133 raw : Schema . Schema . Type < typeof AzureDevOpsPullRequestSchema > ,
62134) : NormalizedAzureDevOpsPullRequestRecord {
63135 return {
64136 number : raw . pullRequestId ,
65137 title : raw . title ,
66- url : trimOptionalString ( raw . _links ?. web ?. href ) ?? trimOptionalString ( raw . url ) ?? "" ,
138+ url : normalizeAzureDevOpsPullRequestUrl ( raw ) ,
67139 baseRefName : normalizeRefName ( raw . targetRefName ) ,
68140 headRefName : normalizeRefName ( raw . sourceRefName ) ,
69141 state : normalizeAzureDevOpsPullRequestState ( raw . status ) ,
0 commit comments