|
| 1 | +#requires -Version 6.0 |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | + |
| 5 | +class CommitNode { |
| 6 | + [string] $Hash |
| 7 | + [string[]] $Parents |
| 8 | + [string] $AuthorName |
| 9 | + [string] $AuthorGitHubLogin |
| 10 | + [string] $AuthorEmail |
| 11 | + [string] $Subject |
| 12 | + [string] $Body |
| 13 | + [string] $PullRequest |
| 14 | + [string] $ChangeLogMessage |
| 15 | + [string] $ThankYouMessage |
| 16 | + |
| 17 | + CommitNode($hash, $parents, $name, $email, $subject, $body) { |
| 18 | + $this.Hash = $hash |
| 19 | + $this.Parents = $parents |
| 20 | + $this.AuthorName = $name |
| 21 | + $this.AuthorEmail = $email |
| 22 | + $this.Subject = $subject |
| 23 | + $this.Body = $body |
| 24 | + |
| 25 | + if ($subject -match "\(#(\d+)\)$") { |
| 26 | + $this.PullRequest = $Matches[1] |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +# These powershell team members don't use 'microsoft.com' for Github email or choose to not show their emails. |
| 32 | +# We have their names in this array so that we don't need to query GitHub to find out if they are powershell team members. |
| 33 | +$Script:powershell_team = @( |
| 34 | + "Robert Holt" |
| 35 | + "Travis Plunk" |
| 36 | + "Joey Aiello" |
| 37 | +) |
| 38 | + |
| 39 | +# They are very active contributors, so we keep their email-login mappings here to save a few queries to Github. |
| 40 | +$Script:community_login_map = @{ |
| 41 | + "[email protected]" = "springcomp" |
| 42 | +} |
| 43 | + |
| 44 | +# Ignore dependency bumping bot (Dependabot): |
| 45 | +$Script:attribution_ignore_list = @( |
| 46 | + 'dependabot[bot]@users.noreply.github.com' |
| 47 | +) |
| 48 | + |
| 49 | +############################## |
| 50 | +#.SYNOPSIS |
| 51 | +#In the release workflow, the release branch will be merged back to master after the release is done, |
| 52 | +#and a merge commit will be created as the child of the release tag commit. |
| 53 | +#This cmdlet takes a release tag or the corresponding commit hash, find its child merge commit, and |
| 54 | +#return its metadata in this format: <merge-commit-hash>|<parent-commit-hashes> |
| 55 | +# |
| 56 | +#.PARAMETER LastReleaseTag |
| 57 | +#The last release tag |
| 58 | +# |
| 59 | +#.PARAMETER CommitHash |
| 60 | +#The commit hash of the last release tag |
| 61 | +# |
| 62 | +#.OUTPUTS |
| 63 | +#Return the metadata of the child merge commit, in this format: <merge-commit-hash>|<parent-commit-hashes> |
| 64 | +############################## |
| 65 | +function Get-ChildMergeCommit |
| 66 | +{ |
| 67 | + [CmdletBinding(DefaultParameterSetName="TagName")] |
| 68 | + param( |
| 69 | + [Parameter(Mandatory, ParameterSetName="TagName")] |
| 70 | + [string]$LastReleaseTag, |
| 71 | + |
| 72 | + [Parameter(Mandatory, ParameterSetName="CommitHash")] |
| 73 | + [string]$CommitHash |
| 74 | + ) |
| 75 | + |
| 76 | + $tag_hash = $CommitHash |
| 77 | + if ($PSCmdlet.ParameterSetName -eq "TagName") { $tag_hash = git rev-parse "$LastReleaseTag^0" } |
| 78 | + |
| 79 | + ## Get the merge commits that are reachable from 'HEAD' but not from the release tag |
| 80 | + $merge_commits_not_in_release_branch = git --no-pager log --merges "$tag_hash..HEAD" --format='%H||%P' |
| 81 | + ## Find the child merge commit, whose parent-commit-hashes contains the release tag hash |
| 82 | + $child_merge_commit = $merge_commits_not_in_release_branch | Select-String -SimpleMatch $tag_hash |
| 83 | + return $child_merge_commit.Line |
| 84 | +} |
| 85 | + |
| 86 | +############################## |
| 87 | +#.SYNOPSIS |
| 88 | +#Create a CommitNode instance to represent a commit. |
| 89 | +# |
| 90 | +#.PARAMETER CommitMetadata |
| 91 | +#The commit metadata. It's in this format: |
| 92 | +#<commit-hash>|<parent-hashes>|<author-name>|<author-email>|<commit-subject> |
| 93 | +# |
| 94 | +#.PARAMETER CommitMetadata |
| 95 | +#The commit metadata, in this format: |
| 96 | +#<commit-hash>|<parent-hashes>|<author-name>|<author-email>|<commit-subject> |
| 97 | +# |
| 98 | +#.OUTPUTS |
| 99 | +#Return the 'CommitNode' object |
| 100 | +############################## |
| 101 | +function New-CommitNode |
| 102 | +{ |
| 103 | + param( |
| 104 | + [Parameter(ValueFromPipeline)] |
| 105 | + [ValidatePattern("^.+\|.+\|.+\|.+\|.+$")] |
| 106 | + [string]$CommitMetadata |
| 107 | + ) |
| 108 | + |
| 109 | + Process { |
| 110 | + $hash, $parents, $name, $email, $subject = $CommitMetadata.Split("||") |
| 111 | + $body = (git --no-pager show $hash -s --format=%b) -join "`n" |
| 112 | + return [CommitNode]::new($hash, $parents, $name, $email, $subject, $body) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +############################## |
| 117 | +#.SYNOPSIS |
| 118 | +#Generate the draft change log. |
| 119 | +# |
| 120 | +#.PARAMETER LastReleaseTag |
| 121 | +#The last release tag |
| 122 | +# |
| 123 | +#.PARAMETER HasCherryPick |
| 124 | +#Indicate whether there are any commits in the last release branch that were cherry-picked from the master branch |
| 125 | +# |
| 126 | +#.PARAMETER Token |
| 127 | +#The authentication token to use for retrieving the GitHub user log-in names for external contributors |
| 128 | +# |
| 129 | +#.OUTPUTS |
| 130 | +#The generated change log draft. |
| 131 | +############################## |
| 132 | +function Get-ChangeLog |
| 133 | +{ |
| 134 | + param( |
| 135 | + [Parameter(Mandatory = $true)] |
| 136 | + [string]$LastReleaseTag, |
| 137 | + |
| 138 | + [Parameter(Mandatory = $true)] |
| 139 | + [string]$ThisReleaseTag, |
| 140 | + |
| 141 | + [Parameter(Mandatory)] |
| 142 | + [string]$Token, |
| 143 | + |
| 144 | + [Parameter()] |
| 145 | + [switch]$HasCherryPick |
| 146 | + ) |
| 147 | + |
| 148 | + $tag_hash = git rev-parse "$LastReleaseTag^0" |
| 149 | + $format = '%H||%P||%aN||%aE||%s' |
| 150 | + $header = @{"Authorization"="token $Token"} |
| 151 | + |
| 152 | + # Find the merge commit that merged the release branch to master. |
| 153 | + $child_merge_commit = Get-ChildMergeCommit -CommitHash $tag_hash |
| 154 | + if($child_merge_commit) |
| 155 | + { |
| 156 | + $commit_hash, $parent_hashes = $child_merge_commit.Split("||") |
| 157 | + } |
| 158 | + # Find the other parent of the merge commit, which represents the original head of master right before merging. |
| 159 | + $other_parent_hash = ($parent_hashes -replace $tag_hash).Trim() |
| 160 | + |
| 161 | + if ($HasCherryPick) { |
| 162 | + ## Sometimes we need to cherry-pick some commits from the master branch to the release branch during the release, |
| 163 | + ## and eventually merge the release branch back to the master branch. This will result in different commit nodes |
| 164 | + ## in master branch that actually represent same set of changes. |
| 165 | + ## |
| 166 | + ## In this case, we cannot simply use the revision range "$tag_hash..HEAD" because it will include the original |
| 167 | + ## commits in the master branch that were cherry-picked to the release branch -- they are reachable from 'HEAD' |
| 168 | + ## but not reachable from the last release tag. Instead, we need to exclude the commits that were cherry-picked, |
| 169 | + ## and only include the commits that are not in the last release into the change log. |
| 170 | + |
| 171 | + # Find the commits that were only in the orginal master, excluding those that were cherry-picked to release branch. |
| 172 | + $new_commits_from_other_parent = git --no-pager log --first-parent --cherry-pick --right-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode |
| 173 | + # Find the commits that were only in the release branch, excluding those that were cherry-picked from master branch. |
| 174 | + $new_commits_from_last_release = git --no-pager log --first-parent --cherry-pick --left-only "$tag_hash...$other_parent_hash" --format=$format | New-CommitNode |
| 175 | + # Find the commits that are actually duplicate but having different patch-ids due to resolving conflicts during the cherry-pick. |
| 176 | + $duplicate_commits = $null |
| 177 | + if($new_commits_from_last_release -and $new_commits_from_other_parent) |
| 178 | + { |
| 179 | + $duplicate_commits = Compare-Object $new_commits_from_last_release $new_commits_from_other_parent -Property PullRequest -ExcludeDifferent -IncludeEqual -PassThru |
| 180 | + } |
| 181 | + if ($duplicate_commits) { |
| 182 | + $duplicate_pr_numbers = @($duplicate_commits | ForEach-Object -MemberName PullRequest) |
| 183 | + $new_commits_from_other_parent = $new_commits_from_other_parent | Where-Object PullRequest -NotIn $duplicate_pr_numbers |
| 184 | + } |
| 185 | + |
| 186 | + # Find the commits that were made after the merge commit. |
| 187 | + $new_commits_after_merge_commit = @(git --no-pager log --first-parent "$commit_hash..HEAD" --format=$format | New-CommitNode) |
| 188 | + $new_commits = $new_commits_after_merge_commit + $new_commits_from_other_parent |
| 189 | + } else { |
| 190 | + ## No cherry-pick was involved in the last release branch. |
| 191 | + ## Using a ref rang like "$tag_hash..HEAD" with 'git log' means getting the commits that are reachable from 'HEAD' but not reachable from the last release tag. |
| 192 | + |
| 193 | + ## We use '--first-parent' for 'git log'. It means for any merge node, only follow the parent node on the master branch side. |
| 194 | + ## In case we merge a branch to master for a PR, only the merge node will show up in this way, the individual commits from that branch will be ignored. |
| 195 | + ## This is what we want because the merge commit itself already represents the PR. |
| 196 | + |
| 197 | + ## First, we want to get all new commits merged during the last release |
| 198 | + $new_commits_during_last_release = @(git --no-pager log --first-parent "$tag_hash..$other_parent_hash" --format=$format | New-CommitNode) |
| 199 | + ## Then, we want to get all new commits merged after the last release |
| 200 | + $new_commits_after_last_release = @(git --no-pager log --first-parent "$commit_hash..HEAD" --format=$format | New-CommitNode) |
| 201 | + ## Last, we get the full list of new commits |
| 202 | + $new_commits = $new_commits_during_last_release + $new_commits_after_last_release |
| 203 | + } |
| 204 | + |
| 205 | + foreach ($commit in $new_commits) { |
| 206 | + Write-Verbose "authorname: $($commit.AuthorName)" |
| 207 | + if ($commit.AuthorEmail.EndsWith("@microsoft.com") -or $powershell_team -contains $commit.AuthorName -or $Script:attribution_ignore_list -contains $commit.AuthorEmail) { |
| 208 | + $commit.ChangeLogMessage = "- {0}" -f (Get-ChangeLogMessage $commit.Subject) |
| 209 | + } else { |
| 210 | + if ($community_login_map.ContainsKey($commit.AuthorEmail)) { |
| 211 | + $commit.AuthorGitHubLogin = $community_login_map[$commit.AuthorEmail] |
| 212 | + } else { |
| 213 | + $uri = "https://api.github.com/repos/PowerShell/PSReadLine/commits/$($commit.Hash)" |
| 214 | + try{ |
| 215 | + $response = Invoke-WebRequest -Uri $uri -Method Get -Headers $header -ErrorAction Ignore |
| 216 | + } catch{} |
| 217 | + if($response) |
| 218 | + { |
| 219 | + $content = ConvertFrom-Json -InputObject $response.Content |
| 220 | + $commit.AuthorGitHubLogin = $content.author.login |
| 221 | + $community_login_map[$commit.AuthorEmail] = $commit.AuthorGitHubLogin |
| 222 | + } |
| 223 | + } |
| 224 | + $commit.ChangeLogMessage = ("- {0} (Thanks @{1}!)" -f (Get-ChangeLogMessage $commit.Subject), $commit.AuthorGitHubLogin) |
| 225 | + $commit.ThankYouMessage = ("@{0}" -f ($commit.AuthorGitHubLogin)) |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + # Write output |
| 230 | + |
| 231 | + $version = $ThisReleaseTag.TrimStart('v') |
| 232 | + Write-Output "### [${version}] - $(Get-Date -Format yyyy-MM-dd)`n" |
| 233 | + |
| 234 | + foreach ($commit in $new_commits) { |
| 235 | + Write-Output $commit.ChangeLogMessage |
| 236 | + } |
| 237 | + |
| 238 | + Write-Output "`n[${version}]: https://github.com/PowerShell/PSReadLine/compare/${LastReleaseTag}...${ThisReleaseTag}`n" |
| 239 | +} |
| 240 | + |
| 241 | +function Get-ChangeLogMessage |
| 242 | +{ |
| 243 | + param($OriginalMessage) |
| 244 | + |
| 245 | + switch -regEx ($OriginalMessage) |
| 246 | + { |
| 247 | + '^Merged PR (\d*): ' { |
| 248 | + return $OriginalMessage.replace($Matches.0,'') + " (Internal $($Matches.1))" |
| 249 | + } |
| 250 | + '^Build\(deps\): ' { |
| 251 | + return $OriginalMessage.replace($Matches.0,'') |
| 252 | + } |
| 253 | + default { |
| 254 | + return $OriginalMessage |
| 255 | + } |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +Export-ModuleMember -Function Get-ChangeLog |
0 commit comments