Skip to content

Commit f86b7a3

Browse files
Add GitHub CLI integration to resolve AppVeyor builds
Enhances Get-AppVeyorFailure.ps1 to first attempt resolving the latest AppVeyor build for a branch using the GitHub CLI and GitHub API, falling back to the previous AppVeyor history API logic if necessary. This improves reliability and accuracy when identifying the correct build associated with a branch.
1 parent 2936ed5 commit f86b7a3

File tree

1 file changed

+73
-22
lines changed

1 file changed

+73
-22
lines changed

.aitools/module/Get-AppVeyorFailure.ps1

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,85 @@ function Get-AppVeyorFailure {
5959
Write-Progress -Activity "Get-AppVeyorFailure" -Status "Fetching latest build for branch '$Branch'..." -PercentComplete 0
6060
Write-Verbose "Looking for latest AppVeyor build for branch: $Branch"
6161

62+
# Try GitHub CLI integration first
63+
$usedGh = $false
6264
try {
63-
# Get recent builds and find the latest one for this branch
64-
$apiParams = @{
65-
Endpoint = "projects/dataplat/dbatools/history?recordsNumber=50"
66-
}
67-
$history = Invoke-AppVeyorApi @apiParams
68-
69-
if (-not $history -or -not $history.builds) {
70-
Write-Verbose "No build history found"
71-
Write-Progress -Activity "Get-AppVeyorFailure" -Completed
72-
return
65+
$Owner = "dataplat"
66+
$Repo = "dbatools"
67+
68+
# Get the latest commit SHA for the branch
69+
$sha = (& gh api "repos/$Owner/$Repo/commits?sha=$Branch&per_page=1" -q '.[0].sha' 2>$null)
70+
if ($sha) {
71+
$sha = $sha.Trim()
72+
73+
# Try checks API first
74+
$detailsUrl = (& gh api "repos/$Owner/$Repo/commits/$sha/check-runs?per_page=100" -q '.check_runs | sort_by(.started_at, .created_at) | reverse[] | select((.name|test("appveyor";"i")) or (.app.slug=="appveyor")) | .details_url' 2>$null)
75+
if ($detailsUrl) {
76+
$detailsUrl = ($detailsUrl -split "`r?`n" | Select-Object -First 1).Trim()
77+
}
78+
79+
# Fallback to statuses API if no check-runs found
80+
if (-not $detailsUrl) {
81+
$detailsUrl = (& gh api "repos/$Owner/$Repo/commits/$sha/status" -q '.statuses | sort_by(.updated_at, .created_at) | reverse[] | select(.context|test("appveyor";"i")) | .target_url' 2>$null)
82+
if ($detailsUrl) {
83+
$detailsUrl = ($detailsUrl -split "`r?`n" | Select-Object -First 1).Trim()
84+
}
85+
}
86+
87+
# Extract AppVeyor build version from URL
88+
if ($detailsUrl -and $detailsUrl -match '/build/([^/?#]+)') {
89+
$version = $Matches[1]
90+
Write-Verbose "Using GitHub checks to resolve AppVeyor build for branch '$Branch' (version: $version)"
91+
92+
# Call AppVeyor API directly with the version
93+
$apiParams = @{
94+
Endpoint = "projects/dataplat/dbatools/build/$version"
95+
}
96+
$build = Invoke-AppVeyorApi @apiParams
97+
98+
if ($build -and $build.build) {
99+
$BuildNumber = $build.build.buildNumber
100+
$usedGh = $true
101+
Write-Verbose "Successfully resolved build #$BuildNumber using GitHub CLI"
102+
}
103+
}
73104
}
105+
} catch {
106+
# Silently fall back to existing logic
107+
Write-Verbose "GitHub CLI approach failed, falling back to AppVeyor history API"
108+
}
74109

75-
# Find the latest build for this branch
76-
$branchBuild = $history.builds | Where-Object { $_.branch -eq $Branch } | Select-Object -First 1
77-
78-
if (-not $branchBuild) {
79-
Write-Verbose "No builds found for branch: $Branch"
110+
# Fallback to existing -Branch logic if GitHub CLI didn't work
111+
if (-not $usedGh) {
112+
try {
113+
# Get recent builds and find the latest one for this branch
114+
$apiParams = @{
115+
Endpoint = "projects/dataplat/dbatools/history?recordsNumber=50"
116+
}
117+
$history = Invoke-AppVeyorApi @apiParams
118+
119+
if (-not $history -or -not $history.builds) {
120+
Write-Verbose "No build history found"
121+
Write-Progress -Activity "Get-AppVeyorFailure" -Completed
122+
return
123+
}
124+
125+
# Find the latest build for this branch
126+
$branchBuild = $history.builds | Where-Object { $_.branch -eq $Branch } | Select-Object -First 1
127+
128+
if (-not $branchBuild) {
129+
Write-Verbose "No builds found for branch: $Branch"
130+
Write-Progress -Activity "Get-AppVeyorFailure" -Completed
131+
return
132+
}
133+
134+
$BuildNumber = $branchBuild.buildNumber
135+
Write-Verbose "Found latest build #$BuildNumber for branch '$Branch'"
136+
} catch {
137+
Write-Verbose "Failed to fetch build history for branch ${Branch}: $_"
80138
Write-Progress -Activity "Get-AppVeyorFailure" -Completed
81139
return
82140
}
83-
84-
$BuildNumber = $branchBuild.buildNumber
85-
Write-Verbose "Found latest build #$BuildNumber for branch '$Branch'"
86-
} catch {
87-
Write-Verbose "Failed to fetch build history for branch ${Branch}: $_"
88-
Write-Progress -Activity "Get-AppVeyorFailure" -Completed
89-
return
90141
}
91142
}
92143

0 commit comments

Comments
 (0)