Skip to content

Commit

Permalink
Merge 93ffa49 into dd395c3
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenkuttappan authored May 25, 2021
2 parents dd395c3 + 93ffa49 commit 794ff2f
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 0 deletions.
13 changes: 13 additions & 0 deletions eng/pipelines/templates/stages/archetype-js-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ stages:
echo "##vso[task.setvariable variable=Package.Archive]$DETECTED_PACKAGE_NAME"
displayName: Detecting package archive
- pwsh: |
$result = .\get-npm-tags.ps1 -packageArtifact $pkgArtifact -workingDirectory $(System.DefaultWorkingDirectory)
echo "##vso[task.setvariable variable=Tag]$result.Tag"
echo "##vso[task.setvariable variable=AdditionalTag]$result.AdditionalTag"
condition: and(succeeded(), ne(variables['Skip.AutoAddTag'], 'true'))
displayName: 'Set Tag and Additional Tag'
- task: PowerShell@2
displayName: 'Publish to npmjs.org'
inputs:
Expand All @@ -89,6 +96,12 @@ stages:
arguments: '-pathToArtifacts $(Package.Archive) -accessLevel "public" -tag $(Tag) -additionalTag "$(AdditionalTag)" -registry ${{parameters.Registry}} -npmToken $(azure-sdk-npm-token)'
pwsh: true

- pwsh: |
$result = .\get-npm-tags.ps1 -packageArtifact $pkgArtifact -workingDirectory $(System.DefaultWorkingDirectory) -npmToken $(azure-sdk-npm-token)
displayName: 'Remove Old Next Tag'
condition: and(succeeded(), ne(variables['Skip.RemoveOldTag'], 'true'))
- ${{if ne(artifact.skipPublishDocMs, 'true')}}:
- deployment: PublishDocs
displayName: Docs.MS Release
Expand Down
87 changes: 87 additions & 0 deletions eng/tools/get-npm-tags.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
param (
[Parameter(mandatory = $true)]
$packageArtifact,
[Parameter(mandatory = $true)]
$workingDirectory
)

. (Join-Path $PSScriptRoot npm-helpers.ps1)
$pkgProps = Get-PackageProperties -packageArtifact $packageArtifact -workingDirectory $workingDirectory
if ($pkgProps -eq $null)
{
Write-Error "Failed to parse package artifact $packageArtifact to get package name"
exit 1
}
$packageName = $pkgProps.PackageId
$packageVersion = $pkgProps.PackageVersion
$newVersion = [AzureEngSemanticVersion]::ParseVersionString($packageVersion)
Write-Host "Package name: $packageName"
Write-Host "Package version: $packageVersion"
Write-Host "Find latest and next versions in npm registry for package"

<#
1. Get latest GA and latest preview from npm
2. Check new version to be published to find is it GA or preview
3. If new Version is GA:
a. If higher than current latest, or first GA then set LATEST tag
4. If new version is preview and higher than current higher version in npm:
a. Set LATEST if package has never GA released
b. Set NEXT tag
#>
$npmVersionInfo = Get-LatestVersionInfoFromNpm -packageName $packageName
if ($npmVersionInfo -eq $null)
{
# Version info object should not be null even if package is not present in npm
Write-Error "Failed to get version info from NPM registry."
exit 1
}
$latestVersion = $npmVersionInfo.Latest
$nextVersion = $npmVersionInfo.Next

$setLatest = $false
$setNext = $false
# Set Latest tag if new version is higher than current GA or if package has never GA released before
if ((!$newVersion.IsPreRelease) -and ($latestVersion -eq $null -or $newVersion.CompareTo($latestVersion) -eq 1)) {
$setLatest = $true
}

if ($newVersion.PrereleaseLabel -eq "preview" -or $newVersion.PrereleaseLabel -eq "beta")
{
Write-Host "Checking for next version tag"
# Set next tag if new preview is higher than both GA and current preview
$highestNpmVersion = Find-RecentPackageVersion -packageName $packageName
# New version is preview and if package is getting released first time or higher than currently available
if ($highestNpmVersion -eq $null -or $newVersion.CompareTo($highestNpmVersion) -eq 1)
{
$setNext = $true
# Set latest tag if package was never GA released
if ($latestVersion -eq $null) {
$setLatest = $true
}
}
}

$tag = ""
$additionalTag = ""
if ($setLatest)
{
Write-Host "Setting Tag to latest"
$tag = "latest"
if ($setNext)
{
Write-Host "Setting AdditionalTag to next"
$additionalTag = "next"
}
}
elseif ($setNext)
{
Write-Host "Setting Tag to next"
$tag = "next"
}

$result = New-Object PSObject -Property @{
Tag = $tag
AdditionalTag = $additionalTag
}
write-Host $result
return $result
76 changes: 76 additions & 0 deletions eng/tools/npm-helpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
$EngPath = Resolve-Path "${PSScriptRoot}/.."
$Engcommon = Join-Path $EngPath "common"
$EngCommonScriptsPath = Join-Path $Engcommon "scripts"
. (Join-Path $EngCommonScriptsPath common.ps1)

function Get-PackageProperties($pkgArtifact, $workingDirectory)
{
if ($GetPackageInfoFromPackageFileFn -and (Test-Path "Function:$GetPackageInfoFromPackageFileFn"))
{
return &$GetPackageInfoFromPackageFileFn -pkg $packageArtifact -workingDirectory $workingDirectory
}
else
{
Write-Error "The function for '$GetPackageInfoFromPackageFileFn' was not found.`
Make sure it is present in eng/scripts/Language-Settings.ps1 and referenced in eng/common/scripts/common.ps1.`
See https://github.com/Azure/azure-sdk-tools/blob/master/doc/common/common_engsys.md#code-structure"
return $null
}
}

function Get-LatestVersionInfoFromNpm($packageName)
{
$latestVersion = npm show $packageName@latest version
$nextVersion = npm show $packageName@next version
Write-Host "Latest version: $latestVersion"
Write-Host "Next version: $nextVersion"
if ( $nextVersion -eq $null) {
Write-Host "'Next' tag is not present in npm registry for package $packageName"
}
else {
$nextVersion = [AzureEngSemanticVersion]::ParseVersionString($nextVersion)
}

if ( $latestVersion -eq $null) {
Write-Host "'latest' tag is not present in npm registry for package $packageName"
}
else {
$latestVersion = [AzureEngSemanticVersion]::ParseVersionString($latestVersion)
}

$result = New-Object PSObject -Property @{
Latest = $latestVersion
Next = $nextVersion
}
return $result
}

function Find-RecentPackageVersion($packageName)
{
$npmVersionInfo = Get-LatestVersionInfoFromNpm -packageName $packageName
if ($npmVersionInfo -ne $null)
{
$latestVersion = $npmVersionInfo.Latest
$nextVersion = $npmVersionInfo.Next
if (($nextVersion -ne $null) -and ($latestVersion -ne $null))
{
if ($latestVersion.CompareTo($nextVersion) -eq 1) {
$highestNpmVersion = $latestVersion
}
else {
$highestNpmVersion = $nextVersion
}
}
else
{
if ($nextVersion -ne $null) {
$highestNpmVersion = $nextVersion
}
else {
$highestNpmVersion = $latestVersion
}
}
}
Write-Host "Recent version uploaded to NPM: $highestNpmVersion"
return $highestNpmVersion
}
55 changes: 55 additions & 0 deletions eng/tools/remove-npm-next-tag.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
param (
[Parameter(mandatory = $true)]
$packageArtifact,
[Parameter(mandatory = $true)]
$workingDirectory,
[Parameter(mandatory = $true)]
$npmToken
)

. (Join-Path $PSScriptRoot npm-helpers.ps1)
$pkgProps = Get-PackageProperties -packageArtifact $packageArtifact -workingDirectory $workingDirectory
if ($pkgProps -eq $null)
{
Write-Error "Failed to parse package artifact $packageArtifact to get package name"
exit 1
}
$packageName = $pkgProps.PackageId
Write-Host "Package name: $packageName"
Write-Host "Find latest and next versions in npm registry for package"

<#
1. Get latest GA and latest preview from npm
2. Check if latest GA is higher than next
3. Remove next tag from preview version if latest is higher than preview version
#>

$npmVersionInfo = Get-LatestVersionInfoFromNpm -packageName $packageName
if ($npmVersionInfo -eq $null)
{
# Version info object should not be null even if package is not present in npm
Write-Error "Failed to get version info from NPM registry."
exit 1
}
$latestVersion = $npmVersionInfo.Latest
$nextVersion = $npmVersionInfo.Next

if ( ($latestVersion -ne $null) -and ($nextVersion -ne $null) -and (!$latestVersion.IsPreRelease))
{
if ($latestVersion.CompareTo($nextVersion) -eq 1)
{
Write-Host "Latest Version $latestVersion is higher than next tagged version $nextVersion."
Write-Host "Removing next tag from $nextVersion."
$scriptsPath = Join-Path $EngPath "scripts"
. (Join-Path $scriptsPath npm-admin-tasks.ps1) -taskType "RemoveTag" -packageName $packageName -pkgVersion $nextVersion.ToString() -tagName "next" -npmToken $npmToken
}
else
{
Write-Host "Latest tagged version is higher than or same as next tagged version."
Write-Host "Skipping remove 'next' tag task."
}
}
else
{
Write-Host "Latest or next tag is missing on npm or latest version is not GA release to compare versions."
}

0 comments on commit 794ff2f

Please sign in to comment.