11#!/usr/bin/env node
22
3- import * as fs from "fs" ;
3+ import { existsSync , readFileSync } from "fs" ;
44import { glob } from "glob" ;
5- import * as path from "path" ;
5+ import { dirname , join , resolve } from "path" ;
66
77import * as commonmark from "commonmark" ;
88import yaml from "js-yaml" ;
@@ -23,10 +23,12 @@ import { Label, LabelContext, PRType } from "./labelling-types.js";
2323import { ImpactAssessment } from "./ImpactAssessment.js" ;
2424import { PRContext } from "./PRContext.js" ;
2525
26+ import { dataPlane , resourceManager } from "@azure-tools/specs-shared/changed-files" ;
2627import { Readme } from "@azure-tools/specs-shared/readme" ;
2728import { Octokit } from "@octokit/rest" ;
2829
2930// todo: we need to populate this so that we can tell if it's a new APIVersion down stream
31+ // TODO: move to .github/shared
3032export async function isNewApiVersion ( context : PRContext ) : Promise < boolean > {
3133 const handlers : ChangeHandler [ ] = [ ] ;
3234 let isAddingNewApiVersion = false ;
@@ -36,7 +38,7 @@ export async function isNewApiVersion(context: PRContext): Promise<boolean> {
3638
3739 const createSwaggerFileHandler = ( ) => {
3840 return ( e : PRChange ) => {
39- if ( e . changeType === " Addition" ) {
41+ if ( e . changeType === ChangeTypes . Addition ) {
4042 const apiVersion = getApiVersionFromSwaggerFile ( e . filePath ) ;
4143 if ( apiVersion ) {
4244 apiVersionSet . add ( apiVersion ) ;
@@ -46,7 +48,7 @@ export async function isNewApiVersion(context: PRContext): Promise<boolean> {
4648 rpFolders . add ( rpFolder ) ;
4749 }
4850 console . log ( `apiVersion: ${ apiVersion } , rpFolder: ${ rpFolder } ` ) ;
49- } else if ( e . changeType === " Update" ) {
51+ } else if ( e . changeType === ChangeTypes . Update ) {
5052 const rpFolder = getRPFolderFromSwaggerFile ( e . filePath ) ;
5153 if ( rpFolder !== undefined ) {
5254 rpFolders . add ( rpFolder ) ;
@@ -69,7 +71,7 @@ export async function isNewApiVersion(context: PRContext): Promise<boolean> {
6971 return false ;
7072 }
7173
72- const targetBranchRPFolder = path . resolve ( context . targetDirectory , firstRPFolder ) ;
74+ const targetBranchRPFolder = resolve ( context . targetDirectory , firstRPFolder ) ;
7375
7476 console . log ( `targetBranchRPFolder: ${ targetBranchRPFolder } ` ) ;
7577
@@ -156,11 +158,11 @@ export async function evaluateImpact(
156158}
157159
158160export function isManagementPR ( filePaths : string [ ] ) : boolean {
159- return filePaths . some ( ( it ) => it . includes ( "resource-manager" ) ) ;
161+ return filePaths . some ( resourceManager ) ;
160162}
161163
162164export function isDataPlanePR ( filePaths : string [ ] ) : boolean {
163- return filePaths . some ( ( it ) => it . includes ( "data-plane" ) ) ;
165+ return filePaths . some ( dataPlane ) ;
164166}
165167
166168export function getAllApiVersionFromRPFolder ( rpFolder : string ) : string [ ] {
@@ -181,7 +183,7 @@ export function getAllApiVersionFromRPFolder(rpFolder: string): string[] {
181183}
182184
183185export function getApiVersionFromSwaggerFile ( swaggerFile : string ) : string | undefined {
184- const swagger = fs . readFileSync ( swaggerFile ) . toString ( ) ;
186+ const swagger = readFileSync ( swaggerFile ) . toString ( ) ;
185187 const swaggerObject = JSON . parse ( swagger ) ;
186188 if ( swaggerObject [ "info" ] && swaggerObject [ "info" ] [ "version" ] ) {
187189 return swaggerObject [ "info" ] [ "version" ] ;
@@ -250,7 +252,10 @@ async function processTypeSpec(ctx: PRContext, labelContext: LabelContext): Prom
250252 } ;
251253 const swaggerFileHandler = ( ) => {
252254 return ( prChange : PRChange ) => {
253- if ( prChange . changeType !== "Deletion" && isSwaggerGeneratedByTypeSpec ( prChange . filePath ) ) {
255+ if (
256+ prChange . changeType !== ChangeTypes . Deletion &&
257+ isSwaggerGeneratedByTypeSpec ( prChange . filePath )
258+ ) {
254259 typeSpecLabel . shouldBePresent = true ;
255260 }
256261 } ;
@@ -268,7 +273,7 @@ async function processTypeSpec(ctx: PRContext, labelContext: LabelContext): Prom
268273
269274function isSwaggerGeneratedByTypeSpec ( swaggerFilePath : string ) : boolean {
270275 try {
271- return ! ! JSON . parse ( fs . readFileSync ( swaggerFilePath ) . toString ( ) ) ?. info [ "x-typespec-generated" ] ;
276+ return ! ! JSON . parse ( readFileSync ( swaggerFilePath ) . toString ( ) ) ?. info [ "x-typespec-generated" ] ;
272277 } catch {
273278 return false ;
274279 }
@@ -329,22 +334,28 @@ export async function getPRChanges(ctx: PRContext): Promise<PRChange[]> {
329334 }
330335
331336 function genChanges ( type : FileTypes , diffs : DiffResult < string > ) {
332- newChanges ( type , " Addition" , diffs . additions ) ;
333- newChanges ( type , " Deletion" , diffs . deletions ) ;
334- newChanges ( type , " Update" , diffs . changes ) ;
337+ newChanges ( type , ChangeTypes . Addition , diffs . additions ) ;
338+ newChanges ( type , ChangeTypes . Deletion , diffs . deletions ) ;
339+ newChanges ( type , ChangeTypes . Update , diffs . changes ) ;
335340 }
336341
337342 function genReadmeChanges ( readmeDiffs ?: DiffResult < ReadmeTag > ) {
338343 if ( readmeDiffs ) {
339- readmeDiffs . additions ?. forEach ( ( d ) => newChange ( "ReadmeFile" , "Addition" , d . readme , d . tags ) ) ;
340- readmeDiffs . changes ?. forEach ( ( d ) => newChange ( "ReadmeFile" , "Update" , d . readme , d . tags ) ) ;
341- readmeDiffs . deletions ?. forEach ( ( d ) => newChange ( "ReadmeFile" , "Deletion" , d . readme , d . tags ) ) ;
344+ readmeDiffs . additions ?. forEach ( ( d ) =>
345+ newChange ( FileTypes . ReadmeFile , ChangeTypes . Addition , d . readme , d . tags ) ,
346+ ) ;
347+ readmeDiffs . changes ?. forEach ( ( d ) =>
348+ newChange ( FileTypes . ReadmeFile , ChangeTypes . Update , d . readme , d . tags ) ,
349+ ) ;
350+ readmeDiffs . deletions ?. forEach ( ( d ) =>
351+ newChange ( FileTypes . ReadmeFile , ChangeTypes . Deletion , d . readme , d . tags ) ,
352+ ) ;
342353 }
343354 }
344355
345- genChanges ( " SwaggerFile" , ctx . getSwaggerDiffs ( ) ) ;
346- genChanges ( " TypeSpecFile" , ctx . getTypeSpecDiffs ( ) ) ;
347- genChanges ( " ExampleFile" , ctx . getExampleDiffs ( ) ) ;
356+ genChanges ( FileTypes . SwaggerFile , ctx . getSwaggerDiffs ( ) ) ;
357+ genChanges ( FileTypes . TypeSpecFile , ctx . getTypeSpecDiffs ( ) ) ;
358+ genChanges ( FileTypes . ExampleFile , ctx . getExampleDiffs ( ) ) ;
348359 genReadmeChanges ( await ctx . getReadmeDiffs ( ) ) ;
349360
350361 console . log ( "RETURN definition getPRChanges" ) ;
@@ -362,11 +373,11 @@ async function processPRType(
362373 const types : PRType [ ] = await getPRType ( context ) ;
363374
364375 const resourceManagerLabelShouldBePresent = processPRTypeLabel (
365- "resource-manager" ,
376+ PRType . ResourceManager ,
366377 types ,
367378 labelContext ,
368379 ) ;
369- const dataPlaneShouldBePresent = processPRTypeLabel ( "data-plane" , types , labelContext ) ;
380+ const dataPlaneShouldBePresent = processPRTypeLabel ( PRType . DataPlane , types , labelContext ) ;
370381
371382 console . log ( "RETURN definition processPRType" ) ;
372383 return { resourceManagerLabelShouldBePresent , dataPlaneShouldBePresent } ;
@@ -383,10 +394,10 @@ async function getPRType(context: PRContext): Promise<PRType[]> {
383394 const prTypes : PRType [ ] = [ ] ;
384395 if ( changedFilePaths . length > 0 ) {
385396 if ( isDataPlanePR ( changedFilePaths ) ) {
386- prTypes . push ( "data-plane" ) ;
397+ prTypes . push ( PRType . DataPlane ) ;
387398 }
388399 if ( isManagementPR ( changedFilePaths ) ) {
389- prTypes . push ( "resource-manager" ) ;
400+ prTypes . push ( PRType . ResourceManager ) ;
390401 }
391402 }
392403 console . log ( "RETURN definition getPRType" ) ;
@@ -432,11 +443,11 @@ async function processSuppression(context: PRContext, labelContext: LabelContext
432443 const createReadmeFileHandler = ( ) => {
433444 return ( e : PRChange ) => {
434445 if (
435- ( e . changeType === " Addition" && getSuppressions ( e . filePath ) . length ) ||
436- ( e . changeType === " Update" &&
446+ ( e . changeType === ChangeTypes . Addition && getSuppressions ( e . filePath ) . length ) ||
447+ ( e . changeType === ChangeTypes . Update &&
437448 diffSuppression (
438- path . resolve ( context . targetDirectory , e . filePath ) ,
439- path . resolve ( context . sourceDirectory , e . filePath ) ,
449+ resolve ( context . targetDirectory , e . filePath ) ,
450+ resolve ( context . sourceDirectory , e . filePath ) ,
440451 ) . length )
441452 ) {
442453 suppressionReviewRequiredLabel . shouldBePresent = true ;
@@ -484,7 +495,7 @@ function getSuppressions(readmePath: string) {
484495 } ;
485496 let suppressionResult : any [ ] = [ ] ;
486497 try {
487- const readme = fs . readFileSync ( readmePath ) . toString ( ) ;
498+ const readme = readFileSync ( readmePath ) . toString ( ) ;
488499 const codeBlocks = getAllCodeBlockNodes ( new commonmark . Parser ( ) . parse ( readme ) ) ;
489500 for ( const block of codeBlocks ) {
490501 if ( block . literal ) {
@@ -534,8 +545,8 @@ async function processRPaaS(
534545 const createReadmeFileHandler = ( ) => {
535546 return async ( e : PRChange ) => {
536547 if (
537- e . changeType !== " Deletion" &&
538- ( await isRPSaaS ( path . join ( context . sourceDirectory , e . filePath ) ) )
548+ e . changeType !== ChangeTypes . Deletion &&
549+ ( await isRPSaaS ( join ( context . sourceDirectory , e . filePath ) ) )
539550 ) {
540551 rpaasLabel . shouldBePresent = true ;
541552 }
@@ -583,12 +594,12 @@ async function processNewRPNamespace(
583594 if ( ! skip ) {
584595 const createSwaggerFileHandler = ( ) => {
585596 return ( e : PRChange ) => {
586- if ( e . changeType === " Addition" ) {
587- const rpFolder = getRPFolderFromSwaggerFile ( path . dirname ( e . filePath ) ) ;
597+ if ( e . changeType === ChangeTypes . Addition ) {
598+ const rpFolder = getRPFolderFromSwaggerFile ( dirname ( e . filePath ) ) ;
588599 console . log ( `Processing newRPNameSpace rpFolder: ${ rpFolder } ` ) ;
589600 if ( rpFolder !== undefined ) {
590- const rpFolderFullPath = path . resolve ( context . targetDirectory , rpFolder ) ;
591- if ( ! fs . existsSync ( rpFolderFullPath ) ) {
601+ const rpFolderFullPath = resolve ( context . targetDirectory , rpFolder ) ;
602+ if ( ! existsSync ( rpFolderFullPath ) ) {
592603 console . log ( `Adding newRPNameSpace rpFolder: ${ rpFolder } ` ) ;
593604 newRPNamespaceLabel . shouldBePresent = true ;
594605 }
@@ -756,7 +767,7 @@ async function processRpaasRpNotInPrivateRepoLabel(
756767
757768 const processPrChange = ( ) => {
758769 return ( e : PRChange ) => {
759- if ( e . changeType === " Addition" ) {
770+ if ( e . changeType === ChangeTypes . Addition ) {
760771 const rpFolderName = getRPRootFolderName ( e . filePath ) ;
761772 console . log (
762773 `Processing processRpaasRpNotInPrivateRepoLabel rpFolderName: ${ rpFolderName } ` ,
0 commit comments