From eed2f89397bf4eb7318570a52ed36195fbe5da2b Mon Sep 17 00:00:00 2001 From: praveenkuttappan Date: Wed, 16 Dec 2020 01:15:34 -0800 Subject: [PATCH 1/3] Move common code into eng common in tools --- .../templates/steps/create-apireview.yml | 19 ++++ eng/common/scripts/Create-APIReview.ps1 | 103 ++++++++++++++++++ eng/common/scripts/common.ps1 | 3 +- 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 eng/common/pipelines/templates/steps/create-apireview.yml create mode 100644 eng/common/scripts/Create-APIReview.ps1 diff --git a/eng/common/pipelines/templates/steps/create-apireview.yml b/eng/common/pipelines/templates/steps/create-apireview.yml new file mode 100644 index 000000000000..1beeabf5b63b --- /dev/null +++ b/eng/common/pipelines/templates/steps/create-apireview.yml @@ -0,0 +1,19 @@ +parameters: + ArtifactPath: $(Build.ArtifactStagingDirectory) + Artifacts: [] + +steps: + - ${{ each artifact in parameters.Artifacts }}: + - task: Powershell@2 + inputs: + filePath: $(Build.SourcesDirectory)/eng/common/scripts/Create-APIReview.ps1 + arguments: > + -ArtifactPath ${{parameters.ArtifactPath}} + -APIViewUri $(azuresdk-apiview-uri) + -APIKey $(azuresdk-apiview-apikey) + -APILabel "Auto Review - $(Build.SourceVersion)" + -PackageName ${{artifact.name}} + pwsh: true + workingDirectory: $(Pipeline.Workspace) + displayName: Create API Review for ${{ artifact.name}} + condition: and(succeededOrFailed(), ne(variables['Skip.CreateApiReview'], 'true') , ne(variables['Build.Reason'],'PullRequest'), eq(variables['System.TeamProject'], 'internal')) diff --git a/eng/common/scripts/Create-APIReview.ps1 b/eng/common/scripts/Create-APIReview.ps1 new file mode 100644 index 000000000000..ecc54b3cfc02 --- /dev/null +++ b/eng/common/scripts/Create-APIReview.ps1 @@ -0,0 +1,103 @@ +[CmdletBinding()] +Param ( + [Parameter(Mandatory=$True)] + [string] $ArtifactPath, + [Parameter(Mandatory=$True)] + [string] $APIViewUri, + [Parameter(Mandatory=$True)] + [string] $APIKey, + [Parameter(Mandatory=$True)] + [string] $APILabel, + [string] $PackageName = "" +) + + +# Submit API review request and return status whether current revision is approved or pending or failed to create review +function Submit-APIReview($packagename, $filePath, $uri, $apiKey, $apiLabel) +{ + $multipartContent = [System.Net.Http.MultipartFormDataContent]::new() + $FileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open) + $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") + $fileHeader.Name = "file" + $fileHeader.FileName = $packagename + $fileContent = [System.Net.Http.StreamContent]::new($FileStream) + $fileContent.Headers.ContentDisposition = $fileHeader + $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("application/octet-stream") + $multipartContent.Add($fileContent) + + + $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") + $stringHeader.Name = "label" + $StringContent = [System.Net.Http.StringContent]::new($apiLabel) + $StringContent.Headers.ContentDisposition = $stringHeader + $multipartContent.Add($stringContent) + + $headers = @{ + "ApiKey" = $apiKey; + "content-type" = "multipart/form-data" + } + + try + { + $Response = Invoke-WebRequest -Method 'POST' -Uri $uri -Body $multipartContent -Headers $headers + $StatusCode = $Response.StatusCode + } + catch + { + $StatusCode = $_.Exception.Response.StatusCode + } + + return $StatusCode +} + + +. (Join-Path $PSScriptRoot common.ps1) +$packages = @{} +if ($FindArtifactForApiReviewFn -and Test-Path "Function:$FindArtifactForApiReviewFn") +{ + $packages = &$FindArtifactForApiReviewFn $ArtifactPath $PackageName +} +else +{ + Write-Host "Function $($FindArtifactForApiReviewFn) is not found" + exit(1) +} + +$responses = @{} +if ($packages) +{ + foreach($pkg in $packages.Keys) + { + Write-Host "Submitting API Review for package $($pkg)" + Write-Host $packages[$pkg] + $responses[$pkg] = Submit-APIReview -packagename $pkg -filePath $packages[$pkg] -uri $APIViewUri -apiKey $APIKey -apiLabel $APILabel + } +} +else +{ + Write-Host "No package is found in artifact path to submit review request" +} + +$FoundFailure = $False +foreach ($pkgName in $responses.Keys) +{ + $respCode = $responses[$pkgName] + if ($respCode -ne '200') + { + $FoundFailure = $True + if ($respCode -eq '201') + { + Write-Host "API Review is pending for package $pkgName" + } + else + { + Write-Host "Failed to create API Review for package $pkgName" + } + } +} +if ($FoundFailure) +{ + Write-Host "Atleast one API review is not yet approved" +} + + diff --git a/eng/common/scripts/common.ps1 b/eng/common/scripts/common.ps1 index fd9f40637d05..e83b9a32e4fe 100644 --- a/eng/common/scripts/common.ps1 +++ b/eng/common/scripts/common.ps1 @@ -34,4 +34,5 @@ $GetPackageInfoFromRepoFn = "Get-${Language}-PackageInfoFromRepo" $GetPackageInfoFromPackageFileFn = "Get-${Language}-PackageInfoFromPackageFile" $PublishGithubIODocsFn = "Publish-${Language}-GithubIODocs" $UpdateDocCIFn = "Update-${Language}-CIConfig" -$GetGithubIoDocIndexFn = "Get-${Language}-GithubIoDocIndex" \ No newline at end of file +$GetGithubIoDocIndexFn = "Get-${Language}-GithubIoDocIndex" +$FindArtifactForApiReviewFn = "Find-${Language}-Artifacts-For-Apireview" \ No newline at end of file From ea5126809fc1d4cb51c50cda3d97947718e01c17 Mon Sep 17 00:00:00 2001 From: praveenkuttappan Date: Thu, 17 Dec 2020 10:11:45 -0800 Subject: [PATCH 2/3] Fix test path syntax --- eng/common/scripts/Create-APIReview.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/eng/common/scripts/Create-APIReview.ps1 b/eng/common/scripts/Create-APIReview.ps1 index ecc54b3cfc02..a4c0f436e134 100644 --- a/eng/common/scripts/Create-APIReview.ps1 +++ b/eng/common/scripts/Create-APIReview.ps1 @@ -53,13 +53,13 @@ function Submit-APIReview($packagename, $filePath, $uri, $apiKey, $apiLabel) . (Join-Path $PSScriptRoot common.ps1) $packages = @{} -if ($FindArtifactForApiReviewFn -and Test-Path "Function:$FindArtifactForApiReviewFn") +if ($FindArtifactForApiReviewFn -and (Test-Path "Function:$FindArtifactForApiReviewFn")) { $packages = &$FindArtifactForApiReviewFn $ArtifactPath $PackageName } else { - Write-Host "Function $($FindArtifactForApiReviewFn) is not found" + Write-Host "Function 'FindArtifactForApiReviewFn' is not found" exit(1) } @@ -99,5 +99,3 @@ if ($FoundFailure) { Write-Host "Atleast one API review is not yet approved" } - - From 261da1b9607c814b732507bbdc3b851145a12c44 Mon Sep 17 00:00:00 2001 From: praveenkuttappan Date: Thu, 17 Dec 2020 11:32:29 -0800 Subject: [PATCH 3/3] Add error handling --- eng/common/pipelines/templates/steps/create-apireview.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/eng/common/pipelines/templates/steps/create-apireview.yml b/eng/common/pipelines/templates/steps/create-apireview.yml index 1beeabf5b63b..7628b22a1951 100644 --- a/eng/common/pipelines/templates/steps/create-apireview.yml +++ b/eng/common/pipelines/templates/steps/create-apireview.yml @@ -17,3 +17,4 @@ steps: workingDirectory: $(Pipeline.Workspace) displayName: Create API Review for ${{ artifact.name}} condition: and(succeededOrFailed(), ne(variables['Skip.CreateApiReview'], 'true') , ne(variables['Build.Reason'],'PullRequest'), eq(variables['System.TeamProject'], 'internal')) + continueOnError: true