Skip to content

Commit

Permalink
Get api review approval status for a given package version (#7824)
Browse files Browse the repository at this point in the history
* Get api review approval status for a given package version
  • Loading branch information
praveenkuttappan authored Mar 8, 2024
1 parent b970a06 commit 3142efa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
34 changes: 25 additions & 9 deletions src/dotnet/APIView/APIViewWeb/Controllers/AutoReviewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,38 @@ public async Task<ActionResult> UploadAutoReview([FromForm] IFormFile file, stri
return StatusCode(statusCode: StatusCodes.Status500InternalServerError);
}

public async Task<ActionResult> GetReviewStatus(string language, string packageName, string reviewId = null, bool? firstReleaseStatusOnly = null)
public async Task<ActionResult> GetReviewStatus(string language, string packageName, string reviewId = null, bool? firstReleaseStatusOnly = null, string packageVersion = null)
{
// This API is used by prepare release script to check if API review for a package is approved or not.
// This caller script doesn't have artifact to submit and so it can't check using create review API
// So it rely on approval status of latest revision of automatic review for the package
// With new restriction of creating automatic review only from master branch or GA version, this should ensure latest revision
// is infact the version intended to be released.
// This API is used to get approval status of an API review revision. If a package version is passed then it will try to find a revision with exact package version match or revisions with same major and minor version.
// If there is no matching revisions then it will return latest automatic revision details.
// This is used by prepare release script and build pipeline to verify approval status.

ReviewListItemModel review = await _reviewManager.GetReviewAsync(packageName: packageName, language: language, isClosed: null);

if (review != null)
{
APIRevisionListItemModel latestAutomaticApiRevisions = await _apiRevisionsManager.GetLatestAPIRevisionsAsync(reviewId: review.Id, apiRevisionType: APIRevisionType.Automatic);
APIRevisionListItemModel apiRevision = null;

if (!string.IsNullOrEmpty(packageVersion))
{
var apiRevisions = await _apiRevisionsManager.GetAPIRevisionsAsync(reviewId: review.Id, packageVersion: packageVersion, apiRevisionType: APIRevisionType.Automatic);
if (apiRevisions.Any())
apiRevision = apiRevisions.FirstOrDefault();
}

if (apiRevision == null)
{
apiRevision = await _apiRevisionsManager.GetLatestAPIRevisionsAsync(reviewId: review.Id, apiRevisionType: APIRevisionType.Automatic);
}


if(apiRevision == null)
{
return StatusCode(StatusCodes.Status404NotFound, "Review is not found for package " + packageName);
}

// Return 200 OK for approved review and 201 for review in pending status
if (firstReleaseStatusOnly != true && latestAutomaticApiRevisions != null && latestAutomaticApiRevisions.IsApproved)
if (firstReleaseStatusOnly != true && apiRevision != null && apiRevision.IsApproved)
{
return Ok();
}
Expand All @@ -87,7 +103,7 @@ public async Task<ActionResult> GetReviewStatus(string language, string packageN
return StatusCode(statusCode: StatusCodes.Status202Accepted);
}
}
throw new Exception("Review is not found for package " + packageName);
return StatusCode(StatusCodes.Status404NotFound, "Review is not found for package " + packageName);
}

[HttpGet]
Expand Down
32 changes: 30 additions & 2 deletions src/dotnet/APIView/APIViewWeb/Managers/APIRevisionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,37 @@ public async Task<PagedList<APIRevisionListItemModel>> GetAPIRevisionsAsync(Page
/// Retrieve Revisions for a particular Review from the Revisions container in CosmosDb
/// </summary>
/// <param name="reviewId"></param> The Reviewid for which the revisions are to be retrieved
/// <param name="packageVersion"></param> Optional package version param to return a matching revision for the package version
/// <param name="apiRevisionType"></param> optional API revision type filter
/// <returns></returns>
public async Task<IEnumerable<APIRevisionListItemModel>> GetAPIRevisionsAsync(string reviewId)
public async Task<IEnumerable<APIRevisionListItemModel>> GetAPIRevisionsAsync(string reviewId, string packageVersion = "", APIRevisionType apiRevisionType = APIRevisionType.All)
{
return await _apiRevisionsRepository.GetAPIRevisionsAsync(reviewId);
var apiRevisions = await _apiRevisionsRepository.GetAPIRevisionsAsync(reviewId);

if (apiRevisionType != APIRevisionType.All)
apiRevisions = apiRevisions.Where(r => r.APIRevisionType == apiRevisionType);

if (!string.IsNullOrEmpty(packageVersion))
{
// Check for exact same package version
// If exact version is not found in revision then search for same major and minor version and return the latest.
var exactMatchRevisions = apiRevisions.Where(r => packageVersion.Equals(r.Files[0].PackageVersion));
if (exactMatchRevisions.Any())
{
return exactMatchRevisions.OrderByDescending(r => r.CreatedOn);
}

// Check for revisions with matching
var versionGroups = packageVersion.Split('.');
var majorMinor = $"{versionGroups[0]}.{versionGroups[1]}.";
var majorMinorMatchRevisions = apiRevisions.Where(r => !string.IsNullOrEmpty(r.Files[0].PackageVersion) && r.Files[0].PackageVersion.StartsWith(majorMinor));
if (majorMinorMatchRevisions.Any())
{
return majorMinorMatchRevisions.OrderByDescending(r => r.CreatedOn);
}
return majorMinorMatchRevisions;
}
return apiRevisions;
}

/// <summary>
Expand Down Expand Up @@ -829,6 +856,7 @@ private async Task GenerateAPIRevisionInExternalResource(ReviewListItemModel rev
return result;
}


public async Task<APIRevisionListItemModel> UpdateRevisionMetadataAsync(APIRevisionListItemModel revision, string packageVersion, string label)
{
if (packageVersion != null && !packageVersion.Equals(revision.Files[0].PackageVersion))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace APIViewWeb.Managers.Interfaces
public interface IAPIRevisionsManager
{
public Task<PagedList<APIRevisionListItemModel>> GetAPIRevisionsAsync(PageParams pageParams, APIRevisionsFilterAndSortParams filterAndSortParams);
public Task<IEnumerable<APIRevisionListItemModel>> GetAPIRevisionsAsync(string reviewId);
public Task<IEnumerable<APIRevisionListItemModel>> GetAPIRevisionsAsync(string reviewId, string packageVersion = "", APIRevisionType apiRevisionType = APIRevisionType.All);
public Task<APIRevisionListItemModel> GetLatestAPIRevisionsAsync(string reviewId = null, IEnumerable<APIRevisionListItemModel> apiRevisions = null, APIRevisionType apiRevisionType = APIRevisionType.All);
public Task<APIRevisionListItemModel> GetAPIRevisionAsync(ClaimsPrincipal user, string apiRevisionId);
public Task<APIRevisionListItemModel> GetAPIRevisionAsync(string apiRevisionId);
Expand Down

0 comments on commit 3142efa

Please sign in to comment.