Skip to content

Fix detectTargetBranch resolving head branch instead of base - #155

Merged
kaovilai merged 1 commit into
mainfrom
fix/detect-target-branch-base-ref
Aug 12, 2026
Merged

Fix detectTargetBranch resolving head branch instead of base#155
kaovilai merged 1 commit into
mainfrom
fix/detect-target-branch-base-ref

Conversation

@kaovilai

@kaovilai kaovilai commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Summary

  • detectTargetBranch() used .base-ref/.commit-ref selectors that no longer match the base branch on GitHub's modern Primer React PR header — .commit-ref in particular belongs to unrelated "force-pushed the X branch" timeline events, so it resolved to the PR's head branch instead of base.
  • Observed live on openshift/oadp-operator#2206: the wrong branch caused the openshift/release presubmits YAML lookup to 404, so lastPresubmitJobs was null and the Test buttons fell back to the raw check context (/test ci/prow/4.23-e2e-test-aws) instead of the bare job name (/test 4.23-e2e-test-aws).
  • Now derives the base branch from the PR header's /tree/ link that points at the current repo: base is always same-repo (head may be on a fork) and always precedes head in DOM order, so this holds regardless of GitHub's generated CSS class names.

Test plan

Note

Responses generated with Claude

Summary by CodeRabbit

  • Bug Fixes

    • Corrected pull request target-branch detection to use the base branch instead of the head branch.
    • Improved branch name handling for same-repository links, including URL decoding.
    • Retained compatibility with legacy branch indicators while removing unreliable detection logic.
  • Tests

    • Verified the fix against a real pull request scenario.
    • Confirmed all 191 unit tests pass.

.base-ref and .commit-ref no longer match the base branch on GitHub's
modern Primer React PR header; .commit-ref in particular belongs to
unrelated "force-pushed the X branch" timeline events. This made
detectTargetBranch() return the PR's head branch instead of base,
so the openshift/release presubmits YAML lookup 404'd and Test
buttons fell back to raw check names (e.g. "/test ci/prow/4.23-e2e-test-aws"
instead of "/test 4.23-e2e-test-aws"), as seen on
openshift/oadp-operator#2206.

Resolve the base branch from the PR header's /tree/ link that points
at the current repo instead: base is always same-repo (head may be a
fork) and always precedes head in DOM order, so this holds regardless
of GitHub's generated CSS class names.

Signed-off-by: Tiger Kaovilai <passawit.kaovilai@gmail.com>
Copilot AI lite review requested due to automatic review settings August 12, 2026 04:40
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 36ecc600-afcd-402b-a477-f37d602e6c38

📥 Commits

Reviewing files that changed from the base of the PR and between ae0d4bd and 3e6d892.

📒 Files selected for processing (2)
  • backlog/tasks/task-14 - Fix-detectTargetBranch-resolving-head-branch-instead-of-base-branch.md
  • content.js

Walkthrough

detectTargetBranch() now resolves the pull request base branch from a current-repository /tree/ link, with .base-ref as a legacy fallback. The completed task document records validation against the referenced pull request and 191 passing unit tests.

Changes

Target branch detection

Layer / File(s) Summary
Resolve the pull request base branch
content.js, backlog/tasks/task-14 - Fix-detectTargetBranch-resolving-head-branch-instead-of-base-branch.md
detectTargetBranch() decodes the branch from the current repository’s /tree/ link and falls back to .base-ref. The obsolete head-ref and .commit-ref fallbacks were removed. The task document records the fix and validation results.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: copilot

Poem

A rabbit checks the branches bright,
The base now points to what is right.
Tree links guide the hopping flow,
Old selectors wait below.
Tests all pass—ears up high!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change to fix incorrect head-branch resolution in detectTargetBranch().
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/detect-target-branch-base-ref

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes detectTargetBranch() in the content script so it reliably resolves a PR’s base branch (rather than accidentally picking up the head branch from unrelated DOM elements), preventing presubmit config lookups from 404’ing and forcing “Test” buttons to fall back to raw check context names.

Changes:

  • Updated detectTargetBranch() to derive the base branch from the first same-repo /tree/<branch> link in the PR header DOM instead of relying on stale .base-ref/.commit-ref selectors.
  • Removed the .commit-ref fallback that could resolve to the head branch via “force-pushed … branch” timeline events.
  • Added a Backlog task record documenting the bug, root cause, and fix.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
content.js Changes base-branch detection logic used for presubmit job lookup to avoid selecting the PR head branch.
backlog/tasks/task-14 - Fix-detectTargetBranch-resolving-head-branch-instead-of-base-branch.md Adds task documentation describing the issue and the implemented fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread content.js
Comment on lines +64 to +70
const prefix = `/${repo}/tree/`;
const treeLink = Array.from(document.querySelectorAll('a[href]'))
.find(a => a.getAttribute('href').startsWith(prefix));
if (treeLink) {
const branch = treeLink.getAttribute('href').slice(prefix.length);
if (branch) return decodeURIComponent(branch);
}
@kaovilai
kaovilai merged commit 6e37e50 into main Aug 12, 2026
4 checks passed
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.

2 participants