Skip to content

Commit d04258d

Browse files
authored
fix(rust-guard): hoist commit sha extraction and add commit-context tests
1 parent 5ce459a commit d04258d

1 file changed

Lines changed: 63 additions & 10 deletions

File tree

  • guards/github-guard/rust-guard/src/labels

guards/github-guard/rust-guard/src/labels/helpers.rs

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,14 +1986,15 @@ pub fn commit_integrity(
19861986
is_default_branch: bool,
19871987
ctx: &PolicyContext,
19881988
) -> Vec<String> {
1989+
let sha = item
1990+
.get("sha")
1991+
.and_then(|v| v.as_str())
1992+
.unwrap_or("unknown");
1993+
let short_sha = short_sha(sha);
1994+
19891995
// Step 1: Check if author is in blocked_users — takes precedence over all other rules.
19901996
let author_login = extract_author_login(item);
19911997
if !author_login.is_empty() && is_blocked_user(author_login, ctx) {
1992-
let sha = item
1993-
.get("sha")
1994-
.and_then(|v| v.as_str())
1995-
.unwrap_or("unknown");
1996-
let short_sha = short_sha(sha);
19971998
crate::log_info(&format!(
19981999
"[integrity] commit:{}@{} → blocked (author '{}' in blocked-users)",
19992000
repo_full_name, short_sha, author_login
@@ -2021,11 +2022,6 @@ pub fn commit_integrity(
20212022
// Collaborator permission fallback for public repos (handles owners/admins
20222023
// whose author_association is missing or "NONE").
20232024
if !repo_private {
2024-
let sha = item
2025-
.get("sha")
2026-
.and_then(|v| v.as_str())
2027-
.unwrap_or("unknown");
2028-
let short_sha = short_sha(sha);
20292025
integrity = elevate_via_collaborator_permission(
20302026
author_login,
20312027
repo_full_name,
@@ -2400,6 +2396,63 @@ mod tests {
24002396
}
24012397
}
24022398

2399+
// =========================================================================
2400+
// Tests for is_default_branch_commit_context / looks_like_commit_sha
2401+
// =========================================================================
2402+
2403+
#[test]
2404+
fn test_is_default_branch_commit_context_empty_ref_is_default() {
2405+
assert!(is_default_branch_commit_context("get_commit", ""));
2406+
assert!(is_default_branch_commit_context("list_commits", ""));
2407+
}
2408+
2409+
#[test]
2410+
fn test_is_default_branch_commit_context_main_master_head_are_default() {
2411+
for branch in &["main", "master", "HEAD", "Main", "MASTER"] {
2412+
assert!(
2413+
is_default_branch_commit_context("list_commits", branch),
2414+
"{branch} should be default-branch context"
2415+
);
2416+
}
2417+
}
2418+
2419+
#[test]
2420+
fn test_is_default_branch_commit_context_get_commit_with_sha_is_default() {
2421+
let sha40 = "a590b228c2e258907f503759c31c75bbfcd78a36";
2422+
assert!(is_default_branch_commit_context("get_commit", sha40));
2423+
assert!(is_default_branch_commit_context("get_commit", "abc1234"));
2424+
}
2425+
2426+
#[test]
2427+
fn test_is_default_branch_commit_context_list_commits_with_sha_is_not_default() {
2428+
let sha40 = "a590b228c2e258907f503759c31c75bbfcd78a36";
2429+
assert!(!is_default_branch_commit_context("list_commits", sha40));
2430+
}
2431+
2432+
#[test]
2433+
fn test_is_default_branch_commit_context_non_hex_sha_not_treated_as_commit() {
2434+
assert!(!is_default_branch_commit_context(
2435+
"get_commit",
2436+
"feature/my-branch"
2437+
));
2438+
assert!(!is_default_branch_commit_context(
2439+
"get_commit",
2440+
"v1.0.0-release"
2441+
));
2442+
}
2443+
2444+
#[test]
2445+
fn test_is_default_branch_commit_context_too_short_sha_not_treated_as_commit() {
2446+
assert!(!is_default_branch_commit_context("get_commit", "abc12"));
2447+
assert!(!is_default_branch_commit_context("get_commit", "abc123"));
2448+
}
2449+
2450+
#[test]
2451+
fn test_is_default_branch_commit_context_too_long_sha_not_treated_as_commit() {
2452+
let long_sha = "a590b228c2e258907f503759c31c75bbfcd78a361";
2453+
assert!(!is_default_branch_commit_context("get_commit", long_sha));
2454+
}
2455+
24032456
// =========================================================================
24042457
// Tests for commit_integrity
24052458
// =========================================================================

0 commit comments

Comments
 (0)