Skip to content

Commit b189619

Browse files
committed
Sync qualified branch handling into the advisory-link PR
2 parents 97ceb80 + 9e1d944 commit b189619

7 files changed

Lines changed: 98 additions & 38 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
### Fixed
2020

2121
- Automatic default-branch detection checks explicit scan refs against the
22-
default branch. An explicit commit must be available in the checkout for
23-
automatic promotion. When available, the selected commit supplies the commit
24-
message and the Git fallback for the committer. CI actor settings retain
25-
precedence.
22+
default branch, including qualified branch refs such as `refs/heads/main`.
23+
An explicit commit must be available in the checkout for automatic promotion.
24+
The selected commit supplies the commit message and Git committer fallback
25+
when available. CI actor settings retain precedence.
2626

2727
## 2.15.10
2828

‎docs/cli-reference.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,8 @@ Without either flag, the CLI marks the scan when automatic detection finds the d
728728
729729
When `--branch` or `--commit-sha` names a different ref than the CI variables, automatic detection also checks the ref you passed. The scan is marked only when that branch is the default branch and that commit is in the checkout and reachable from the default branch. An explicit commit that is missing from the checkout disqualifies automatic detection, even when it matches the SHA reported by CI. These flags never turn detection on.
730730
731+
In a Git checkout, `--branch refs/heads/main` and `--branch refs/remotes/origin/main` are recorded as `main`. The shorthand `origin/main` also resolves to `main` when Git recognizes it as the remote-tracking branch. A branch literally named `origin/main` keeps its name.
732+
731733
The commit check is reachability, so an older commit already in the default branch's history can still qualify: for example, a release tag that was merged back into the default branch, scanned with `--commit-sha` and no `--branch`. Pass `--no-default-branch` when a scan of such a ref must not change the default branch.
732734

733735
With `--commit-sha`, that commit supplies the commit message (unless `--commit-message` is set) and the git fallback for the committer, when the checkout has it. `--committers` and CI actor values (`GITHUB_ACTOR`, `GITLAB_USER_LOGIN`, `BITBUCKET_STEP_TRIGGERER_UUID`) still take precedence over the commit's committer.

‎pyproject.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ dependencies = [
1414
"mdutils==1.8.1",
1515
"prettytable==3.19.0",
1616
"gitpython==3.2.1",
17-
"packaging==26.3",
17+
"packaging==26.4",
1818
"python-dotenv==1.2.4",
1919
"socketdev==3.6.4",
2020
"beautifulsoup4==4.15.0",
21-
"markdown==3.11.3",
21+
"markdown==3.11.4",
2222
"brotli==1.2.1; platform_python_implementation == 'CPython'",
2323
"brotlicffi==1.2.1.0; platform_python_implementation != 'CPython'",
2424
# requests' CA bundle. A floor rather than an exact pin, so later trust-store
@@ -53,7 +53,7 @@ test = [
5353
dev = [
5454
"ruff==0.17.6",
5555
"twine==7.0.0", # for building
56-
"uv==0.13.9", # for dependency management
56+
"uv==0.13.10", # for dependency management
5757
"pre-commit==4.7.0",
5858
"hatch==1.18.2"
5959
]

‎socketsecurity/core/git_interface.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,28 @@ def _is_commit_and_branch_default(self) -> bool:
511511
log.debug(f"Error determining if commit and branch are default: {error}")
512512
return False
513513

514+
def scan_branch_name(self, branch: str) -> str:
515+
"""Resolve a qualified branch ref to the branch name used by scan requests."""
516+
for prefix in ("refs/heads/", "refs/remotes/origin/"):
517+
if branch.startswith(prefix):
518+
return branch.removeprefix(prefix)
519+
520+
if not branch.startswith("origin/"):
521+
return branch
522+
523+
# Shorthand can also be a literal branch name, such as origin/main.
524+
if (
525+
branch == self.branch
526+
or branch in self.repo.heads
527+
or self._resolve_ref(f"refs/remotes/origin/{branch}")
528+
):
529+
return branch
530+
531+
name = branch.removeprefix("origin/")
532+
if name == self.branch or self._resolve_ref(f"refs/remotes/{branch}"):
533+
return name
534+
return branch
535+
514536
def apply_explicit_ref(self, branch: str | None, commit_sha: str | None) -> None:
515537
"""Record explicit scan refs and use the selected commit's local metadata."""
516538
self._explicit_branch = branch if branch and branch != self.branch else None

‎socketsecurity/socketcli.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def apply_git_context(config: CliConfig) -> Tuple[bool, Optional[Git]]:
229229
except NoSuchPathError:
230230
raise Exception(f"Unable to find path {config.target_path}")
231231

232+
if config.branch:
233+
config.branch = git_repo.scan_branch_name(config.branch)
232234
git_repo.apply_explicit_ref(config.branch, config.commit_sha)
233235
if not config.repo:
234236
config.repo = git_repo.repo_name

‎tests/core/test_default_branch_flags.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Which default-branch and head-scan parameters a full scan is created with."""
22

33
import pytest
4+
from git import Repo
45

56
from socketsecurity import socketcli
67
from socketsecurity.core import Core
@@ -127,6 +128,39 @@ def test_scm_detection_still_marks_the_default_branch_run(
127128
assert params.commit_message.strip() == "dev work"
128129

129130

131+
@pytest.mark.parametrize("branch", ["refs/heads/develop", "origin/develop", "refs/remotes/origin/develop"])
132+
def test_qualified_default_branch_uses_the_short_branch_name(
133+
cli_runtime, github_dispatch_on_develop, branch # noqa: F811
134+
):
135+
assert github_dispatch_on_develop(["--branch", branch]) == 0
136+
137+
params = _scan_params(cli_runtime)
138+
assert params.branch == "develop"
139+
assert _flags(params) == (True, True)
140+
141+
142+
def test_qualified_scan_of_another_branch_does_not_mark_it_as_default(
143+
cli_runtime, github_dispatch_on_develop # noqa: F811
144+
):
145+
assert github_dispatch_on_develop(["--branch", "refs/heads/main"]) == 0
146+
147+
params = _scan_params(cli_runtime)
148+
assert params.branch == "main"
149+
assert params.make_default_branch is False
150+
151+
152+
def test_branch_named_like_a_remote_shorthand_keeps_its_name(
153+
cli_runtime, github_dispatch_on_develop, release_tag_checkout # noqa: F811
154+
):
155+
Repo(release_tag_checkout.path).create_head("origin/develop")
156+
157+
assert github_dispatch_on_develop(["--branch", "origin/develop"]) == 0
158+
159+
params = _scan_params(cli_runtime)
160+
assert params.branch == "origin/develop"
161+
assert params.make_default_branch is False
162+
163+
130164
def test_explicit_default_branch_overrides_the_explicit_ref_check(
131165
cli_runtime, github_dispatch_on_develop, release_tag_checkout # noqa: F811
132166
):

0 commit comments

Comments
 (0)