@@ -596,7 +596,9 @@ private static DeleteApi GetDeleteApi(IServiceProvider provider)
596596
597597 var taskDictionary = new ConcurrentDictionary < ApiName , AsyncLazy < Unit > > ( ) ;
598598
599- return async ( name , cancellationToken ) =>
599+ return deleteApi ;
600+
601+ async ValueTask deleteApi ( ApiName name , CancellationToken cancellationToken )
600602 {
601603 using var _ = activitySource . StartActivity ( nameof ( DeleteApi ) )
602604 ? . AddTag ( "api.name" , name ) ;
@@ -610,39 +612,40 @@ await taskDictionary.GetOrAdd(name,
610612 . WithCancellation ( cancellationToken ) ;
611613 } ;
612614
613- async ValueTask deleteApiInner ( ApiName name , CancellationToken cancellationToken )
614- {
615- if ( await isApiRevisionNumberCurrentInSourceControl ( name , cancellationToken ) )
616- {
617- logger . LogInformation ( "API {ApiName} is the current revision in source control. Skipping deletion..." , name ) ;
618- return ;
619- }
615+ async ValueTask deleteApiInner ( ApiName name , CancellationToken cancellationToken ) =>
616+ await ApiName . TryParseRevisionedName ( name )
617+ . Map ( async api => await processRevisionedApi ( api . RootName , api . RevisionNumber , cancellationToken ) )
618+ . IfLeft ( async _ => await processRootApi ( name , cancellationToken ) ) ;
620619
620+ async ValueTask processRootApi ( ApiName name , CancellationToken cancellationToken ) =>
621621 await deleteFromApim ( name , cancellationToken ) ;
622- }
623622
624- /// <summary>
625- /// We don't want to delete a revision if it was just made current. For instance:
626- /// 1. Dev has apiA with revision 1 (current) and revision 2. Artifacts folder has:
627- /// - /apis/apiA/apiInformation.json with revision 1 as current
628- /// - /apis/apiA;rev=2/apiInformation.json
629- /// 2. User makes revision 2 current in dev APIM.
630- /// 3. User runs extractor for dev APIM. Artifacts folder has:
631- /// - /apis/apiA/apiInformation.json with revision 2 as current
632- /// - /apis/apiA;rev=1/apiInformation.json
633- /// - /apis/apiA;rev=2 folder gets deleted.
634- /// 4. User runs publisher to prod APIM. We don't want to handle the deletion of folder /apis/apiA;rev=2, as it's the current revision.
635- async ValueTask < bool > isApiRevisionNumberCurrentInSourceControl ( ApiName name , CancellationToken cancellationToken ) =>
636- await ApiName . TryParseRevisionedName ( name )
637- . Match ( async _ => await ValueTask . FromResult ( false ) ,
638- async api =>
639- {
640- var ( rootName , revisionNumber ) = api ;
641- var sourceControlRevisionNumberOption = await tryGetRevisionNumberInSourceControl ( rootName , cancellationToken ) ;
642- return sourceControlRevisionNumberOption
643- . Map ( sourceControlRevisionNumber => sourceControlRevisionNumber == revisionNumber )
644- . IfNone ( false ) ;
645- } ) ;
623+ async ValueTask processRevisionedApi ( ApiName name , ApiRevisionNumber revisionNumber , CancellationToken cancellationToken )
624+ {
625+ var rootName = ApiName . GetRootName ( name ) ;
626+ var currentRevisionNumberOption = await tryGetRevisionNumberInSourceControl ( rootName , cancellationToken ) ;
627+
628+ await currentRevisionNumberOption . Match ( // If the current revision in source control has a different revision number, delete this revision.
629+ // We don't want to delete a revision if it was just made current. For instance:
630+ // 1. Dev has apiA with revision 1 (current) and revision 2. Artifacts folder has:
631+ // - /apis/apiA/apiInformation.json with revision 1 as current
632+ // - /apis/apiA;rev=2/apiInformation.json
633+ // 2. User makes revision 2 current in dev APIM.
634+ // 3. User runs extractor for dev APIM. Artifacts folder has:
635+ // - /apis/apiA/apiInformation.json with revision 2 as current
636+ // - /apis/apiA;rev=1/apiInformation.json
637+ // - /apis/apiA;rev=2 folder gets deleted.
638+ // 4. User runs publisher to prod APIM. We don't want to handle the deletion of folder /apis/apiA;rev=2, as it's the current revision.
639+ async currentRevisionNumber =>
640+ {
641+ if ( currentRevisionNumber != revisionNumber )
642+ {
643+ await deleteFromApim ( name , cancellationToken ) ;
644+ }
645+ } ,
646+ // If there is no current revision in source control, process the root API deletion
647+ async ( ) => await deleteApi ( rootName , cancellationToken ) ) ;
648+ }
646649
647650 async ValueTask < Option < ApiRevisionNumber > > tryGetRevisionNumberInSourceControl ( ApiName name , CancellationToken cancellationToken )
648651 {
@@ -670,8 +673,11 @@ private static DeleteApiFromApim GetDeleteApiFromApim(IServiceProvider provider)
670673 {
671674 logger . LogInformation ( "Deleting API {ApiName}..." , name ) ;
672675
673- await ApiUri . From ( name , serviceUri )
674- . Delete ( pipeline , cancellationToken ) ;
676+ var apiUri = ApiUri . From ( name , serviceUri ) ;
677+
678+ await ( ApiName . IsRevisioned ( name )
679+ ? apiUri . Delete ( pipeline , cancellationToken )
680+ : apiUri . DeleteAllRevisions ( pipeline , cancellationToken ) ) ;
675681 } ;
676682 }
677683}
0 commit comments