Skip to content

Commit c80b72d

Browse files
authored
Sync eng/common directory with azure-sdk-tools for PR 1287 (Azure#17630)
* Move common code to create API review into eng common in tools
1 parent f2dc485 commit c80b72d

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
parameters:
2+
ArtifactPath: $(Build.ArtifactStagingDirectory)
3+
Artifacts: []
4+
5+
steps:
6+
- ${{ each artifact in parameters.Artifacts }}:
7+
- task: Powershell@2
8+
inputs:
9+
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Create-APIReview.ps1
10+
arguments: >
11+
-ArtifactPath ${{parameters.ArtifactPath}}
12+
-APIViewUri $(azuresdk-apiview-uri)
13+
-APIKey $(azuresdk-apiview-apikey)
14+
-APILabel "Auto Review - $(Build.SourceVersion)"
15+
-PackageName ${{artifact.name}}
16+
pwsh: true
17+
workingDirectory: $(Pipeline.Workspace)
18+
displayName: Create API Review for ${{ artifact.name}}
19+
condition: and(succeededOrFailed(), ne(variables['Skip.CreateApiReview'], 'true') , ne(variables['Build.Reason'],'PullRequest'), eq(variables['System.TeamProject'], 'internal'))
20+
continueOnError: true
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

eng/common/scripts/common.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ $GetPackageInfoFromRepoFn = "Get-${Language}-PackageInfoFromRepo"
3434
$GetPackageInfoFromPackageFileFn = "Get-${Language}-PackageInfoFromPackageFile"
3535
$PublishGithubIODocsFn = "Publish-${Language}-GithubIODocs"
3636
$UpdateDocCIFn = "Update-${Language}-CIConfig"
37-
$GetGithubIoDocIndexFn = "Get-${Language}-GithubIoDocIndex"
37+
$GetGithubIoDocIndexFn = "Get-${Language}-GithubIoDocIndex"
38+
$FindArtifactForApiReviewFn = "Find-${Language}-Artifacts-For-Apireview"

0 commit comments

Comments
 (0)