fix: use shared git helpers for code-mappings repo inference by BYK · Pull Request #1087 · getsentry/cli · GitHub
Skip to content

fix: use shared git helpers for code-mappings repo inference#1087

Merged
BYK merged 1 commit into
mainfrom
fix/code-mappings-git-helpers
Jun 9, 2026
Merged

fix: use shared git helpers for code-mappings repo inference#1087
BYK merged 1 commit into
mainfrom
fix/code-mappings-git-helpers

Conversation

@BYK

@BYK BYK commented Jun 9, 2026

Copy link
Copy Markdown
Member

Summary

Follow-up fix for #1086 — replaces buggy custom git helpers in code-mappings upload with the well-tested shared helpers from src/lib/git.ts.

What was broken

The SSH_REMOTE_RE regex (:(.+?)(?:\.git)?$) in upload.ts incorrectly matched HTTPS URLs because they contain : (in https:). This produced garbage repo names:

Remote URL Expected Got
https://github.com/owner/repo.git owner/repo //github.com/owner/repo
ssh://git@github.com:22/owner/repo.git owner/repo //git@github.com:22/owner/repo

The corrupted name was sent to the Sentry API as the repository field.

What this fixes

  1. Buggy regex replaced — Uses parseRemoteUrl() from src/lib/git.ts which correctly tries new URL() first (handles https/ssh/git protocols) and only falls back to SCP-style regex when URL parsing fails.

  2. Shell injection risk removed — Replaced execSync (shell) with execFileSync (no shell) via the shared git() helper.

  3. Code consolidated — New inferRepositoryName() and inferDefaultBranch() functions in git.ts replace the duplicated logic in upload.ts. Both use this.cwd for correct working directory.

  4. ASCII art dividers removed — Per AGENTS.md prohibited comment styles, replaced // ── Section ─── dividers with plain // Section in code-mappings and dart-symbol-map files.

Files changed

File Change
src/lib/git.ts Added inferRepositoryName() and inferDefaultBranch()
src/commands/code-mappings/upload.ts Removed custom git helpers, imported from git.ts
src/lib/api/code-mappings.ts Removed ASCII art dividers
src/commands/dart-symbol-map/upload.ts Removed ASCII art dividers
src/lib/api/dart-symbols.ts Removed ASCII art dividers

Net: 66 insertions, 96 deletions (less code, more correct)

Replace buggy custom git helpers in code-mappings upload with the
well-tested parseRemoteUrl from src/lib/git.ts.

Fixes:
- SSH_REMOTE_RE incorrectly matched HTTPS URLs (e.g., the colon in
  https://github.com/owner/repo matched the SCP-style regex, producing
  garbage repo names like '//github.com/owner/repo')
- execSync with string interpolation replaced with execFileSync via
  the shared git() helper (no shell injection risk)
- inferRepo/inferDefaultBranch consolidated into git.ts as
  inferRepositoryName/inferDefaultBranch with upstream→origin fallback

Also removes ASCII art section dividers from code-mappings and
dart-symbol-map files per AGENTS.md prohibited comment styles.
@BYK BYK enabled auto-merge (squash) June 9, 2026 17:10
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-1087/

Built to branch gh-pages at 2026-06-09 17:11 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

Comment thread src/lib/git.ts
Comment on lines +272 to +274
return "main";
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inferDefaultBranch truncates branch names containing '/'

Splitting the ref by / and taking .at(-1) discards all but the last path segment, so a default branch like release/2.0 (output: refs/remotes/origin/release/2.0) would be returned as "2.0" instead of "release/2.0". Use output.slice(\refs/remotes/${remote}/`.length) || "main"` instead.

Evidence
  • git symbolic-ref refs/remotes/origin/HEAD emits refs/remotes/origin/release/2.0 for a default branch named release/2.0.
  • In inferDefaultBranch, output.split("/") produces ["refs", "remotes", "origin", "release", "2.0"] and .at(-1) returns only "2.0", dropping the release/ prefix.
  • The ?? "main" guard is ineffective: split("/") always returns a non-empty array, so .at(-1) is never undefined; an empty output yields "" (falsy, not nullish) and still bypasses the fallback.
  • The truncated branch flows through inferDefaultBranch(repoInfo?.remote ?? "origin", this.cwd) into uploadCodeMappings({ ... }) in src/commands/code-mappings/upload.ts.

Identified by Warden find-bugs · VXV-VGS

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

❌ Patch coverage is 0.00%. Project has 5019 uncovered lines.
✅ Project coverage is 81.17%. Comparing base (base) to head (head).

Files with missing lines (2)
File Patch % Lines
src/lib/git.ts 0.00% ⚠️ 12 Missing
src/commands/code-mappings/upload.ts 0.00% ⚠️ 1 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    81.13%    81.17%    +0.04%
==========================================
  Files          383       383         —
  Lines        26671     26659       -12
  Branches     17340     17336        -4
==========================================
+ Hits         21640     21640         —
- Misses        5031      5019       -12
- Partials      1797      1796        -1

Generated by Codecov Action

@BYK BYK merged commit 3af5252 into main Jun 9, 2026
26 checks passed
@BYK BYK deleted the fix/code-mappings-git-helpers branch June 9, 2026 17:19
BYK added a commit that referenced this pull request Jun 9, 2026
## Summary

Follow-up to #1087 — fixes a warden bot finding where
`inferDefaultBranch()` truncated branch names containing slashes.

## Bug

`inferDefaultBranch()` used `output.split('/').at(-1)` to extract the
branch name from a git symbolic-ref output like
`refs/remotes/origin/main`. This broke for branches with slashes:

| Ref output | Expected | Got |
|-----------|----------|-----|
| `refs/remotes/origin/main` | `main` | `main` |
| `refs/remotes/origin/release/2.0` | `release/2.0` | `2.0` |

## Fix

Use prefix-based slicing: strip the known `refs/remotes/{remote}/`
prefix, preserving the full branch name regardless of slashes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant