@@ -30,13 +30,30 @@ if (![string]::IsNullOrEmpty($env:BaseBranch))
3030 $baseBranchParameter = " -B $env: BaseBranch " # optionally pass the base branch if provided as the PR will target the default branch otherwise
3131}
3232
33+ $headBranchParameter = " "
34+
35+ if (! [string ]::IsNullOrEmpty($env: BranchName ))
36+ {
37+ # Explicitly set the head branch. On the GitHub App auth path the repository is cloned with
38+ # 'git clone --depth 1', which implies --single-branch and only tracks the base branch. As a
39+ # result no 'refs/remotes/origin/<branch>' tracking ref exists after pushing, so 'gh pr create'
40+ # cannot auto-detect that the branch was pushed and aborts with "you must first push the current
41+ # branch to a remote, or use the --head flag". Passing --head bypasses that detection.
42+ $headBranchParameter = " -H $env: BranchName "
43+ }
44+
3345# The installed application is required to have the following permissions: read/write on pull requests/
3446$tokenGenerationScript = " $env: ScriptsDirectory \Generate-Github-Token.ps1"
3547$env: GITHUB_TOKEN = & $tokenGenerationScript - AppClientId $env: GhAppId - AppPrivateKeyContents $env: GhAppKey - Repository $env: RepoName
3648Write-Host " Fetched Github Token for PR generation and set as environment variable."
3749
3850# No need to specify reviewers as code owners should be added automatically.
3951Invoke-Expression " gh auth login" # login to GitHub
40- Invoke-Expression " gh pr create -t "" $title "" -b "" $body "" $baseBranchParameter | Write-Host"
52+ Invoke-Expression " gh pr create -R "" $env: RepoName "" -t "" $title "" -b "" $body "" $baseBranchParameter $headBranchParameter | Write-Host"
53+
54+ if ($LASTEXITCODE -ne 0 )
55+ {
56+ throw " Failed to create pull request for $env: RepoName (branch '$env: BranchName '). 'gh pr create' exited with code $LASTEXITCODE ."
57+ }
4158
4259Write-Host " Pull Request Created successfully."
0 commit comments