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