3333 Justification = ' Variable is used in script blocks.'
3434)]
3535[Diagnostics.CodeAnalysis.SuppressMessageAttribute (
36- ' PSUseDeclaredVarsMoreThanAssignments' , ' prNumber ' ,
36+ ' PSUseDeclaredVarsMoreThanAssignments' , ' prHeadRef ' ,
3737 Justification = ' Variable is used in script blocks.'
3838)]
3939[Diagnostics.CodeAnalysis.SuppressMessageAttribute (
40- ' PSUseDeclaredVarsMoreThanAssignments' , ' prHeadRef' ,
40+ ' PSUseDeclaredVarsMoreThanAssignments' , ' commitSha' ,
41+ Justification = ' Variable is used in script blocks.'
42+ )]
43+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute (
44+ ' PSUseDeclaredVarsMoreThanAssignments' , ' commitMessage' ,
4145 Justification = ' Variable is used in script blocks.'
4246)]
4347[Diagnostics.CodeAnalysis.SuppressMessageAttribute (
@@ -80,6 +84,7 @@ LogGroup 'Load inputs' {
8084 $usePRTitleAsReleaseName = $env: PSMODULE_RELEASE_PSMODULE_INPUT_UsePRTitleAsReleaseName -eq ' true'
8185 $usePRTitleAsNotesHeading = $env: PSMODULE_RELEASE_PSMODULE_INPUT_UsePRTitleAsNotesHeading -eq ' true'
8286 $releaseTag = $env: PSMODULE_RELEASE_PSMODULE_INPUT_ReleaseTag
87+ $commitSha = $env: PSMODULE_RELEASE_PSMODULE_INPUT_CommitSha
8388 if ([string ]::IsNullOrWhiteSpace($releaseTag )) {
8489 throw ' ReleaseTag is required. Ensure Publish.Module.Resolution.FullVersion is passed from the Plan job.'
8590 }
@@ -90,15 +95,29 @@ LogGroup 'Load inputs' {
9095 Write-Host " WhatIf: [$whatIf ]"
9196}
9297
93- LogGroup ' Load PR information ' {
98+ LogGroup ' Load release context ' {
9499 $githubEventJson = Get-Content - Raw $env: GITHUB_EVENT_PATH
95100 $githubEvent = $githubEventJson | ConvertFrom-Json
96- $pull_request = $githubEvent.pull_request
97- if (-not $pull_request ) {
98- throw ' GitHub event does not contain pull_request data. This script must be run from a pull_request event.'
101+ $pullRequestJson = $env: PSMODULE_RELEASE_PSMODULE_INPUT_PullRequest
102+ $pullRequest = if (-not [string ]::IsNullOrWhiteSpace($pullRequestJson ) -and $pullRequestJson -ne ' null' ) {
103+ $pullRequestJson | ConvertFrom-Json
104+ } elseif ($githubEvent.pull_request ) {
105+ [pscustomobject ]@ {
106+ Number = $githubEvent.pull_request.number
107+ Title = $githubEvent.pull_request.title
108+ Body = $githubEvent.pull_request.body
109+ HeadRef = $githubEvent.pull_request.head.ref
110+ }
111+ }
112+ $prNumber = $pullRequest.Number
113+ $prHeadRef = $pullRequest.HeadRef
114+ $commitMessage = $githubEvent.head_commit.message
115+
116+ if ($prNumber ) {
117+ Write-Host " Pull request: [#$prNumber ]"
118+ } else {
119+ Write-Host ' No pull request context is available; using the pushed commit message for release notes.'
99120 }
100- $prNumber = $pull_request.number
101- $prHeadRef = $pull_request.head.ref
102121}
103122
104123LogGroup ' Resolve version from manifest' {
@@ -153,6 +172,7 @@ LogGroup 'Resolve version from manifest' {
153172 ReleaseTag = $releaseTag
154173 PRNumber = $prNumber
155174 PRHeadRef = $prHeadRef
175+ CommitSha = $commitSha
156176 } | Format-List | Out-String
157177 )
158178}
@@ -178,31 +198,38 @@ LogGroup 'Create GitHub release' {
178198 }
179199
180200 if (-not $releaseExists ) {
181- if ($usePRTitleAsReleaseName -and $pull_request .title ) {
182- $releaseCreateCommand += @ (' --title' , $pull_request .title )
183- Write-Host " Using PR title as release name: [$ ( $pull_request .title ) ]"
201+ if ($usePRTitleAsReleaseName -and $pullRequest .Title ) {
202+ $releaseCreateCommand += @ (' --title' , $pullRequest .Title )
203+ Write-Host " Using PR title as release name: [$ ( $pullRequest .Title ) ]"
184204 } else {
185205 $releaseCreateCommand += @ (' --title' , $releaseTag )
186206 }
187207
188208 # Build release notes content. Uses a file to preserve special characters.
189- if ($usePRTitleAsNotesHeading -and $usePRBodyAsReleaseNotes -and $pull_request .title -and $pull_request .body ) {
190- $notes = " # $ ( $pull_request .title ) (#$prNumber )`n`n $ ( $pull_request .body ) "
209+ if ($usePRTitleAsNotesHeading -and $usePRBodyAsReleaseNotes -and $pullRequest .Title -and $pullRequest .Body ) {
210+ $notes = " # $ ( $pullRequest .Title ) (#$prNumber )`n`n $ ( $pullRequest .Body ) "
191211 $notesFilePath = [System.IO.Path ]::GetTempFileName()
192212 Set-Content - Path $notesFilePath - Value $notes - Encoding utf8
193213 $releaseCreateCommand += @ (' --notes-file' , $notesFilePath )
194214 Write-Host ' Using PR title as H1 heading with link and body as release notes'
195- } elseif ($usePRBodyAsReleaseNotes -and $pull_request .body ) {
215+ } elseif ($usePRBodyAsReleaseNotes -and $pullRequest .Body ) {
196216 $notesFilePath = [System.IO.Path ]::GetTempFileName()
197- Set-Content - Path $notesFilePath - Value $pull_request .body - Encoding utf8
217+ Set-Content - Path $notesFilePath - Value $pullRequest .Body - Encoding utf8
198218 $releaseCreateCommand += @ (' --notes-file' , $notesFilePath )
199219 Write-Host ' Using PR body as release notes'
220+ } elseif (-not [string ]::IsNullOrWhiteSpace($commitMessage )) {
221+ $notesFilePath = [System.IO.Path ]::GetTempFileName()
222+ Set-Content - Path $notesFilePath - Value $commitMessage - Encoding utf8
223+ $releaseCreateCommand += @ (' --notes-file' , $notesFilePath )
224+ Write-Host ' Using the pushed commit message as release notes'
200225 } else {
201226 $releaseCreateCommand += @ (' --generate-notes' )
202227 }
203228
204229 if ($createPrerelease ) {
205230 $releaseCreateCommand += @ (' --target' , $prHeadRef , ' --prerelease' )
231+ } elseif (-not [string ]::IsNullOrWhiteSpace($commitSha )) {
232+ $releaseCreateCommand += @ (' --target' , $commitSha )
206233 }
207234
208235 try {
@@ -247,9 +274,9 @@ LogGroup 'Create GitHub release' {
247274 }
248275 }
249276
250- if ($whatIf ) {
277+ if ($whatIf -and $prNumber ) {
251278 Write-Host " gh pr comment $prNumber -b '✅ $ ( $releaseType ) : GitHub - $name $releaseTag '"
252- } else {
279+ } elseif ( -not $whatIf -and $prNumber ) {
253280 gh pr comment $prNumber - b " ✅ $releaseType `: GitHub - [$name $releaseTag ]($releaseURL )"
254281 if ($LASTEXITCODE -ne 0 ) {
255282 throw ' Failed to comment on the pull request.'
0 commit comments