diff --git a/scripts/GetLatestCommitSHA.ps1 b/scripts/GetLatestCommitSHA.ps1 new file mode 100644 index 00000000000..6841b74e768 --- /dev/null +++ b/scripts/GetLatestCommitSHA.ps1 @@ -0,0 +1,62 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Gets the latest commit SHA for the repository. + +.Description + Uses the GitHub API and the owner name, the repository name, and the branch name + to get the latest commit SHAand set its value to an environment variable named + LATEST_COMMIT_SHA in an Azure DevOps release environment. + +.Parameter owner + Specifies the owner of the repo. + +.Parameter repo + Specifies the name of the repository. + +.Parameter branchName + Specifies the target branch name. The default value is 'master'. +#> + +Param( + [string]$owner, + [string]$repo, + [string]$branchName = "master" +) + +if ([string]::IsNullOrEmpty($owner)) { + Write-Error "owner cannot be empty." + EXIT 1 +} + +if ([string]::IsNullOrEmpty($repo)) { + Write-Error "repo cannot be empty." + EXIT 1 +} + +Write-Host "Getting the latest commit SHA for $($branchName):" -ForegroundColor Magenta + +$latestCommitUrl = "https://api.github.com/repos/$($owner)/$($repo)/commits/$($branchName)" + +Write-Host "Getting latest commit with '$($latestCommitUrl)'" -ForegroundColor Blue +$latestCommitData = Invoke-RestMethod -Uri $latestCommitUrl -Method Get + +if ($latestCommitData.Count -eq 0) { + Write-Host "Unable to get latest commit with '$($latestCommitUrl)'" -ForegroundColor Red + EXIT 1; +} + +if ([string]::IsNullOrEmpty($latestCommitData.sha)) { + Write-Host "SHA is not present in the latest commit that is fetched" -ForegroundColor Red + Write-Host "Latest Commit Data:" -ForegroundColor Cyan + Write-Host -Object $latestCommitData -ForegroundColor Cyan + EXIT 1; +} + +Write-Host "Latest Commit SHA is '$($latestCommitData.sha)'" -ForegroundColor Green + +Write-Host "##vso[task.setvariable variable=LATEST_COMMIT_SHA]$($latestCommitData.sha)" + +Write-Host "Updated the LATEST_COMMIT_SHA environment variable with the latest commit SHA." -ForegroundColor Green \ No newline at end of file diff --git a/scripts/GetNugetPackageVersion.ps1 b/scripts/GetNugetPackageVersion.ps1 new file mode 100644 index 00000000000..6a18bbee1b0 --- /dev/null +++ b/scripts/GetNugetPackageVersion.ps1 @@ -0,0 +1,40 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Get the NuGet package version. + +.Description + Get the NuGet package version and write the package version to an environment + variable named VERSION_STRING in the Azure DevOps release environment. + VERSION_STRING is used to name a tag for setting a GitHub release. This + script assumes that the NuGet package has been named with correct version number. + +.Parameter packageDirPath + Specifies the fully qualified path to the NuGet package directory. +#> + +Param( + [string]$packageDirPath +) + +Write-Host "Get the NuGet package version and set it in the global variable: VERSION_STRING" -ForegroundColor Magenta + +$nugetPackageName = (Get-ChildItem (Join-Path $packageDirPath *.nupkg) -Exclude *.symbols.nupkg).Name + +Write-Host "Found NuGet package: $nugetPackageName" -ForegroundColor Magenta +$packageVersionRegex = "Microsoft\.Graph\.Beta\.([0-9]+\.[0-9]+\.[0-9]+-\w+)\.nupkg" + +## Extracts the package version from nupkg file name. +if ($nugetPackageName -match $packageVersionRegex) +{ + $packageVersion = $Matches[1] + Write-Host "##vso[task.setvariable variable=VERSION_STRING]$($packageVersion)"; + Write-Host "Updated the VERSION_STRING environment variable with the package version value '$packageVersion'." -ForegroundColor Green +} +else +{ + Write-Host "Regular expression used in extracting the package version is: $packageVersionRegex" + Write-Error "We can't extract package version from the string" +} diff --git a/scripts/ValidateUpdatedNugetVersion.ps1 b/scripts/ValidateUpdatedNugetVersion.ps1 new file mode 100644 index 00000000000..a66ad5d16b2 --- /dev/null +++ b/scripts/ValidateUpdatedNugetVersion.ps1 @@ -0,0 +1,69 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Gets the latest production release version of the specified NuGet package. + +.Description + Gets the NuGet package version of latest production release and compares the + version to the version set in the specified project file. If they match, this + script will fail and indicate that the version needs to be updated. + +.Parameter packageName + Specifies the package name of the package. For example, 'microsoft.kiota.abstractions' + is a valid package name. + +.Parameter projectPath + Specifies the path to the project file. +#> + +Param( + [parameter(Mandatory = $true)] + [string]$packageName, + + [parameter(Mandatory = $true)] + [string]$projectPath +) + +[xml]$xmlDoc = Get-Content $projectPath + +# Assumption: VersionPrefix is set in the first property group. +$versionPrefixString = $xmlDoc.Project.PropertyGroup[0].VersionPrefix +if($xmlDoc.Project.PropertyGroup[0].VersionSuffix){ + $versionPrefixString = $versionPrefixString + "-" + $xmlDoc.Project.PropertyGroup[0].VersionSuffix +} + + +# System.Version, get the version prefix. +$currentProjectVersion = [System.Management.Automation.SemanticVersion]"$versionPrefixString" + +# API is case-sensitive +$packageName = $packageName.ToLower() +$url = "https://api.nuget.org/v3/registration5-gz-semver2/$packageName/index.json" + +# Call the NuGet API for the package and get the current published version. +Try { + $nugetIndex = Invoke-RestMethod -Uri $url -Method Get +} +Catch { + if ($_.ErrorDetails.Message && $_.ErrorDetails.Message.Contains("The specified blob does not exist.")) { + Write-Host "No package exists. You will probably be publishing $packageName for the first time." + Exit # exit gracefully + } + + Write-Host $_ + Exit 1 +} + +$currentPublishedVersion = [System.Management.Automation.SemanticVersion]$nugetIndex.items[$nugetIndex.items.Count-1].upper + +# Validate that the version number has been updated. +if ($currentProjectVersion -le $currentPublishedVersion) { + + Write-Error "The current published version number, $currentPublishedVersion, and the version number ` + in the csproj file, $currentProjectVersion, match. You must increment the version" +} +else { + Write-Host "Validated that the version has been updated from $currentPublishedVersion to $currentProjectVersion" -ForegroundColor Green +} \ No newline at end of file