Skip to content
Merged
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
45 changes: 44 additions & 1 deletion eng/scripts/get-aspire-cli-pr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,50 @@ function Get-PRHeadSHA {

Write-Message "Getting HEAD SHA for PR #$PRNumber" -Level Verbose

$headSha = Invoke-GitHubAPICall -Endpoint "$Script:GHReposBase/pulls/$PRNumber" -JqFilter ".head.sha" -ErrorMessage "Failed to get HEAD SHA for PR #$PRNumber"
if ($Script:Repository -notmatch '^([^/]+)/([^/]+)$') {
throw "Invalid repository format '$Script:Repository'. Expected 'owner/name'."
}
$owner = $Matches[1]
$name = $Matches[2]

$graphqlQuery = 'query($owner:String!, $name:String!, $number:Int!) { repository(owner:$owner, name:$name) { pullRequest(number:$number) { headRefOid } } }'
$ghCommand = @(
"gh", "api", "graphql",
"-f", "query=$graphqlQuery",
"-f", "owner=$owner",
"-f", "name=$name",
"-F", "number=$PRNumber",
"--jq", ".data.repository.pullRequest.headRefOid"
)

Write-Message "Calling GitHub API: $($ghCommand -join ' ')" -Level Verbose

$graphQlError = $null
try {
$headSha = & $ghCommand[0] $ghCommand[1..($ghCommand.Length-1)] 2>$null
if ($LASTEXITCODE -ne 0) {
$graphQlError = "gh exited with code $LASTEXITCODE"
} elseif ([string]::IsNullOrWhiteSpace($headSha) -or $headSha -eq "null") {
$graphQlError = "GraphQL returned empty or null result"
} else {
# Normalize to a single trimmed string in case of unexpected multi-line output
$headSha = ($headSha | Select-Object -First 1).Trim()
}
}
catch {
$graphQlError = $_.Exception.Message
}

if ($graphQlError) {
Write-Message "GraphQL PR head lookup failed, falling back to REST API: $graphQlError" -Level Verbose
try {
$headSha = Invoke-GitHubAPICall -Endpoint "$Script:GHReposBase/pulls/$PRNumber" -JqFilter ".head.sha" -ErrorMessage "Failed to get HEAD SHA for PR #$PRNumber using REST fallback"
}
catch {
throw "Failed to get HEAD SHA for PR #$PRNumber with GraphQL query: $graphQlError`nREST fallback error: $($_.Exception.Message)"
}
}

if ([string]::IsNullOrWhiteSpace($headSha) -or $headSha -eq "null") {
Write-Message "This could mean:" -Level Info
Write-Message " - The PR number does not exist" -Level Info
Expand Down
31 changes: 26 additions & 5 deletions eng/scripts/get-aspire-cli-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,35 @@ get_pr_head_sha() {

say_verbose "Getting HEAD SHA for PR #$pr_number"

local head_sha
if ! head_sha=$(gh_api_call "${GH_REPOS_BASE}/pulls/$pr_number" ".head.sha" "Failed to get HEAD SHA for PR #$pr_number"); then
say_info "This could mean:"
say_info " - The PR number does not exist"
say_info " - You don't have access to the repository"
local repo_owner repo_name
if [[ "$REPO" =~ ^([^/]+)/([^/]+)$ ]]; then
repo_owner="${BASH_REMATCH[1]}"
repo_name="${BASH_REMATCH[2]}"
else
say_error "Invalid repository format '$REPO'. Expected 'owner/name'."
exit 1
fi

local graphql_query='query($owner:String!, $name:String!, $number:Int!) { repository(owner:$owner, name:$name) { pullRequest(number:$number) { headRefOid } } }'
local gh_cmd=(gh api graphql -f query="$graphql_query" -f owner="$repo_owner" -f name="$repo_name" -F number="$pr_number" --jq ".data.repository.pullRequest.headRefOid")

say_verbose "Calling GitHub API: ${gh_cmd[*]}"

local head_sha
if head_sha=$("${gh_cmd[@]}" 2>/dev/null) && [[ -n "$head_sha" && "$head_sha" != "null" ]]; then
# GraphQL succeeded with a valid SHA
:
else
say_verbose "GraphQL PR head lookup failed or returned empty, falling back to REST API"

if ! head_sha=$(gh_api_call "${GH_REPOS_BASE}/pulls/$pr_number" ".head.sha" "Failed to get HEAD SHA for PR #$pr_number using REST fallback"); then
say_info "This could mean:"
say_info " - The PR number does not exist"
say_info " - You don't have access to the repository"
exit 1
fi
fi
Comment thread
radical marked this conversation as resolved.

if [[ -z "$head_sha" || "$head_sha" == "null" ]]; then
say_error "Could not retrieve HEAD SHA for PR #$pr_number"
exit 1
Expand Down
Loading