Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get api review approval status for a given package version #7824

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,30 @@ 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 apiRevisions = null;

if (!string.IsNullOrEmpty(packageVersion))
{
apiRevisions = await _apiRevisionsManager.GetRevisionForPackageVersionAsync(reviewId: review.Id, packageVersion: packageVersion, apiRevisionType: APIRevisionType.Automatic);
}

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

// Return 200 OK for approved review and 201 for review in pending status
if (firstReleaseStatusOnly != true && latestAutomaticApiRevisions != null && latestAutomaticApiRevisions.IsApproved)
if (firstReleaseStatusOnly != true && apiRevisions != null && apiRevisions.IsApproved)
{
return Ok();
}
Expand Down
38 changes: 38 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Managers/APIRevisionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,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 All @@ -839,5 +840,42 @@ public async Task<APIRevisionListItemModel> UpdateRevisionMetadataAsync(APIRevis
}
return revision;
}


/// <summary>
/// Retrieve the latest APIRevison for a particular Review with a matching package version(atleast major and minor version match).
/// Filter by APIRevisionType if specified and Review contains specified type
/// If APIRevisionType is not specified, return the latest revision irrespective of the type
/// Return default if no revision is found
/// </summary>
/// <param name="reviewId"></param>
/// <param name="packageVersion"></param>
/// <param name="apiRevisionType"></param>
/// <returns>APIRevisionListItemModel</returns>
public async Task<APIRevisionListItemModel> GetRevisionForPackageVersionAsync(string reviewId, string packageVersion, APIRevisionType apiRevisionType = APIRevisionType.All)
{
var apiRevisions = await _apiRevisionsRepository.GetAPIRevisionsAsync(reviewId);
if (apiRevisionType != APIRevisionType.All && apiRevisions.Any(r => r.APIRevisionType == apiRevisionType))
{
apiRevisions = apiRevisions.Where(r => r.APIRevisionType == apiRevisionType);
// 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).First();
}

// 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).First();
}
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ public Task<APIRevisionListItemModel> CreateAPIRevisionAsync(string userName, st
public Task AssignReviewersToAPIRevisionAsync(ClaimsPrincipal User, string apiRevisionId, HashSet<string> reviewers);
public Task<IEnumerable<APIRevisionListItemModel>> GetAPIRevisionsAssignedToUser(string userName);
public Task<APIRevisionListItemModel> UpdateRevisionMetadataAsync(APIRevisionListItemModel revision, string packageVersion, string label);
public Task<APIRevisionListItemModel> GetRevisionForPackageVersionAsync(string reviewId, string packageVersion, APIRevisionType apiRevisionType);
praveenkuttappan marked this conversation as resolved.
Show resolved Hide resolved
}
}