Skip to content

Commit a5a2bb7

Browse files
Sync eng/common directory with azure-sdk-tools for PR 13235 (#47543)
* Remove ApiKey usage * Add -TestAuth flag to verify Bearer token authentication * TEMP: Enable TestAuthOnly for pipeline testing * Remove testing logs * Additional clean up * Keep apikey fallback while migrating * Keep migration to new endpoint * Keep migration to new endpoint * Feedback --------- Co-authored-by: Alitzel Mendez <[email protected]> Co-authored-by: Alitzel Mendez <[email protected]>
1 parent 2933ee1 commit a5a2bb7

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

eng/common/pipelines/templates/steps/create-apireview.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,25 @@ steps:
3737
parameters:
3838
WorkingDirectory: ${{ parameters.SourceRootPath }}
3939

40-
- task: Powershell@2
40+
- task: AzureCLI@2
4141
inputs:
42-
filePath: ${{ parameters.SourceRootPath }}/eng/common/scripts/Create-APIReview.ps1
42+
azureSubscription: 'APIView prod deployment'
43+
scriptType: pscore
44+
scriptLocation: scriptPath
45+
scriptPath: ${{ parameters.SourceRootPath }}/eng/common/scripts/Create-APIReview.ps1
4346
# PackageInfoFiles example: @('a/file1.json','a/file2.json')
4447
arguments: >
4548
-PackageInfoFiles @('${{ join(''',''', parameters.PackageInfoFiles) }}')
4649
-ArtifactList ('${{ convertToJson(parameters.Artifacts) }}' | ConvertFrom-Json | Select-Object Name)
4750
-ArtifactPath '${{parameters.ArtifactPath}}'
4851
-ArtifactName ${{ parameters.ArtifactName }}
49-
-APIKey '$(azuresdk-apiview-apikey)'
5052
-PackageName '${{parameters.PackageName}}'
5153
-SourceBranch '$(Build.SourceBranchName)'
5254
-DefaultBranch '$(DefaultBranch)'
5355
-ConfigFileDir '${{parameters.ConfigFileDir}}'
5456
-BuildId '$(Build.BuildId)'
5557
-RepoName '$(Build.Repository.Name)'
5658
-MarkPackageAsShipped $${{parameters.MarkPackageAsShipped}}
57-
pwsh: true
5859
displayName: Create API Review
5960
condition: >-
6061
and(

eng/common/scripts/Create-APIReview.ps1

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,42 @@ Param (
44
[array] $ArtifactList,
55
[Parameter(Mandatory=$True)]
66
[string] $ArtifactPath,
7-
[Parameter(Mandatory=$True)]
8-
[string] $APIKey,
97
[string] $SourceBranch,
108
[string] $DefaultBranch,
119
[string] $RepoName,
1210
[string] $BuildId,
1311
[string] $PackageName = "",
1412
[string] $ConfigFileDir = "",
15-
[string] $APIViewUri = "https://apiview.dev/AutoReview",
13+
[string] $APIViewUri = "https://apiview.dev/autoreview",
1614
[string] $ArtifactName = "packages",
1715
[bool] $MarkPackageAsShipped = $false,
1816
[Parameter(Mandatory=$False)]
1917
[array] $PackageInfoFiles
2018
)
2119

2220
Set-StrictMode -Version 3
21+
2322
. (Join-Path $PSScriptRoot common.ps1)
2423
. (Join-Path $PSScriptRoot Helpers ApiView-Helpers.ps1)
2524

25+
# Get Bearer token for APIView authentication
26+
# In Azure DevOps, this uses the service connection's Managed Identity/Service Principal
27+
function Get-ApiViewBearerToken()
28+
{
29+
try {
30+
$tokenResponse = az account get-access-token --resource "api://apiview" --output json 2>&1
31+
if ($LASTEXITCODE -ne 0) {
32+
Write-Error "Failed to acquire access token: $tokenResponse"
33+
return $null
34+
}
35+
return ($tokenResponse | ConvertFrom-Json).accessToken
36+
}
37+
catch {
38+
Write-Error "Failed to acquire access token: $($_.Exception.Message)"
39+
return $null
40+
}
41+
}
42+
2643
# Submit API review request and return status whether current revision is approved or pending or failed to create review
2744
function Upload-SourceArtifact($filePath, $apiLabel, $releaseStatus, $packageVersion, $packageType)
2845
{
@@ -78,9 +95,17 @@ function Upload-SourceArtifact($filePath, $apiLabel, $releaseStatus, $packageVer
7895
Write-Host "Request param, compareAllRevisions: true"
7996
}
8097

81-
$uri = "${APIViewUri}/UploadAutoReview"
98+
$uri = "${APIViewUri}/upload"
99+
100+
# Get Bearer token for authentication
101+
$bearerToken = Get-ApiViewBearerToken
102+
if (-not $bearerToken) {
103+
Write-Error "Failed to acquire Bearer token for APIView authentication."
104+
return [System.Net.HttpStatusCode]::Unauthorized
105+
}
106+
82107
$headers = @{
83-
"ApiKey" = $apiKey;
108+
"Authorization" = "Bearer $bearerToken";
84109
"content-type" = "multipart/form-data"
85110
}
86111

@@ -115,20 +140,28 @@ function Upload-ReviewTokenFile($packageName, $apiLabel, $releaseStatus, $review
115140
if($MarkPackageAsShipped) {
116141
$params += "&setReleaseTag=true"
117142
}
118-
$uri = "${APIViewUri}/CreateApiReview?${params}"
143+
$uri = "${APIViewUri}/create?${params}"
119144
if ($releaseStatus -and ($releaseStatus -ne "Unreleased"))
120145
{
121146
$uri += "&compareAllRevisions=true"
122147
}
123148

124149
Write-Host "Request to APIView: $uri"
150+
151+
# Get Bearer token for authentication
152+
$bearerToken = Get-ApiViewBearerToken
153+
if (-not $bearerToken) {
154+
Write-Error "Failed to acquire Bearer token for APIView authentication."
155+
return [System.Net.HttpStatusCode]::Unauthorized
156+
}
157+
125158
$headers = @{
126-
"ApiKey" = $APIKey;
159+
"Authorization" = "Bearer $bearerToken"
127160
}
128161

129162
try
130163
{
131-
$Response = Invoke-WebRequest -Method 'GET' -Uri $uri -Headers $headers
164+
$Response = Invoke-WebRequest -Method 'POST' -Uri $uri -Headers $headers
132165
Write-Host "API review: $($Response.Content)"
133166
$StatusCode = $Response.StatusCode
134167
}

0 commit comments

Comments
 (0)