Skip to content

Commit ccc6286

Browse files
Fix PR creation on GitHub App auth path (pass --head to gh pr create)
The GitHub App auth generation path clones repos with `git clone --depth 1`, which implies --single-branch and only tracks the base branch. After pushing the generated feature branch, no `refs/remotes/origin/<branch>` tracking ref exists locally, so `gh pr create` (which auto-detects the pushed head via that ref) aborts with "you must first push the current branch to a remote, or use the --head flag" even though the push succeeded. - create-pull-request.ps1: pass `-H $env:BranchName` and `-R $env:RepoName` to `gh pr create`, and fail the step on a non-zero exit code instead of always printing "Pull Request Created successfully." - language-generation-kiota.yml: thread `BranchName` into the create-pull-request.ps1 step env so the head branch is available. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f11b751 commit ccc6286

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

.azure-pipelines/generation-templates/language-generation-kiota.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ steps:
166166
displayName: 'Create Pull Request for the generated build for ${{ parameters.repoName }}'
167167
env:
168168
BaseBranch: ${{ parameters.baseBranchName}}
169+
BranchName: ${{ parameters.branchName }}
169170
GeneratePullRequest: ${{ parameters.generatePullRequest}}
170171
GhAppId: $(microsoft-graph-devx-bot-appid)
171172
GhAppKey: $(microsoft-graph-devx-bot-privatekey)

scripts/create-pull-request.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
3648
Write-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.
3951
Invoke-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

4259
Write-Host "Pull Request Created successfully."

0 commit comments

Comments
 (0)