|
| 1 | +[CmdletBinding()] |
| 2 | +Param ( |
| 3 | + [Parameter(Mandatory=$True)] |
| 4 | + [string] $ArtifactPath, |
| 5 | + [Parameter(Mandatory=$True)] |
| 6 | + [string] $APIViewUri, |
| 7 | + [Parameter(Mandatory=$True)] |
| 8 | + [string] $APIKey, |
| 9 | + [Parameter(Mandatory=$True)] |
| 10 | + [string] $APILabel, |
| 11 | + [string] $PackageName = "" |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +# Submit API review request and return status whether current revision is approved or pending or failed to create review |
| 16 | +function Submit-APIReview($packagename, $filePath, $uri, $apiKey, $apiLabel) |
| 17 | +{ |
| 18 | + $multipartContent = [System.Net.Http.MultipartFormDataContent]::new() |
| 19 | + $FileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open) |
| 20 | + $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") |
| 21 | + $fileHeader.Name = "file" |
| 22 | + $fileHeader.FileName = $packagename |
| 23 | + $fileContent = [System.Net.Http.StreamContent]::new($FileStream) |
| 24 | + $fileContent.Headers.ContentDisposition = $fileHeader |
| 25 | + $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("application/octet-stream") |
| 26 | + $multipartContent.Add($fileContent) |
| 27 | + |
| 28 | + |
| 29 | + $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") |
| 30 | + $stringHeader.Name = "label" |
| 31 | + $StringContent = [System.Net.Http.StringContent]::new($apiLabel) |
| 32 | + $StringContent.Headers.ContentDisposition = $stringHeader |
| 33 | + $multipartContent.Add($stringContent) |
| 34 | + |
| 35 | + $headers = @{ |
| 36 | + "ApiKey" = $apiKey; |
| 37 | + "content-type" = "multipart/form-data" |
| 38 | + } |
| 39 | + |
| 40 | + try |
| 41 | + { |
| 42 | + $Response = Invoke-WebRequest -Method 'POST' -Uri $uri -Body $multipartContent -Headers $headers |
| 43 | + $StatusCode = $Response.StatusCode |
| 44 | + } |
| 45 | + catch |
| 46 | + { |
| 47 | + $StatusCode = $_.Exception.Response.StatusCode |
| 48 | + } |
| 49 | + |
| 50 | + return $StatusCode |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +. (Join-Path $PSScriptRoot common.ps1) |
| 55 | +$packages = @{} |
| 56 | +if ($FindArtifactForApiReviewFn -and (Test-Path "Function:$FindArtifactForApiReviewFn")) |
| 57 | +{ |
| 58 | + $packages = &$FindArtifactForApiReviewFn $ArtifactPath $PackageName |
| 59 | +} |
| 60 | +else |
| 61 | +{ |
| 62 | + Write-Host "Function 'FindArtifactForApiReviewFn' is not found" |
| 63 | + exit(1) |
| 64 | +} |
| 65 | + |
| 66 | +$responses = @{} |
| 67 | +if ($packages) |
| 68 | +{ |
| 69 | + foreach($pkg in $packages.Keys) |
| 70 | + { |
| 71 | + Write-Host "Submitting API Review for package $($pkg)" |
| 72 | + Write-Host $packages[$pkg] |
| 73 | + $responses[$pkg] = Submit-APIReview -packagename $pkg -filePath $packages[$pkg] -uri $APIViewUri -apiKey $APIKey -apiLabel $APILabel |
| 74 | + } |
| 75 | +} |
| 76 | +else |
| 77 | +{ |
| 78 | + Write-Host "No package is found in artifact path to submit review request" |
| 79 | +} |
| 80 | + |
| 81 | +$FoundFailure = $False |
| 82 | +foreach ($pkgName in $responses.Keys) |
| 83 | +{ |
| 84 | + $respCode = $responses[$pkgName] |
| 85 | + if ($respCode -ne '200') |
| 86 | + { |
| 87 | + $FoundFailure = $True |
| 88 | + if ($respCode -eq '201') |
| 89 | + { |
| 90 | + Write-Host "API Review is pending for package $pkgName" |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + Write-Host "Failed to create API Review for package $pkgName" |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +if ($FoundFailure) |
| 99 | +{ |
| 100 | + Write-Host "Atleast one API review is not yet approved" |
| 101 | +} |
0 commit comments