From 74c8605e9a060dcd28d46cc12d77b07860c93a28 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Sun, 17 Dec 2017 17:18:24 -0800 Subject: [PATCH 1/6] Automate the generation of ChangeLog --- tools/release.psm1 | 166 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tools/release.psm1 diff --git a/tools/release.psm1 b/tools/release.psm1 new file mode 100644 index 00000000000..493c5a26346 --- /dev/null +++ b/tools/release.psm1 @@ -0,0 +1,166 @@ +class CommitNode { + [string] $Hash + [string[]] $Parents + [string] $AuthorName + [string] $AuthorGitHubLogin + [string] $AuthorEmail + [string] $Subject + [string] $Body + [string] $PullRequest + [string] $ChangeLogMessage + [bool] $IsBreakingChange + + CommitNode($hash, $parents, $name, $email, $subject, $body) { + $this.Hash = $hash + $this.Parents = $parents + $this.AuthorName = $name + $this.AuthorEmail = $email + $this.Subject = $subject + $this.Body = $body + $this.IsBreakingChange = $body -match "\[breaking change\]" + + if ($subject -match "\(#(\d+)\)") { + $this.PullRequest = $Matches[1] + } else { + throw "PR number is missing from the commit subject. (commit: $hash)" + } + } +} + +############################## +#.SYNOPSIS +#In the release workflow, the release branch will be merged back to master after the release is done, +#and a merge commit will be greated as the child of the release tag commit. +#This cmdlet takes a release tag or the corresponding commit hash, find its child merge commit, and +#return its metadata in this format: | +# +#.PARAMETER LastReleaseTag +#The last release tag +# +#.PARAMETER CommitHash +#The commit hash of the last release tag +# +#.OUTPUTS +#Return the metadata of the child merge commit, in this format: | +############################## +function Get-ChildMergeCommit +{ + [CmdletBinding(DefaultParameterSetName="TagName")] + param( + [Parameter(Mandatory, ParameterSetName="TagName")] + [string]$LastReleaseTag, + + [Parameter(Mandatory, ParameterSetName="CommitHash")] + [string]$CommitHash + ) + + $tag_hash = $CommitHash + if ($PSCmdlet.ParameterSetName -eq "TagName") { git rev-parse "$LastReleaseTag^0" } + + ## Get the merge commits that are reachable from 'HEAD' but not from the release tag + $merge_commits_not_in_release_branch = git --no-pager log --merges "$tag_hash..HEAD" --format='%H|%P' + ## Find the child merge commit, whose parent-commit-hashes contains the release tag hash + $child_merge_commit = $merge_commits_not_in_release_branch | Select-String -SimpleMatch $tag_hash + return $child_merge_commit.Line +} + +############################## +#.SYNOPSIS +#Create a CommitNode instance to represent a commit. +# +#.PARAMETER CommitMetadata +#The commit metadata. It's in this format: +#|||| +# +#.PARAMETER CommitMetadata +#The commit metadata, in this format: +#|||| +# +#.OUTPUTS +#Return the 'CommitNode' object +############################## +function New-CommitNode +{ + param( + [Parameter(ValueFromPipeline)] + [ValidatePattern("^.+\|.+\|.+\|.+\|.+$")] + [string]$CommitMetadata + ) + + Process { + $hash, $parents, $name, $email, $subject = $CommitMetadata.Split("|") + $body = (git --no-pager show $hash -s --format=%b) -join "`n" + return [CommitNode]::new($hash, $parents, $name, $email, $subject, $body) + } +} + +############################## +#.SYNOPSIS +#Generate the draft change log. +# +#.PARAMETER LastReleaseTag +#The last release tag +# +#.PARAMETER HasCherryPick +#Indicate whether there are any commits in the last release branch that were cherry-picked from the master branch +# +#.PARAMETER Token +#The authentication token to use for retrieving the GitHub user log-in names for external contributors +# +#.OUTPUTS +#The generated change log draft. +############################## +function Get-ChangeLog +{ + param( + [Parameter(Mandatory)] + [string]$LastReleaseTag, + + [Parameter(Mandatory)] + [string]$Token, + + [Parameter()] + [switch]$HasCherryPick + ) + + $tag_hash = git rev-parse "$LastReleaseTag^0" + $format = '%H|%P|%aN|%aE|%s' + $header = @{"Authorization"="token $Token"} + + if ($HasCherryPick) { + $child_merge_commit = Get-ChildMergeCommit -CommitHash $tag_hash + $commit_hash, $parent_hashes = $child_merge_commit.Split("|") + $other_parent_hash = ($parent_hashes -replace $tag_hash).Trim() + + $new_commits_from_other_parent = git --no-pager log --no-merges --cherry-pick --right-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode + $new_commits_from_last_release = git --no-pager log --no-merges --cherry-pick --left-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode + $duplicate_commits = Compare-Object $new_commits_from_last_release $new_commits_from_other_parent -Property PullRequest -ExcludeDifferent -IncludeEqual -PassThru + if ($duplicate_commits) { + $duplicate_pr_numbers = @($duplicate_commits | ForEach-Object -MemberName PullRequest) + $new_commits_from_other_parent = $new_commits_from_other_parent | Where-Object PullRequest -NotIn $duplicate_pr_numbers + } + + $new_commits_after_merge_commit = git --no-pager log --no-merges "$commit_hash..HEAD" --format=$format | New-CommitNode + $new_commits = $new_commits_after_merge_commit + $new_commits_from_other_parent + } else { + $new_commits = git --no-pager log --no-merges "$tag_hash..HEAD" --format=$format | New-CommitNode + } + + foreach ($commit in $new_commits) { + if ($commit.AuthorEmail.EndsWith("@microsoft.com")) { + $commit.ChangeLogMessage = $commit.Subject + } else { + $uri = "https://api.github.com/repos/PowerShell/PowerShell/commits/$($commit.Hash)" + $response = Invoke-WebRequest -Uri $uri -Method Get -Headers $header + $content = ConvertFrom-Json -InputObject $response.Content + $commit.AuthorGitHubLogin = $content.author.login + $commit.ChangeLogMessage = "{0} (Thanks @$($commit.AuthorGitHubLogin)!)" -f $commit.Subject + } + + if ($commit.IsBreakingChange) { + $commit.ChangeLogMessage = "[Breaking Change] {0}" -f $commit.ChangeLogMessage + } + } + + $new_commits | Sort-Object -Descending -Property IsBreakingChange | ForEach-Object -MemberName ChangeLogMessage +} From f442cea047ca16005aff36e210af8021b79bf65c Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Sun, 17 Dec 2017 17:19:44 -0800 Subject: [PATCH 2/6] Rename the module to 'releaseTools.psm1' --- tools/{release.psm1 => releaseTools.psm1} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tools/{release.psm1 => releaseTools.psm1} (100%) diff --git a/tools/release.psm1 b/tools/releaseTools.psm1 similarity index 100% rename from tools/release.psm1 rename to tools/releaseTools.psm1 From a39f77a05e9bc8d75a1ac112e0eacba9633cd4c2 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 18 Dec 2017 10:18:54 -0800 Subject: [PATCH 3/6] Add more comments --- tools/releaseTools.psm1 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tools/releaseTools.psm1 b/tools/releaseTools.psm1 index 493c5a26346..4b90229306b 100644 --- a/tools/releaseTools.psm1 +++ b/tools/releaseTools.psm1 @@ -128,21 +128,39 @@ function Get-ChangeLog $header = @{"Authorization"="token $Token"} if ($HasCherryPick) { + ## Sometimes we need to cherry-pick some commits from the master branch to the release branch during the release, + ## and eventually merge the release branch back to the master branch. This will result in different commit nodes + ## in master branch that actually represent same set of changes. + ## + ## In this case, we cannot simply use the revision range "$tag_hash..HEAD" becuase it will include the original + ## commits in the master branch that were cherry-picked to the release branch -- they are reachable from 'HEAD' + ## but not reachable from the last release tag. Instead, we need to exclude the commits that were cherry-picked, + ## and only include the commits that are not in the last release into the change log. + + # Find the merge commit that merged the release branch to master. $child_merge_commit = Get-ChildMergeCommit -CommitHash $tag_hash $commit_hash, $parent_hashes = $child_merge_commit.Split("|") + # Find the other parent of the merge commit, which represents the original head of master right before merging. $other_parent_hash = ($parent_hashes -replace $tag_hash).Trim() + # Find the commits that were only in the orginal master, excluding those that were cherry-picked to release branch. $new_commits_from_other_parent = git --no-pager log --no-merges --cherry-pick --right-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode + # Find the commits that were only in the release branch, excluding those that were cherry-picked from master branch. $new_commits_from_last_release = git --no-pager log --no-merges --cherry-pick --left-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode + # Find the commits that are actually duplicate but having different patch-ids due to resolving conflicts during the cherry-pick. $duplicate_commits = Compare-Object $new_commits_from_last_release $new_commits_from_other_parent -Property PullRequest -ExcludeDifferent -IncludeEqual -PassThru if ($duplicate_commits) { $duplicate_pr_numbers = @($duplicate_commits | ForEach-Object -MemberName PullRequest) $new_commits_from_other_parent = $new_commits_from_other_parent | Where-Object PullRequest -NotIn $duplicate_pr_numbers } + # Find the commits that were made after the merge commit. $new_commits_after_merge_commit = git --no-pager log --no-merges "$commit_hash..HEAD" --format=$format | New-CommitNode $new_commits = $new_commits_after_merge_commit + $new_commits_from_other_parent } else { + ## No cherry-pick was involved in the last release branch. + ## We can get all new commits using the revision range "$tag_hash..HEAD", meaning the commits that are + ## reachable from 'HEAD' but not reachable from the last release tag. $new_commits = git --no-pager log --no-merges "$tag_hash..HEAD" --format=$format | New-CommitNode } From 29aaa9bfed4636a11be83bd141a4fe22eddab130 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 18 Dec 2017 10:43:32 -0800 Subject: [PATCH 4/6] Add the prefix '-' to changelog entries --- tools/releaseTools.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/releaseTools.psm1 b/tools/releaseTools.psm1 index 4b90229306b..b7065387d89 100644 --- a/tools/releaseTools.psm1 +++ b/tools/releaseTools.psm1 @@ -166,17 +166,17 @@ function Get-ChangeLog foreach ($commit in $new_commits) { if ($commit.AuthorEmail.EndsWith("@microsoft.com")) { - $commit.ChangeLogMessage = $commit.Subject + $commit.ChangeLogMessage = "- {0}" -f $commit.Subject } else { $uri = "https://api.github.com/repos/PowerShell/PowerShell/commits/$($commit.Hash)" $response = Invoke-WebRequest -Uri $uri -Method Get -Headers $header $content = ConvertFrom-Json -InputObject $response.Content $commit.AuthorGitHubLogin = $content.author.login - $commit.ChangeLogMessage = "{0} (Thanks @$($commit.AuthorGitHubLogin)!)" -f $commit.Subject + $commit.ChangeLogMessage = "- {0} (Thanks @{1}!)" -f $commit.Subject, $commit.AuthorGitHubLogin } if ($commit.IsBreakingChange) { - $commit.ChangeLogMessage = "[Breaking Change] {0}" -f $commit.ChangeLogMessage + $commit.ChangeLogMessage = "{0} [Breaking Change]" -f $commit.ChangeLogMessage } } From 0fa993bcda6517688c4b0e779d0d222ffd8b11c7 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 18 Dec 2017 12:25:26 -0800 Subject: [PATCH 5/6] Address comments --- tools/releaseTools.psm1 | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/releaseTools.psm1 b/tools/releaseTools.psm1 index b7065387d89..5ab7c816ed0 100644 --- a/tools/releaseTools.psm1 +++ b/tools/releaseTools.psm1 @@ -30,7 +30,7 @@ class CommitNode { ############################## #.SYNOPSIS #In the release workflow, the release branch will be merged back to master after the release is done, -#and a merge commit will be greated as the child of the release tag commit. +#and a merge commit will be created as the child of the release tag commit. #This cmdlet takes a release tag or the corresponding commit hash, find its child merge commit, and #return its metadata in this format: | # @@ -164,14 +164,20 @@ function Get-ChangeLog $new_commits = git --no-pager log --no-merges "$tag_hash..HEAD" --format=$format | New-CommitNode } + $community_login_map = @{} foreach ($commit in $new_commits) { if ($commit.AuthorEmail.EndsWith("@microsoft.com")) { $commit.ChangeLogMessage = "- {0}" -f $commit.Subject } else { - $uri = "https://api.github.com/repos/PowerShell/PowerShell/commits/$($commit.Hash)" - $response = Invoke-WebRequest -Uri $uri -Method Get -Headers $header - $content = ConvertFrom-Json -InputObject $response.Content - $commit.AuthorGitHubLogin = $content.author.login + if ($community_login_map.ContainsKey($commit.AuthorEmail)) { + $commit.AuthorGitHubLogin = $community_login_map[$commit.AuthorEmail] + } else { + $uri = "https://api.github.com/repos/PowerShell/PowerShell/commits/$($commit.Hash)" + $response = Invoke-WebRequest -Uri $uri -Method Get -Headers $header + $content = ConvertFrom-Json -InputObject $response.Content + $commit.AuthorGitHubLogin = $content.author.login + $community_login_map[$commit.AuthorEmail] = $commit.AuthorGitHubLogin + } $commit.ChangeLogMessage = "- {0} (Thanks @{1}!)" -f $commit.Subject, $commit.AuthorGitHubLogin } From 3c09ff0171dd41b3cbadd971893eac87447eb61b Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 19 Dec 2017 10:42:18 -0800 Subject: [PATCH 6/6] Only expose the function 'Get-ChangeLog' --- tools/releaseTools.psm1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/releaseTools.psm1 b/tools/releaseTools.psm1 index 5ab7c816ed0..206faa95d3f 100644 --- a/tools/releaseTools.psm1 +++ b/tools/releaseTools.psm1 @@ -188,3 +188,5 @@ function Get-ChangeLog $new_commits | Sort-Object -Descending -Property IsBreakingChange | ForEach-Object -MemberName ChangeLogMessage } + +Export-ModuleMember -Function Get-ChangeLog