Skip to content

Commit

Permalink
Enhancement (generate): Add support for generating release notes from…
Browse files Browse the repository at this point in the history
… any valid specified ref to previous release tag
  • Loading branch information
joeltimothyoh committed May 19, 2024
1 parent 2f3d155 commit 8d6f649
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Describe "PSRepositoryReleaseManager" -Tag 'Integration' {
BeforeAll {
$ErrorView = 'NormalView'
$env:RELEASE_TAG_REF = git describe --tags --abbrev=0
$env:RELEASE_TAG_REF = 'HEAD'
$env:PROJECT_DIRECTORY = "$(git rev-parse --show-toplevel)"
}
BeforeEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ function Get-RepositoryCommitHistory {
)

try {
"Verifying refs" | Write-Verbose
Push-Location $Path
$FirstRef,$SecondRef | % {
git rev-parse $_ > $null
"Validating FirstRef '$FirstRef'" | Write-Verbose
git rev-parse $FirstRef > $null
if ($LASTEXITCODE) {
throw "An error occurred."
}
if ($SecondRef) {
"Validating SecondRef '$SecondRef'" | Write-Verbose
git rev-parse $SecondRef > $null
if ($LASTEXITCODE) {
throw "An error occurred."
}
}
"First ref: '$($FirstRef)':" | Write-Verbose
if ($SecondRef) {
"Second ref: '$SecondRef':" | Write-Verbose
$commitSHARange = "$($FirstRef)...$($SecondRef)"
}else {
"Second ref unspecifed. Full history of First ref will be retrieved." | Write-Verbose
"SecondRef unspecifed. The full commit history from FirstRef '$FirstRef' will be retrieved." | Write-Verbose
$commitSHARange = $FirstRef
}
$gitArgs = @(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,39 @@ function Get-RepositoryReleasePrevious {

try {
Push-Location $Path
"Retrieving info on release tags" | Write-Verbose
$releaseTagsInfo = (git --no-pager log --date-order --tags --simplify-by-decoration --pretty="format:%H %D") -split "`n" | % {
if ($_ -match '\s+tag:\s+(v\d+\.\d+\.\d+)(,|$)') {
$_
}
# Validate specified ref is an existing ref
"Validating ref '$Ref'" | Write-Verbose
git rev-parse "$Ref" | Write-Verbose
if ($LASTEXITCODE) {
throw "An error occurred."
}
if (!$releaseTagsInfo) {
throw "No release tags exist in the repository '$($Path)'."
"Verifying if ref '$Ref' is a tag" | Write-Verbose
$isRefTag = $null
git show-ref --verify "refs/tags/$Ref" | Write-Verbose
if (!$LASTEXITCODE) {
$isRefTag = $true
}
"Release tags info:" | Write-Verbose
$releaseTagsInfo | Write-Verbose

if ($Ref) {
$refSHA = git rev-parse $Ref
if ($LASTEXITCODE) {
throw "An error occurred."
# Retrieve previous release tags of the specified ref within the same git tree
"Retrieving info on previous release tags" | Write-Verbose
$tagsPreviousInfo = (git --no-pager log "$Ref" --date-order --simplify-by-decoration --pretty="format:%H %D") -split "`n" | % {
if ($_ -match '\s+tag:\s+(v\d+\.\d+\.\d+)(,|$)') {
$_
}
"Found SHA: $refSHA" | Write-Verbose
}
if (@($releaseTagsInfo).Count -eq 1) {
throw "Only one release tag exists in the repository '$($Path)'."
}

$releasePreviousCommitSHA = if ($Ref) {
"Searching for the previous release relative from the ref '$($Ref)'" | Write-Verbose
$cnt = 0;
foreach ($r in $releaseTagsInfo) {
if ($r -match "^$refSHA\s+") {
break
}
$cnt++
}
if ($releaseTagsInfo.Count -eq $cnt) {
throw "The specified ref '$($Ref)' is not a valid release."
}
if (@($releaseTagsInfo).Count -eq ($cnt+1)) {
throw "No previous release exists relative from the ref '$($Ref)'"
}
($releaseTagsInfo[$cnt+1] -split "\s")[0]
}else {
($releaseTagsInfo[1] -split "\s")[0]
"Previous release tags info:" | Write-Verbose
$tagsPreviousInfo | Write-Verbose
if ($isRefTag -And @($tagsPreviousInfo).Count -eq 1) {
"No previous tags for ref '$ref' exists in the repository '$($Path)'." | Write-Verbose
return
}
"Previous release commit SHA: $releasePreviousCommitSHA" | Write-Verbose
$tagPreviousCommitSHA = if ($isRefTag) { # If specified ref is a tag, the previous tag is on the following line
(@($tagsPreviousInfo)[1] -split "\s")[0]
}else {
(@($tagsPreviousInfo)[0] -split "\s")[0]
}
"Previous release commit SHA: $tagPreviousCommitSHA" | Write-Verbose
"Retrieving previous release tag(s)" | Write-Verbose
git tag --points-at $releasePreviousCommitSHA | Sort-Object -Descending # Returns an array of tags if they point to the same commit
git tag --points-at $tagPreviousCommitSHA | Sort-Object -Descending # Returns an array of tags if they point to the same commit
}catch {
Write-Error -Exception $_.Exception -Message $_.Exception.Message -Category $_.CategoryInfo.Category -TargetObject $_.TargetObject
}finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function Changes-HashSubject-Merges {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%h %s'
Merges = $true
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function Changes-HashSubject-NoMerges {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%h %s'
NoMerges = $true
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function Changes-HashSubject {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%h %s'
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function VersionDate-HashSubject-Merges {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%h %s'
Merges = $true
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function VersionDate-HashSubject-NoMerges {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%h %s'
NoMerges = $true
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function VersionDate-HashSubject {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%h %s'
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function VersionDate-Subject-Merges {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%s'
Merges = $true
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function VersionDate-Subject-NoMerges {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%s'
NoMerges = $true
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function VersionDate-Subject {
}
$funcArgs = @{
Path = $Path
FirstRef = if ($previousRelease) { @($previousRelease)[0] } else { $TagName }
FirstRef = $TagName
PrettyFormat = '%s'
}
if ($previousRelease) { $funcArgs['SecondRef'] = $TagName }
if ($previousRelease) { $funcArgs['SecondRef'] = @($previousRelease)[0] }
$commitHistory = Get-RepositoryCommitHistory @funcArgs
$releaseBody = & {
@"
Expand Down

0 comments on commit 8d6f649

Please sign in to comment.