[devops] Fix apidiff GitHub comment step failing on git ancestry check Fixes #26107#26144
Conversation
The 'Publish GitHub comment for change detection' pwsh step in the apidiff
pipeline could fail (marking the build partiallySucceeded) with:
Error checking if current commit is latest in PR: Failed to determine
whether '<commit>' is an ancestor of 'origin/<target_branch>'.
Root cause: the git helper functions Test-GitIsAncestor (git merge-base
--is-ancestor) and Get-GitCommitParents (git rev-list) leave a non-zero
$LASTEXITCODE behind. IsCurrentCommitLatestInPR catches the thrown
exception and returns true (so the comment is still computed), but the
leaked native exit code propagates to the end of the Azure DevOps pwsh
inline script (which ends with 'exit $LASTEXITCODE'), failing the step.
This happens on the Windows apidiff agents when origin/<target_branch>
isn't available in the shallow clone (merge-base exits with a non-0/1
code), and also in the ordinary 'not an ancestor' (exit 1) case.
Fix: capture the git exit code and reset $LASTEXITCODE to 0 in both
helpers so it never leaks out.
Also fix a pre-existing bug: 'git rev-list --parents -n 1 -- $Commit'
placed '--' before the commit, so git treated it as a pathspec and always
failed with usage exit 129, meaning merge-parent detection never worked.
Removed the erroneous '--'.
Added Pester tests covering both helpers, verifying the return value and
that $LASTEXITCODE stays 0 across the ancestor, non-ancestor and
unresolvable-ref cases.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes intermittent failures in the API diff pipeline’s “Publish GitHub comment for change detection” step by preventing native git exit codes from leaking out of PowerShell helper functions (which could otherwise cause the Azure DevOps pwsh task to exit non-zero).
Changes:
- Fix
Get-GitCommitParentsto callgit rev-listcorrectly (removes an erroneous--that made the command fail) and ensure$LASTEXITCODEis reset after invocation. - Fix
Test-GitIsAncestorto capture thegit merge-base --is-ancestorexit code and reset$LASTEXITCODEso it can’t propagate to the enclosing task. - Add Pester coverage for both helpers, including “success”, “failure”, and “unresolvable ref” scenarios, and assertions that
$LASTEXITCODEremains0.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tools/devops/automation/scripts/GitHub.psm1 | Fixes git helper implementations to avoid leaking native exit codes and corrects rev-list invocation. |
| tools/devops/automation/scripts/GitHub.Tests.ps1 | Adds Pester tests validating helper return values and verifying $LASTEXITCODE is not leaked. |
| $head = (& git rev-parse HEAD).Trim() | ||
| $parent = (& git rev-parse HEAD~1).Trim() | ||
| $global:LASTEXITCODE = 0 |
| $head = (& git rev-parse HEAD).Trim() | ||
| $parent = (& git rev-parse HEAD~1).Trim() | ||
| $global:LASTEXITCODE = 0 |
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
🚀 [CI Build #1fd6119] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 203 tests passed 🎉 Tests counts✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download macOS tests✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
The "Publish GitHub comment for change detection" step in the apidiff pipeline intermittently failed (marking builds
partiallySucceeded) with:Root cause
The git helpers
Test-GitIsAncestor(git merge-base --is-ancestor) andGet-GitCommitParents(git rev-list) inGitHub.psm1leave a non-zero$LASTEXITCODEbehind.IsCurrentCommitLatestInPRcatches the thrown exception and returnstrue(so the comment is still computed), but the leaked native git exit code propagates to the end of the Azure DevOpspwshinline script (which ends withexit $LASTEXITCODE), so the step is reported as failed.This happens on the Windows apidiff agents when
origin/<target_branch>isn't available in the shallow clone (merge-base exits with a non-0/1 code), and also in the ordinary "not an ancestor" (exit 1) case.Fix
$LASTEXITCODEto 0 in both helpers so it never leaks out and fails the enclosing task.git rev-list --parents -n 1 -- $Commitplaced--before the commit, so git treated it as a pathspec and always failed with usage exit 129 — meaning merge-parent detection never actually worked. Removed the erroneous--.$LASTEXITCODEstays 0 across the ancestor, non-ancestor and unresolvable-ref cases.Fixes #26107
🤖 Pull request created by Copilot