microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
eng/emitters/scripts/Detect-Api-Changes.ps1
147lines · modecode
| 1 | # cSpell:ignore PULLREQUEST |
| 2 | # cSpell:ignore TARGETBRANCH |
| 3 | [CmdletBinding()] |
| 4 | Param ( |
| 5 | [Parameter(Mandatory=$True)] |
| 6 | [string] $ArtifactPath, |
| 7 | [Parameter(Mandatory=$True)] |
| 8 | [string] $PullRequestNumber, |
| 9 | [Parameter(Mandatory=$True)] |
| 10 | [string] $BuildId, |
| 11 | [Parameter(Mandatory=$True)] |
| 12 | [string] $CommitSha, |
| 13 | [Parameter(Mandatory=$True)] |
| 14 | [array] $ArtifactList, |
| 15 | [string] $APIViewUri, |
| 16 | [string] $RepoFullName = "", |
| 17 | [string] $BuildArtifactName = "", |
| 18 | [string] $TargetBranch = ("origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" -replace "refs/heads/"), |
| 19 | [string] $DevopsProject = "internal", |
| 20 | [string] $LanguageShortName = "Unknown" |
| 21 | ) |
| 22 | |
| 23 | . (Join-Path $PSScriptRoot "../../common/scripts/git-helpers.ps1") |
| 24 | |
| 25 | if ($LanguageShortName -eq "Unknown") |
| 26 | { |
| 27 | Write-Host "Language short name is not provided. Please provide the language short name." |
| 28 | exit 1 |
| 29 | } |
| 30 | else |
| 31 | { |
| 32 | $functionScriptPath = Join-Path $PSScriptRoot "/../../../packages/http-client-$LanguageShortName/eng/scripts/Functions.ps1" |
| 33 | if (!(Test-Path $functionScriptPath)) |
| 34 | { |
| 35 | Write-Host "Functions script path $($functionScriptPath) is invalid." |
| 36 | exit 1 |
| 37 | } |
| 38 | . ($functionScriptPath) |
| 39 | } |
| 40 | |
| 41 | # Submit API review request and return status whether current revision is approved or pending or failed to create review |
| 42 | function Submit-Request($filePath, $packageName) |
| 43 | { |
| 44 | $repoName = $RepoFullName |
| 45 | if (!$repoName) { |
| 46 | $repoName = "microsoft/typespec" |
| 47 | } |
| 48 | $isJsPackage = $packageName.StartsWith("typespec-") |
| 49 | $reviewFileName = $isJsPackage ? "$($packageName)_js.json" : "$($packageName)_$($LanguageShortName).json" |
| 50 | $query = [System.Web.HttpUtility]::ParseQueryString('') |
| 51 | $query.Add('artifactName', $BuildArtifactName) |
| 52 | $query.Add('buildId', $BuildId) |
| 53 | $query.Add('filePath', $filePath) |
| 54 | $query.Add('commitSha', $CommitSha) |
| 55 | $query.Add('repoName', $repoName) |
| 56 | $query.Add('pullRequestNumber', $PullRequestNumber) |
| 57 | $query.Add('packageName', $packageName) |
| 58 | $query.Add('language', $isJsPackage ? "js" : $LanguageShortName) |
| 59 | $query.Add('project', $DevopsProject) |
| 60 | $reviewFileFullName = Join-Path -Path $ArtifactPath $reviewFileName |
| 61 | if (Test-Path $reviewFileFullName) |
| 62 | { |
| 63 | $query.Add('codeFile', $reviewFileName) |
| 64 | } |
| 65 | $uri = [System.UriBuilder]$APIViewUri |
| 66 | $uri.query = $query.toString() |
| 67 | Write-Host "Request URI: $($uri.Uri.OriginalString)" |
| 68 | try |
| 69 | { |
| 70 | $Response = Invoke-WebRequest -Method 'GET' -Uri $uri.Uri -MaximumRetryCount 3 |
| 71 | $StatusCode = $Response.StatusCode |
| 72 | } |
| 73 | catch |
| 74 | { |
| 75 | Write-Host "Error $StatusCode - Exception details: $($_.Exception.Response)" |
| 76 | $StatusCode = $_.Exception.Response.StatusCode |
| 77 | } |
| 78 | |
| 79 | return $StatusCode |
| 80 | } |
| 81 | |
| 82 | function Should-Process-Package($packageName) |
| 83 | { |
| 84 | $configFileDir = Join-Path -Path $ArtifactPath "PackageInfo" |
| 85 | $pkgPropPath = Join-Path -Path $configFileDir "$packageName.json" |
| 86 | if (!(Test-Path $pkgPropPath)) |
| 87 | { |
| 88 | Write-Host " Package property file path $($pkgPropPath) is invalid." |
| 89 | return $False |
| 90 | } |
| 91 | # Get package info from json file created before updating version to daily dev |
| 92 | $pkgInfo = Get-Content $pkgPropPath | ConvertFrom-Json |
| 93 | $packagePath = $pkgInfo.DirectoryPath |
| 94 | $modifiedFiles = @(Get-ChangedFiles -DiffPath "$packagePath/*" -DiffFilterType '') |
| 95 | $filteredFileCount = $modifiedFiles.Count |
| 96 | Write-Host "Number of modified files for package: $filteredFileCount" |
| 97 | return ($filteredFileCount -gt 0 -and $pkgInfo.IsNewSdk) |
| 98 | } |
| 99 | |
| 100 | function Log-Input-Params() |
| 101 | { |
| 102 | Write-Host "Artifact Path: $($ArtifactPath)" |
| 103 | Write-Host "Artifact Name: $($BuildArtifactName)" |
| 104 | Write-Host "PullRequest Number: $($PullRequestNumber)" |
| 105 | Write-Host "BuildId: $($BuildId)" |
| 106 | Write-Host "Language: $($Language)" |
| 107 | Write-Host "Commit SHA: $($CommitSha)" |
| 108 | Write-Host "Repo Name: $($RepoFullName)" |
| 109 | Write-Host "Project: $($DevopsProject)" |
| 110 | } |
| 111 | |
| 112 | Log-Input-Params |
| 113 | |
| 114 | $responses = @{} |
| 115 | foreach ($artifact in $ArtifactList) |
| 116 | { |
| 117 | Write-Host "Processing $($artifact.name)" |
| 118 | $packages = Find-Artifacts-For-Apireview $ArtifactPath $artifact.name |
| 119 | if ($packages) |
| 120 | { |
| 121 | $pkgPath = $packages.Values[0] |
| 122 | $isRequired = Should-Process-Package -packageName $artifact.name |
| 123 | Write-Host "Is API change detect required for $($artifact.name):$($isRequired)" |
| 124 | if ($isRequired -eq $True) |
| 125 | { |
| 126 | $filePath = $pkgPath.Replace($ArtifactPath , "").Replace("\", "/") |
| 127 | $respCode = Submit-Request -filePath $filePath -packageName $artifact.name |
| 128 | if ($respCode -ne '200') |
| 129 | { |
| 130 | $responses[$artifact.name] = $respCode |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | Write-Host "Pull request does not have any change for $($artifact.name). Skipping API change detect." |
| 136 | } |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | Write-Host "No package is found in artifact path to find API changes for $($artifact.name)" |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | foreach($pkg in $responses.keys) |
| 145 | { |
| 146 | Write-Host "API detection request status for $($pkg) : $($responses[$pkg])" |
| 147 | } |
| 148 | |