Skip to content

fix(github-actions-grafana-jump): resolve a file view's ref when the branch name has a slash - #53

Merged
nsheaps-oura merged 1 commit into
mainfrom
n8bot/branch-ref-disambiguation
Aug 10, 2026
Merged

nsheaps-oura merged 1 commit into
mainfrom
n8bot/branch-ref-disambiguation

Conversation

@nsheaps-oura

Copy link
Copy Markdown
Collaborator

Problem

/org/repo/blob/<ref>/<path> gives no hint about where the ref ends and the file path begins, so parseRepoFileContext() took the first /-separated segment after /blob/ as the whole ref. On a branch whose name contains a slash that's wrong:

/blob/nate-ai/generic-grafana-jump/README.md reads equally well as

ref path
nate-ai generic-grafana-jump/README.md
nate-ai/generic-grafana-jump README.md

Only the repo's real ref list settles it, and GitHub's own UI settles it that way. Taking the first segment means the branch-aware config fetch added in #51 went looking at a truncated, usually-nonexistent ref — so it 404'd and silently dropped the repo's links, or worse matched an unrelated real branch that happened to share the truncated prefix.

What changed

GitHub has already done that resolution to render the page, and the file view's own DOM states the answer twice:

<button id="ref-picker-repos-header-ref-selector"
        aria-label="nate-ai/generic-grafana-jump branch" ...>
<script type="application/json" data-target="react-app.embeddedData">
  ... "refInfo":{"name":"nate-ai/generic-grafana-jump", ...} ...

So the ref is read from the page and layered on top of the pure URL parse, exactly the way #51 already layers a run page's DOM-only ref via repoConfigTarget(). parseRepoFileContext() itself is unchanged: still pure, synchronous, and still the reading that stands when the DOM has no answer.

flowchart LR
  A["/blob/&lt;ref&gt;/&lt;path&gt;"] --> B["parseRepoFileContext():<br/>first segment"]
  B --> C{"ref picker's<br/>aria-label?"}
  C -- "no" --> D{"embedded<br/>refInfo.name?"}
  C -- "yes" --> E{"is it a valid reading<br/>of this URL?"}
  D -- "yes" --> E
  D -- "no" --> F["no override:<br/>first segment stands"]
  E -- "yes" --> G["use the full ref"]
  E -- "no: stale, or short SHA" --> F
Loading

Why not GET /repos/{org}/{repo}/branches

That was the obvious approach and it's the worse one here:

  • Private repos. api.github.com is a different host than github.com, so the browser's github.com session cookie isn't sent with it, and an anonymous branch listing for a private repo 404s. That's the same trap already documented on resolveDefaultBranch() in this file (confirmed there against a real private repo) — the fix would silently do nothing exactly where a wrong ref hurts most.
  • Requests. A ref listing needs a new @connect api.github.com grant, is paginated for repos over 100 branches, and shares a 60/hr unauthenticated per-IP limit. Reading the page costs zero requests, so there's no rate limit, no pagination, and no cache to keep from going stale.
  • Authority. refInfo.name is GitHub's resolved answer for this exact URL, rather than a longest-prefix match reconstructed from a list.

Why a scraped value can't go wrong

refMatchesBlobPath() only accepts a ref that is a valid reading of the URL on screen — some leading run of the segments after /blob/, decoded and rejoined, with at least one segment left over to be the file path. So:

  • a value left behind by a soft navigation that changed refs describes a different URL, fails the check, and is ignored;
  • an abbreviated commit SHA in the picker doesn't equal the full SHA in the URL, fails the check, and the first-segment reading stands — which for a SHA URL is already the whole ref;
  • /blob/my%2Fbranch/..., where the slash arrives percent-encoded inside one segment, still matches, since every split is tried rather than assuming one URL segment per part of the name.

When nothing matches there is no override and today's behavior stands. Nothing guesses a ref.

Both sources are tried in turn rather than only the first one that has a value, so a source answering with something that doesn't fit the URL can't shadow one that answers correctly.

Verification

Code-correct — yarn test → 78/78 pass (67 before). New cases cover refNameFromRefSelectorLabel on the branch/tag label forms plus a ref literally named some branch, refNameFromEmbeddedData on the payload shape and a JSON-escaped name, and refMatchesBlobPath on multi-segment refs, percent-encoded refs, the stale-value and short-SHA rejections, a ref that would leave no file path, and non-file-view pathnames. yarn install --immutable, yarn build, yarn lint all clean (the only lint warnings are pre-existing ones in two other packages).

Live-observed (real github.com pages, logged in). The aria-label and embedded refInfo.name were both read off live pages — a slashed branch, main, and a soft navigation that switched from main to the slashed branch through the ref picker, which re-rendered both sources correctly (so neither goes stale on GitHub's client-side router).

End-to-end at the config-fetch layer. A disposable branch n8bot/tmp-slash-ref-check (no branch named n8bot exists, so the naive parse truncates to a dead ref) was pushed carrying a real .github/jump-links.config.yaml. Its file view was loaded in the browser, the two DOM values captured verbatim, and the compiled dist/ run against them:

page naive ref corrected ref
probe config on the slashed branch n8bot → 404 n8bot/tmp-slash-ref-check → 200, parsed to the probe's link
a file on nate-ai/generic-grafana-jump nate-ai → 404 nate-ai/generic-grafana-jump → 404 (no config on that branch)
a file on main (regression) main main — unchanged
stale DOM value against a main URL main main — override correctly rejected

The temp branch and its remote ref have been deleted.

Not verified live: the assembled userscript still hasn't been run in a browser session with this build — github.com's CSP forbids eval and injecting a script from anywhere but its own asset host, so the only route left is installing it over the userscript currently installed in the browser, which I didn't do unprompted. Say the word and I'll install this build and confirm a slashed branch's links actually render on its file view.

Co-Authored-By: Claude [email protected]

https://claude.ai/code/session_01LeMSMf28QJFLTiQ25sVM3S

…branch name has a slash

`/org/repo/blob/<ref>/<path>` doesn't mark where the ref ends and the path begins, so `parseRepoFileContext()` took the first segment after `/blob/` as the whole ref. On a branch whose name contains a slash that's a truncated, usually-nonexistent ref, so the branch-aware config fetch added in #51 either 404'd and silently dropped the repo's links, or matched an unrelated real branch sharing the truncated prefix.

GitHub has already resolved the ref to render the page and states the answer twice in the file view's DOM: the ref-picker button's `aria-label` ("<ref> branch"/"<ref> tag") and the `refInfo.name` in its embedded React payload. Both are now read and layered on top of the pure URL parse, the same way a run page's DOM-only ref already is.

Reading the page rather than reconstructing the ref from a `GET /repos/{org}/{repo}/branches` listing keeps it working on private repos - api.github.com isn't sent the browser's github.com session cookie, the trap already documented on `resolveDefaultBranch()` - and costs no requests, so there's no rate limit and nothing to cache or invalidate.

Whichever source answers, the value is only used if it's a valid reading of the URL on screen (`refMatchesBlobPath()`), which covers both a value going stale mid soft-navigation and the abbreviated-commit-SHA case. When neither source answers, the truncated first-segment reading stands - today's behavior. Nothing guesses a ref.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01LeMSMf28QJFLTiQ25sVM3S
@github-actions

Copy link
Copy Markdown

Userscript Version Preview

Preview only — versions and CHANGELOGs are bumped automatically on merge to main, not in this PR. Manual bumps to a higher version are preserved.

Package Base New Action
github-actions-grafana-jump 0.2.10 0.2.11 will-bump

@nsheaps-oura
nsheaps-oura merged commit 81d8e1c into main Aug 10, 2026
6 checks passed
@nsheaps-oura
nsheaps-oura deleted the n8bot/branch-ref-disambiguation branch August 10, 2026 21:02
nsheaps-oura added a commit that referenced this pull request Aug 11, 2026
…ge at a branch

Switching the branch selector on a repo's home page navigates to
/org/repo/tree/<ref>, a URL shape no page parser recognized - so no page
kind matched and the toolbar buttons disappeared entirely until you went
back to the default branch.

Verified against the live github.com DOM that this is the same page as
/org/repo: the repo header actions element the repoHome links are injected
into is present there with the same shape, on a hard load and after the
branch-selector navigation. It is absent on a directory view below the
root (/tree/<ref>/<path>), which stays unmatched as before - that really
is a different page with a different header.

So the ref'd root resolves to the existing repoHome kind, now carrying the
ref it's viewed at, and reads .github/jump-links.config.yaml from that
ref - the same branch-aware config lookup a file view and a
branch-filtered Actions tab already do (#51), and one of the pages you
land on while trying a config change out on a branch. The ref is not
exposed as a {{branch}} field: which fields the repo home page offers
shouldn't change with how you got there.

A ref containing a slash makes this URL ambiguous the same way it does for
/blob/ (#53): /tree/renovate/all-patch is equally the root at that ref or
directory all-patch at ref renovate. The single-segment form is read
purely from the URL; the ambiguous form is settled against the ref GitHub
itself resolved the page to, reusing #53's ref-picker/refInfo DOM reading,
and stays unmatched when the DOM doesn't answer rather than being guessed
at.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01LeMSMf28QJFLTiQ25sVM3S
nsheaps-oura added a commit that referenced this pull request Aug 12, 2026
…from the PR's own branch

A PR page kept reading .github/jump-links.config.yaml from the default
branch, so a config added on a branch didn't show up on the pull request
proposing it - the one page you're most likely to be looking at while
trying a config change out before merging it. Branch-aware config
(#51) skipped the PR
page because a PR's head branch isn't in its URL, and resolving it looked
like it needed an API call.

It doesn't: GitHub's PR header already states the merge in full ("wants to
merge N commits into <base> from <head>"), with each branch as a link to
its own tree. That's read from the live DOM the same way a workflow run's
branch already is
(#53), reusing that
change's tree-href parsing - which is why branchFromRunTreeHref is now
branchFromTreeHref, shared by both headers.

- A fork PR's head branch is in the contributor's own repo, so the shared
  href check rejects it and the config keeps coming from the default
  branch, matching what a fork-triggered run page already does.
- A merged or closed PR's head branch is usually deleted, so a PR target
  falls back to the default branch when its own ref has no config, rather
  than leaving the page with fewer links than before.

Verified live against the PR headers on jouzen/android#28338 (open draft,
same-repo), #51 (merged, head branch deleted)
and #54, and cli/cli#14108 (fork).
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01LeMSMf28QJFLTiQ25sVM3S
nsheaps-oura added a commit that referenced this pull request Aug 12, 2026
…from the PR's own branch (#55)

## The bug

Branch-aware repo config
([#51](#51)) reads
`.github/jump-links.config.yaml` at the ref you're looking at, but it
deliberately left the PR page out: a PR's head branch isn't in its URL,
and resolving it looked like it needed an API round-trip. So a PR page
always read the default branch's config — on the one page you're most
likely to be on while trying a config change out before merging it.

That's now a live problem: two open draft PRs
([jouzen/android#28338](jouzen/android#28338),
[jouzen/ios#32201](jouzen/ios#32201)) add a
config file that only exists on their own branch, and their PR pages
showed nothing.

**Reproduced live** on
[jouzen/android#28338](jouzen/android#28338):
the URL the old code builds is
`.../jouzen/android/HEAD/.github/jump-links.config.yaml`, and that path
doesn't exist on `main` yet (`gh api ... ?ref=main` → `Not Found`),
while the branch's own copy has a `pr` page entry ("CI traces for this
PR").

## What it does now

No API call needed — GitHub's PR header already states the whole merge,
with each branch as a link to its own tree. That gets read from the live
DOM the same way a workflow run's branch already is
([#53](#53)),
reusing that change's tree-href parsing. `branchFromRunTreeHref` is
therefore now `branchFromTreeHref`, shared by both headers.

```mermaid
flowchart TD
    A["/org/repo/pull/123"] --> B["head-branch link in the PR header"]
    B --> C{"is its href a branch<br/>of this same repo?"}
    C -->|yes| D["read the config at that branch"]
    C -->|"no (fork PR)"| E["read the default branch's, as before"]
    C -->|"header hasn't rendered yet"| F["default branch; re-checked on<br/>the next DOM mutation"]
    D --> G{"does that branch<br/>have a config?"}
    G -->|yes| H["use it"]
    G -->|"no (e.g. deleted after merge)"| E
```

### Which element, and why that one

The head branch is one whole href, so unlike the `/blob/<ref>/<path>`
URLs [#53](#53) had
to disambiguate, a slashed branch name needs no special handling here.
What the header actually renders, captured from
[#54](#54 own
page:

```html
<div class="d-flex flex-items-center overflow-hidden gap-1">
  <a href="/nsheaps/greasemonkey-scripts/tree/n8bot/tree-view-repohome"
     data-component="BranchName" ...>n8bot/tree-view-repohome</a>
  <span data-component="Tooltip" ...>nsheaps/greasemonkey-scripts:n8bot/tree-view-repohome</span>
  <button data-component="IconButton" ...>   <!-- "Copy head branch name to clipboard" -->
</div>
```

The base branch is the same component rendered right next to it, so
which one is the head is decided **structurally, not by position**: only
the head branch has the "copy branch name" button beside it, as an
element sibling of the link. Matched on `data-component`, Primer React's
own attribute — the class names around it
(`PullRequestBranchName-module__branchName__SCtl2`) are per-build hashes
and aren't used.

### Fork PRs and merged/closed PRs

| Case | Live check | Behavior |
| --- | --- | --- |
| Open PR, same repo |
[jouzen/android#28338](jouzen/android#28338)
(draft), [#54](#54)
| config read at the head branch |
| Fork PR | [cli/cli#14108](cli/cli#14108), head
`/loganrosen/cli-1/tree/...` | **no override** — href is a different
repo |
| Merged PR, head branch deleted |
[#51](#51) | head
branch still resolvable; its config 404s, so falls back to the default
branch |
| PR sub-tabs |
[#51](#51) Files
changed (`/pull/51/changes`) | same header, same answer |

A fork PR is skipped for the same two reasons the run page already skips
a fork-triggered run: that ref doesn't exist in the repo being browsed,
and a fork's copy of the config isn't this repo's config to read —
anyone can open a fork PR, so honoring one would let any contributor
decide what buttons the page offers.

A merged or closed PR's head branch is usually deleted, and insisting on
it would leave those pages with *no* repo config where before they had
the default branch's. So a PR target is the one target marked
`fallBackToDefaultBranch`: if its own ref has no config, the default
branch's is read instead. Both reads share the existing per-`{org, repo,
ref}` cache, so the fallback costs one extra request per repo, not one
per pass. Pages whose own URL names a ref keep
[#51](#51
stricter rule — that ref's version is the answer, including when it
hasn't got one.

## Verification

**Live, in the browser** (real github.com pages, logged in): ran the
compiled DOM-reading logic (`prHeadBranchHref` → `branchFromTreeHref` →
`repoConfigUrl`) against each page in the table above and confirmed the
resolved branch and the resulting raw URL. On
[jouzen/android#28338](jouzen/android#28338) it
resolves `n8bot/jump-links-config` and builds
`https://raw.githubusercontent.com/jouzen/android/n8bot/jump-links-config/.github/jump-links.config.yaml`,
where the old code built the `HEAD` URL that 404s. Also confirmed the
header renders the pair twice (page header + the sticky one when
scrolled) and both state the same branch.

**Code-correct:** `yarn test` — 83/83 pass, including new coverage for a
PR head href (slashed name), a fork PR's href, and the three
`fetchRepoConfigForTarget` fallback cases (falls back on a 404, doesn't
when the ref has its own config, doesn't at all for a ref the URL itself
named). `yarn install --immutable`, `yarn build`, `yarn lint` clean.

**Not verified:** the finished userscript running as an installed script
on a PR page. Same two blockers
[#54](#54) documents
— github.com's CSP blocks both loading the built script into the page
and any page-context fetch to `raw.githubusercontent.com` (which is
exactly why the script uses `GM.xmlHttpRequest`), and installing a dev
build into the browser's script manager would change local browser
config. So the config-fetch half was checked out of band (`gh api` for
the branch's file and its absence on `main`, `curl` for the 404 a
deleted branch gives) rather than through the script itself.

Co-Authored-By: Claude <[email protected]>

https://claude.ai/code/session_01LeMSMf28QJFLTiQ25sVM3S

---------

Co-authored-by: Claude <[email protected]>
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