From ccc62869227a5f19ef54d7ccebe7455e309073b3 Mon Sep 17 00:00:00 2001 From: "Jingjing Jia (from Dev Box)" Date: Tue, 30 Jun 2026 20:13:44 -0700 Subject: [PATCH] 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/` 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> --- .../language-generation-kiota.yml | 1 + scripts/create-pull-request.ps1 | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.azure-pipelines/generation-templates/language-generation-kiota.yml b/.azure-pipelines/generation-templates/language-generation-kiota.yml index 2bcccca70..09944949f 100644 --- a/.azure-pipelines/generation-templates/language-generation-kiota.yml +++ b/.azure-pipelines/generation-templates/language-generation-kiota.yml @@ -166,6 +166,7 @@ steps: displayName: 'Create Pull Request for the generated build for ${{ parameters.repoName }}' env: BaseBranch: ${{ parameters.baseBranchName}} + BranchName: ${{ parameters.branchName }} GeneratePullRequest: ${{ parameters.generatePullRequest}} GhAppId: $(microsoft-graph-devx-bot-appid) GhAppKey: $(microsoft-graph-devx-bot-privatekey) diff --git a/scripts/create-pull-request.ps1 b/scripts/create-pull-request.ps1 index d8932c341..0875c8080 100644 --- a/scripts/create-pull-request.ps1 +++ b/scripts/create-pull-request.ps1 @@ -30,6 +30,18 @@ if (![string]::IsNullOrEmpty($env:BaseBranch)) $baseBranchParameter = "-B $env:BaseBranch" # optionally pass the base branch if provided as the PR will target the default branch otherwise } +$headBranchParameter = "" + +if (![string]::IsNullOrEmpty($env:BranchName)) +{ + # Explicitly set the head branch. On the GitHub App auth path the repository is cloned with + # 'git clone --depth 1', which implies --single-branch and only tracks the base branch. As a + # result no 'refs/remotes/origin/' tracking ref exists after pushing, so 'gh pr create' + # cannot auto-detect that the branch was pushed and aborts with "you must first push the current + # branch to a remote, or use the --head flag". Passing --head bypasses that detection. + $headBranchParameter = "-H $env:BranchName" +} + # The installed application is required to have the following permissions: read/write on pull requests/ $tokenGenerationScript = "$env:ScriptsDirectory\Generate-Github-Token.ps1" $env:GITHUB_TOKEN = & $tokenGenerationScript -AppClientId $env:GhAppId -AppPrivateKeyContents $env:GhAppKey -Repository $env:RepoName @@ -37,6 +49,11 @@ Write-Host "Fetched Github Token for PR generation and set as environment variab # No need to specify reviewers as code owners should be added automatically. Invoke-Expression "gh auth login" # login to GitHub -Invoke-Expression "gh pr create -t ""$title"" -b ""$body"" $baseBranchParameter | Write-Host" +Invoke-Expression "gh pr create -R ""$env:RepoName"" -t ""$title"" -b ""$body"" $baseBranchParameter $headBranchParameter | Write-Host" + +if ($LASTEXITCODE -ne 0) +{ + throw "Failed to create pull request for $env:RepoName (branch '$env:BranchName'). 'gh pr create' exited with code $LASTEXITCODE." +} Write-Host "Pull Request Created successfully." \ No newline at end of file