Skip to content

Instantly share code, notes, and snippets.

@heaths
Last active April 5, 2022 22:40
Show Gist options
  • Save heaths/350485ab034f9da2bd10468dfcf2c794 to your computer and use it in GitHub Desktop.
Save heaths/350485ab034f9da2bd10468dfcf2c794 to your computer and use it in GitHub Desktop.
Copies OpenAPI specifications (swaggers) from one version to another for Azure/azure-rest-api-specs repo.
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string] $ServiceDirectory,
[Parameter(Mandatory=$true)]
[string] $ResourceProvider,
[Parameter(Mandatory=$true)]
[string] $SourceVersion,
[Parameter(Mandatory=$true)]
[string] $TargetVersion,
[Parameter()]
[string] $SourceBranch
)
$ErrorActionPreference = 'Stop'
# Make sure we are not in main; if so, fetch the latest main and create a new release branch.
if ($(git branch --show-current) -eq 'main') {
$targetBranch = "release-$ServiceDirectory-$ResourceProvider-$TargetVersion"
Write-Warning "Creating release branch '$targetBranch' from latest upstream 'main'"
Write-Verbose "Fetching branch 'main' from repo 'https://github.com/Azure/azure-rest-api-specs.git'"
git fetch https://github.com/Azure/azure-rest-api-specs.git main | out-null
git checkout -b $targetBranch FETCH_HEAD | out-null
}
# Fetch the source branch.
if (!$SourceBranch) {
$SourceBranch = "release-$ServiceDirectory-$ResourceProvider-$SourceVersion"
}
Write-Verbose "Fetching branch '$SourceVersion' from upstream"
git fetch https://github.com/Azure/azure-rest-api-specs.git $SourceBranch | out-null
# Make sure we're in the repo root.
Push-Location $(git rev-parse --show-toplevel)
trap { Pop-Location }
# Copy the files from the $SourceVersion to the $TargetVersion
$sourceQuality = if ($SourceVersion -match '-preview$') { 'preview' } else { 'stable' }
$targetQuality = if ($TargetVersion -match '-preview$') { 'preview' } else { 'stable' }
$sourceDirectory = "specification/$ServiceDirectory/data-plane/$ResourceProvider/$sourceQuality/$SourceVersion"
$targetDirectory = "specification/$ServiceDirectory/data-plane/$ResourceProvider/$targetQuality/$TargetVersion"
foreach ($sourcePath in $(git ls-tree -r --name-only FETCH_HEAD -- $sourceDirectory)) {
# Use String.Replace to avoid parsing $sourceDirectory as regex.
$targetPath = $sourcePath.Replace($sourceDirectory, $targetDirectory)
# Make sure the target directory exists.
New-Item -Path $(Split-Path $targetPath) -Type 'Directory' -ErrorAction 'Ignore' | out-null
Write-Verbose "Copying '$sourcePath' to '$targetPath' and updating api-version"
git show FETCH_HEAD:$sourcePath | ForEach-Object { $_ -replace "\`"(?<name>(api-?)?version)\`"\s*:\s*\`"$SourceVersion`"", "`"`${name}`": `"$TargetVersion`"" } | Set-Content $targetPath -Force
}
Write-Warning "Update 'specification/$ServiceDirectory/data-plane/$ResourceProvider/readme.md' manually to add tag '$TargetVersion' and set the default top-level 'tag: $TargetVersion' if appropriate."
Pop-Location
<#
.SYNOPSIS
Copies OpenAPI specifications (swaggers) from one version to another.
.DESCRIPTION
WARNING: This is only intended to run within a cloned repository of https://github.com/Azure/azure-rest-api-specs (referred to as the upstream remote) and for data-plane specifications.
This script creates a new OpenAPI specification version from an existing one whether or not its merged to main. It will create a release branch that you can commit and push to the upstream remote.
.PARAMETER ServiceDirectory
The directory name directly under the "specification" directory e.g., "cognitiveservices".
.PARAMETER ResourceProvider
The directory name directly under the "specification/$ServiceDirectory/data-plane" directory e.g., "Language".
.PARAMETER SourceVersion
The OpenAPI version from which to copy e.g., "2022-03-01-preview".
.PARAMETER TargetVersion
The OpenAPI version to create e.g., "2022-05-01" or "2022-05-01-preview".
.PARAMETER SourceBranch
Overrides the automatically generated source branch name "release-$ServiceDirectory-$ResourceProvider-$SourceVersion" e.g., "release-cognitiveservices-2022-03-01-preview".
.EXAMPLE
Copy-AzSpecifications.ps1 -ServiceDirectory cognitiveservices -ResourceProvider Language -SourceVersion 2022-03-01-preview -SourceBranch release-cognitiveservices-2022-03-01-preview -TargetVersion 2022-05-01
Creates a new 2022-05-01 (GA) release branch and specifications from a 2022-03-01-preview branch not yet merged to main, using the previously customized name release-cognitiveservices-2022-03-01-preview.
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment