Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion scripts/create-pull-request.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,30 @@ 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/<branch>' 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
Write-Host "Fetched Github Token for PR generation and set as environment variable."

# 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."
Loading