From da7860bec15ccb7c83294bd2ba6b7cc8a35c42de Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu <31145988+chidozieononiwu@users.noreply.github.com> Date: Fri, 19 Jan 2024 11:05:50 -0800 Subject: [PATCH] Resolve Error in CSharpLanguageService (#7519) * Resolve Error in CSharpLanguageService * handle case where apiRevision codeFile is empty --- .../APIViewWeb/Helpers/PageModelHelpers.cs | 15 +++++++++++---- .../APIViewWeb/Languages/CSharpLanguageService.cs | 6 +++++- .../LeanModels/ReviewRevisionPageModels.cs | 4 +++- .../APIViewWeb/Pages/Assemblies/Review.cshtml.cs | 7 ++++++- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/dotnet/APIView/APIViewWeb/Helpers/PageModelHelpers.cs b/src/dotnet/APIView/APIViewWeb/Helpers/PageModelHelpers.cs index 1bde8ef0f15..2ca14c16acd 100644 --- a/src/dotnet/APIView/APIViewWeb/Helpers/PageModelHelpers.cs +++ b/src/dotnet/APIView/APIViewWeb/Helpers/PageModelHelpers.cs @@ -257,7 +257,7 @@ public static async Task GetReviewContentAsync( var activeRevision = await reviewRevisionsManager.GetLatestAPIRevisionsAsync(reviewId, apiRevisions, APIRevisionType.Automatic); if (activeRevision == null) { - reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevison; + reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevisonRedirectToIndexPage; reviewPageContent.NotificationMessage = $"Review with ID {reviewId} has no valid APIRevisons"; return reviewPageContent; } @@ -271,7 +271,7 @@ public static async Task GetReviewContentAsync( else { reviewPageContent.NotificationMessage = $"A revision with ID {revisionId} does not exist for review with id {reviewId}"; - reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevison; + reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevisonRedirectToIndexPage; return reviewPageContent; } } @@ -308,14 +308,14 @@ public static async Task GetReviewContentAsync( { getCodeLines = true; reviewPageContent.NotificationMessage = $"There is no diff between the two revisions. {activeRevision.Id} : {diffRevisionId}"; - reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevison; + reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevisonProceedWithPageLoad; } } else { getCodeLines = true; reviewPageContent.NotificationMessage = $"A diffRevision with ID {diffRevisionId} does not exist for this review."; - reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevison; + reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevisonProceedWithPageLoad; } } @@ -324,6 +324,13 @@ public static async Task GetReviewContentAsync( codeLines = CreateLines(diagnostics: fileDiagnostics, lines: activeRevisionHtmlLines, comments: comments); } + if (codeLines == null || codeLines.Length == 0) + { + reviewPageContent.NotificationMessage = $"A revision with ID {activeRevision.Id} has no content."; + reviewPageContent.Directive = ReviewContentModelDirective.ErrorDueToInvalidAPIRevisonRedirectToIndexPage; + return reviewPageContent; + } + HashSet preferredApprovers = new HashSet(); var approverConfig = configuration["approvers"]; if (!string.IsNullOrEmpty(approverConfig)) diff --git a/src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs index cf37326500c..133a3d1ce55 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs @@ -60,6 +60,10 @@ public override Task GetCodeFileAsync(string originalName, Stream stre var nuspecEntry = archive.Entries.Single(entry => IsNuspec(entry.Name)); var dllEntries = archive.Entries.Where(entry => IsDll(entry.Name)).ToArray(); + if (dllEntries.Length == 0) + { + return Task.FromResult(GetDummyReviewCodeFile(originalName, dependencies)); + } var dllEntry = dllEntries.First(); if (dllEntries.Length > 1) @@ -96,7 +100,7 @@ public override Task GetCodeFileAsync(string originalName, Stream stre } var assemblySymbol = CompilationFactory.GetCompilation(dllStream, docStream); - if ( assemblySymbol == null) + if (assemblySymbol == null) { return Task.FromResult(GetDummyReviewCodeFile(originalName, dependencies)); } diff --git a/src/dotnet/APIView/APIViewWeb/LeanModels/ReviewRevisionPageModels.cs b/src/dotnet/APIView/APIViewWeb/LeanModels/ReviewRevisionPageModels.cs index d0735206b0a..7f71f38d5fd 100644 --- a/src/dotnet/APIView/APIViewWeb/LeanModels/ReviewRevisionPageModels.cs +++ b/src/dotnet/APIView/APIViewWeb/LeanModels/ReviewRevisionPageModels.cs @@ -9,7 +9,9 @@ public enum ReviewContentModelDirective { ProceedWithPageLoad = 0, TryGetlegacyReview, - ErrorDueToInvalidAPIRevison + ErrorDueToInvalidAPIRevisonProceedWithPageLoad, + ErrorDueToInvalidAPIRevisonRedirectToIndexPage + } public class ReviewContentModel diff --git a/src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml.cs b/src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml.cs index 3186acb7087..92495041421 100644 --- a/src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml.cs +++ b/src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml.cs @@ -124,11 +124,16 @@ public async Task OnGetAsync(string id, string revisionId = null) } } - if (ReviewContent.Directive == ReviewContentModelDirective.ErrorDueToInvalidAPIRevison) + if (ReviewContent.Directive == ReviewContentModelDirective.ErrorDueToInvalidAPIRevisonProceedWithPageLoad) { NotificationMessage = ReviewContent.NotificationMessage; } + if (ReviewContent.Directive == ReviewContentModelDirective.ErrorDueToInvalidAPIRevisonRedirectToIndexPage) + { + return RedirectToPage("Index", new { notificationMessage = ReviewContent.NotificationMessage }); + } + if (ReviewContent.APIRevisionsGrouped == null || !ReviewContent.APIRevisionsGrouped.Any()) { return RedirectToPage("LegacyReview", new { id = id });