fix(azure): analyze repos with no default branch instead of panicking#95
Conversation
GitRepository.DefaultBranch (and Name/Size) are omitempty pointers, so the Azure DevOps API can return them as nil — e.g. for disabled/archived repos or repos whose HEAD ref was never initialized. getMostImportantBranch dereferenced *repo.DefaultBranch unconditionally, crashing the entire analysis with "panic: runtime error: invalid memory address or nil pointer dereference" partway through a large org scan. - Add defaultBranchName helper for nil-safe default-branch extraction. - When a repo has no default branch, fall back to the existing biggest-branch engine (handleNonDefaultBranch) so the repo is still analyzed, not skipped. - Harden handleNonDefaultBranch: skip nil branch names, and keep a first-branch fallback so a repo with no recent commits and no default branch still resolves to a real branch rather than an empty string. - Guard the getRepoAnalyse error fallback and the *repo.Size dereference. - Add TestDefaultBranchName. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add a fake git.Client test double (embeds the 121-method interface, overrides the 5 calls exercised) and unit tests covering the new code from the previous commit, which was failing the SonarQube new-code coverage condition (7.9% < 80%): - handleNonDefaultBranch: biggest-branch pick, first-branch fallback, default-branch fallback, nil-branch-name skip, no-analyzable-branch error. - getMostImportantBranch: default-branch mode with/without a default present, single-branch mode, GetRepository error propagation. - handleDefaultOrSingleBranch: nil-Size guard (and Size-present path). - getRepoAnalyse error fallback: skip repo with nil default branch, use the default when present. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ides) The doc comment predated adding GetRepositories/GetItems for the getRepoAnalyse test. Update it to reflect all five overridden methods. Addresses Gitar review feedback on PR #95. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Code Review ✅ Approved 1 resolved / 1 findingsAdds nil-safe handling for Azure DevOps repositories with missing default branches, preventing panics and ensuring analysis completion. The stale documentation in the fakeGitClient was also corrected. ✅ 1 resolved✅ Quality: fakeGitClient doc comment says "three calls" but overrides five
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |



Problem
A large Azure DevOps org scan crashes partway through branch identification with:
The analysis dies mid-run (e.g. at repo 432/522), even though the offending repository looks accessible and valid in the Azure DevOps UI.
Root cause
In the Azure DevOps Go API,
git.GitRepository.DefaultBranch(as well asNameandSize) is a*stringtaggedomitempty, soGetRepositorycan legitimately return it asnil— typically for a disabled/archived repository, or one whose HEAD ref was never initialized (yet still has branches, so it passes the empty-repo check).getMostImportantBranchdereferenced*repo.DefaultBranchunconditionally:Because the run was in default-branch mode, that one repo took down the entire scan. There were two further unguarded derefs of these
omitemptypointers (getRepoAnalyseerror fallback, and*repo.Size).Fix
Rather than skip a repo with no default branch, this routes it through the branch-scanning engine the tool already has (
handleNonDefaultBranch), which picks the most active branch.defaultBranchNamehelper for nil-safe default-branch extraction (stripsrefs/heads/).getMostImportantBranch: when a repo has no default branch, fall back to scanning every branch and picking the most active one — so the repo is still analyzed, not dropped or crashed on.handleNonDefaultBranch: skip nil branch names, and keep a first-branch fallback so a repo with no recent commits and no default branch still resolves to a real branch instead of an empty string.getRepoAnalyseerror fallback and the*repo.Sizedereference.TestDefaultBranchName.Behavior
For a disabled/archived-style repo with a nil
DefaultBranch:Testing
go build ./...✅go test ./pkg/devops/getazure/✅ (incl. newTestDefaultBranchName)