Skip to content

Commit

Permalink
Improve release notes generator (#1374)
Browse files Browse the repository at this point in the history
Parity with improvements done on Test Platform repo
  • Loading branch information
Evangelink authored Nov 10, 2022
1 parent 1b49f6d commit 10f4cd3
Showing 1 changed file with 52 additions and 62 deletions.
114 changes: 52 additions & 62 deletions scripts/write-release-notes.ps1
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
<#
.SYNOPSIS
Create release notes for current project using either last commit to last tag or between last 2 tags.
.EXAMPLE
Assuming you are on branch rel/17.4 and you want to create the release notes, run
.\write-release-notes -PackageVersion 17.4.0
it will create release notes between last commit of current branch and last release
If you want to generate the release notes between 2 tags, use simply
.\write-release-notes
#>

[CmdletBinding()]
param
(
[string] $Path = ".",
# if this is a pre-release or stable version
[switch] $Stable,
# externally provide the version number we will use
[string] $PackageVersion,
# in CI we don't know the end tag, so we diff till the current commit
[switch] $EndWithLatestCommit
[string] $Path = ".",
[ValidatePattern("^\d+\.\d+\.\d+(-preview-\d{8}-\d{2})?$")][string] $PackageVersion
)

if ($EndWithLatestCommit -and [string]::IsNullOrWhiteSpace($PackageVersion)) {
throw "EndWithLatestCommit was enabled, provide PackageVersion in this format 16.8.0-preview-20200924-01, or this format 16.8.0."
}

$repoUrl = $(if ((git -C $Path remote -v) -match "upstream") {
git -C $Path remote get-url --push upstream
}
else {
git -C $Path remote get-url --push origin
})-replace "\.git$"
}) -replace "\.git$"

# list all tags on this branch ordered by creator date to get the latest, stable or pre-release tag.
# For stable release we choose only tags without any dash, for pre-release we choose all tags.
$tags = git -C $Path tag -l --sort=creatordate | Where-Object { $_ -match "v\d+\.\d+\.\d+.*" -and (-not $Stable -or $_ -notlike '*-*') }
$tags = git -C $Path tag -l --sort=refname | Where-Object { $_ -match "v\d+\.\d+\.\d+.*" -and (-not $Stable -or $_ -notlike '*-*') }

if ($EndWithLatestCommit) {
$HasPackageVersion = ![string]::IsNullOrWhiteSpace($PackageVersion)

if ($HasPackageVersion) {
# in CI we don't have the tag yet, so we show changes between the most recent tag, and this commit
# we figure out the tag from the package version that is set by vsts-prebuild
$start = $tags | Select-Object -Last 1
Expand All @@ -38,18 +41,20 @@ if ($EndWithLatestCommit) {
else {
# normally we show changes between the latest two tags
$start, $end = $tags | Select-Object -Last 2
Write-Host "$start -- $end"
$tag = $end
}

# # override the tags to use if you need
# $start = "v16.8.0-preview-20200812-03"
# $end = $tag = "v16.8.0-preview-20200921-01"

Write-Host "Generating release notes for $start..$end$(if ($EndWithLatestCommit) { " (expected tag: $tag)" })"
Write-Host "Generating release notes for $start..$end$(if ($HasPackageVersion) { " (expected tag: $tag)" })"

$sourceBranch = $branch = $env:BUILD_SOURCEBRANCH -replace "^refs/heads/" # Get the source branch name from the CI if possible.
if ([string]::IsNullOrWhiteSpace($branch)) {
$sourceBranch = $branch = git -C $Path rev-parse --abbrev-ref HEAD
$sourceBranch = $branch = git -C $Path rev-parse --abbrev-ref HEAD
if ($sourceBranch -eq "HEAD") {
# when CI checks out just the single commit, https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
$sourceBranch = $env:BUILD_SOURCEBRANCH -replace "^refs/heads/"
}

if ([string]::IsNullOrWhiteSpace($branch)) {
Expand All @@ -62,71 +67,56 @@ if ([string]::IsNullOrWhiteSpace($sourceBranch)) {

Write-Host "Branch is $branch"
Write-Host "SourceBranch is $sourceBranch"
$branchesWithStartTag = if ($start) { git -C $Path branch --contains tags/$start }

if (-not $branchesWithStartTag -or -not ($branchesWithStartTag -match $branch)) {
Write-Host "This branch $branch$(if($branch -ne $sourceBranch){" ($sourceBranch)"}), does not contain the starting tag $start. Skipping generating release notes."
if ($branchesWithStartTag) {
Write-Host "The tag is present on branches:`n$($branchesWithStartTag)."
}
return
}
else {
Write-Host "Branch $branch$(if($branch -ne $sourceBranch){" ($sourceBranch)"}) has tag $start, getting log since that."
}

$discards = @(
"^Update dependencies from (?:https:\/\/github\.com[A-Za-z0-9\/\.-]+ )+build [0-9]{8}.[0-9]+ \(\#[0-9]+\)$",
"^\[.+\] Update dependencies from (?:[A-Za-z0-9\/\.-]+ )+\(\#[0-9]+\)$",
"^LEGO\: Pull request from lego\/[a-z0-9\-_]+ to .+ \(\#[0-9]+\)$",
"^Localized file check-in by OneLocBuild Task \(\#[0-9]+\)$"
)
$discard = $discards -join "|"
$discard = @(
"^Update dependencies from https:\/\/",
"^\[.+\] Update dependencies from",
"^LEGO: Pull request from lego",
"^Localized file check-in by OneLocBuild Task:",
"^Juno: check in to lego"
) -join "|"

$prUrl = "$repoUrl/pull/"
$v = $tag -replace '^v'
$b = if ($Stable) { $v } else { $tag -replace '.*?(\d+-\d+)$', '$1' }
$tagVersionNumber = $tag -replace '^v'
# using .. because I want to know the changes that are on this branch, but don't care about the changes that I don't have https://stackoverflow.com/a/24186641/3065397
$log = (git -C $Path log "$start..$end" --oneline --pretty="format:%s" --first-parent)
$date = ([datetime](git -C $path log -1 --format=%ai $end)).ToString("MMMM yyyy")
$issues = $log | ForEach-Object {
if($_ -match $discard) { }
elseif ($_ -match '^(?<message>.+)\s\(#(?<pr>\d+)\)?$') {
if ($matches.pr) {
$pr = $matches.pr
"- [x] [$($matches.message)]($prUrl$pr)"
if ($_ -notmatch $discard) {
if ($_ -match '^(?<message>.+)\s\(#(?<pr>\d+)\)?$') {
$message = "* $($matches.message)"
if ($matches.pr) {
$pr = $matches.pr
$message += " [#$pr]($prUrl$pr)"
}

$message
}
else {
"- [x] $($matches.message)"
"* $_"
}

$message
}
else
{
"- [x] $_"
}
}

$output = @"
See release notes [here](https://github.com/microsoft/testfx/blob/main/docs/releases.md#$(("$v $date" -replace "\.", "" -replace "\W", "-").ToLowerInvariant())).
See the release notes [here](https://github.com/microsoft/testfx/blob/main/docs/releases.md#$(("$tagVersionNumber $date" -replace "\.", "" -replace "\W", "-").ToLowerInvariant())).
---
-------------------------------
# $v ($date)
## $tagVersionNumber ($date)
$($issues -join "`n")
A list of changes since last release are available [here]($repoUrl/compare/$start...$tag)
See full log [here]($repoUrl/compare/$start...$tag)
### Builds
* MSTest: [$v](https://www.nuget.org/packages/MSTest/$v)
* MSTest.TestFramework: [$v](https://www.nuget.org/packages/MSTest.TestFramework/$v)
* MSTest.TestAdapter: [$v](https://www.nuget.org/packages/MSTest.TestAdapter/$v)
### Artifacts
"@
* MSTest: [$tagVersionNumber](https://www.nuget.org/packages/MSTest/$tagVersionNumber)
* MSTest.TestFramework: [$tagVersionNumber](https://www.nuget.org/packages/MSTest.TestFramework/$tagVersionNumber)
* MSTest.TestAdapter: [$tagVersionNumber](https://www.nuget.org/packages/MSTest.TestAdapter/$tagVersionNumber)
"@

$output
$output | clip

0 comments on commit 10f4cd3

Please sign in to comment.