...

Text file src/github.com/Azure/azure-sdk-for-go/eng/common/scripts/Detect-Api-Changes.ps1

Documentation: github.com/Azure/azure-sdk-for-go/eng/common/scripts

     1# cSpell:ignore PULLREQUEST
     2# cSpell:ignore TARGETBRANCH
     3[CmdletBinding()]
     4Param (
     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] $ArtifactName = "packages",
    18  [string] $TargetBranch = ("origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" -replace "refs/heads/")
    19)
    20
    21. (Join-Path $PSScriptRoot common.ps1)
    22
    23# Submit API review request and return status whether current revision is approved or pending or failed to create review
    24function Submit-Request($filePath, $packageName)
    25{
    26    $repoName = $RepoFullName
    27    if (!$repoName) {
    28        $repoName = "azure/azure-sdk-for-$LanguageShort"
    29    }
    30    $query = [System.Web.HttpUtility]::ParseQueryString('')
    31    $query.Add('artifactName', $ArtifactName)
    32    $query.Add('buildId', $BuildId)
    33    $query.Add('filePath', $filePath)
    34    $query.Add('commitSha', $CommitSha)
    35    $query.Add('repoName', $repoName)
    36    $query.Add('pullRequestNumber', $PullRequestNumber)
    37    $query.Add('packageName', $packageName)
    38    $uri = [System.UriBuilder]$APIViewUri
    39    $uri.query = $query.toString()
    40    Write-Host "Request URI: $($uri.Uri.OriginalString)"
    41    try
    42    {
    43        $Response = Invoke-WebRequest -Method 'GET' -Uri $uri.Uri -MaximumRetryCount 3
    44        $StatusCode = $Response.StatusCode
    45    }
    46    catch
    47    {
    48        Write-Host "Error $StatusCode - Exception details: $($_.Exception.Response)"
    49        $StatusCode = $_.Exception.Response.StatusCode
    50    }
    51
    52    return $StatusCode
    53}
    54
    55function Should-Process-Package($pkgPath, $packageName)
    56{
    57    $pkg = Split-Path -Leaf $pkgPath
    58    $configFileDir = Join-Path -Path $ArtifactPath "PackageInfo"
    59    $pkgPropPath = Join-Path -Path $configFileDir "$packageName.json"
    60    if (!(Test-Path $pkgPropPath))
    61    {
    62        Write-Host " Package property file path $($pkgPropPath) is invalid."
    63        return $False
    64    }
    65    # Get package info from json file created before updating version to daily dev
    66    $pkgInfo = Get-Content $pkgPropPath | ConvertFrom-Json
    67    $packagePath = $pkgInfo.DirectoryPath
    68    $modifiedFiles  = Get-ChangedFiles -DiffPath "$packagePath/*" -DiffFilterType ''
    69    $filteredFileCount = $modifiedFiles.Count
    70    Write-Host "Number of modified files for package: $filteredFileCount"
    71    return ($filteredFileCount -gt 0 -and $pkgInfo.IsNewSdk)
    72}
    73
    74function Log-Input-Params()
    75{
    76    Write-Host "Artifact Path: $($ArtifactPath)"
    77    Write-Host "Artifact Name: $($ArtifactName)"
    78    Write-Host "PullRequest Number: $($PullRequestNumber)"
    79    Write-Host "BuildId: $($BuildId)"
    80    Write-Host "Language: $($Language)"
    81    Write-Host "Commit SHA: $($CommitSha)"
    82    Write-Host "Repo Name: $($RepoFullName)"
    83    Write-Host "Package Name: $($PackageName)"
    84}
    85
    86Log-Input-Params
    87
    88if (!($FindArtifactForApiReviewFn -and (Test-Path "Function:$FindArtifactForApiReviewFn")))
    89{
    90    Write-Host "The function for 'FindArtifactForApiReviewFn' was not found.`
    91    Make sure it is present in eng/scripts/Language-Settings.ps1 and referenced in eng/common/scripts/common.ps1.`
    92    See https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/common_engsys.md#code-structure"
    93    exit 1
    94}
    95
    96$responses = @{}
    97foreach ($artifact in $ArtifactList)
    98{
    99    Write-Host "Processing $($artifact.name)"
   100    $packages = &$FindArtifactForApiReviewFn $ArtifactPath $artifact.name
   101    if ($packages)
   102    {
   103        $pkgPath = $packages.Values[0]
   104        $isRequired = Should-Process-Package -pkgPath $pkgPath -packageName $artifact.name
   105        Write-Host "Is API change detect required for $($artifact.name):$($isRequired)"
   106        if ($isRequired -eq $True)
   107        {
   108            $filePath = $pkgPath.Replace($ArtifactPath , "").Replace("\", "/")
   109            $respCode = Submit-Request -filePath $filePath -packageName $artifact.name
   110            if ($respCode -ne '200')
   111            {
   112                $responses[$artifact.name] = $respCode
   113            }
   114        }
   115        else
   116        {
   117            Write-Host "Pull request does not have any change for $($artifact.name). Skipping API change detect."
   118        }
   119    }
   120    else
   121    {
   122        Write-Host "No package is found in artifact path to find API changes for $($artifact.name)"
   123    }
   124}
   125
   126foreach($pkg in $responses.keys)
   127{
   128    Write-Host "API detection request status for $($pkg) : $($responses[$pkg])"
   129}

View as plain text