Skip to content

Commit 930f48c

Browse files
refactor(rust-guard): extract is_any_trusted_actor helper and collapse URL fallback loop
- Add is_any_trusted_actor(username, ctx) helper combining the three constituent trust predicates (first-party bot, configured bot, trusted user); replaces copy-pasted triple-OR at three call sites in helpers.rs and tool_rules.rs - Replace three structurally identical if-let blocks in extract_repo_from_item with a for-field loop over ["repository_url", "html_url", "url"], matching the idiom already used in extract_number_from_url All 317 Rust guard tests pass. Closes #4252 Co-authored-by: Copilot <[email protected]>
1 parent baa83f9 commit 930f48c

2 files changed

Lines changed: 19 additions & 32 deletions

File tree

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

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -973,23 +973,12 @@ pub fn extract_repo_from_item(item: &Value) -> String {
973973
{
974974
return name.to_string();
975975
}
976-
// repository_url parsing for search endpoints
977-
if let Some(url) = item.get("repository_url").and_then(|v| v.as_str()) {
978-
if let Some(repo_id) = extract_repo_from_github_url(url) {
979-
return repo_id;
980-
}
981-
}
982-
// html_url parsing as last resort - extract owner/repo from URLs like:
983-
// https://github.com/owner/repo/pull/123 or https://github.com/owner/repo/issues/456
984-
if let Some(url) = item.get("html_url").and_then(|v| v.as_str()) {
985-
if let Some(repo_id) = extract_repo_from_github_url(url) {
986-
return repo_id;
987-
}
988-
}
989-
// Generic URL field fallback
990-
if let Some(url) = item.get("url").and_then(|v| v.as_str()) {
991-
if let Some(repo_id) = extract_repo_from_github_url(url) {
992-
return repo_id;
976+
// URL field fallback (repository_url for search results, html_url / url as generic fallbacks)
977+
for field in &["repository_url", "html_url", "url"] {
978+
if let Some(url) = item.get(*field).and_then(|v| v.as_str()) {
979+
if let Some(repo_id) = extract_repo_from_github_url(url) {
980+
return repo_id;
981+
}
993982
}
994983
}
995984
String::new()
@@ -1278,11 +1267,7 @@ pub fn has_author_association(item: &Value) -> bool {
12781267
/// Users in the trusted_users list are also elevated to approved integrity.
12791268
pub fn author_association_floor(item: &Value, scope: &str, ctx: &PolicyContext) -> Vec<String> {
12801269
let author_login = extract_author_login(item);
1281-
if !author_login.is_empty()
1282-
&& (is_trusted_first_party_bot(author_login)
1283-
|| is_configured_trusted_bot(author_login, ctx)
1284-
|| is_trusted_user(author_login, ctx))
1285-
{
1270+
if !author_login.is_empty() && is_any_trusted_actor(author_login, ctx) {
12861271
return writer_integrity(scope, ctx);
12871272
}
12881273

@@ -1476,10 +1461,7 @@ pub fn pr_integrity(
14761461
);
14771462
// Elevate trusted bots and trusted users
14781463
let enriched_floor = if let Some(ref login) = facts.author_login {
1479-
if is_trusted_first_party_bot(login)
1480-
|| is_configured_trusted_bot(login, ctx)
1481-
|| is_trusted_user(login, ctx)
1482-
{
1464+
if is_any_trusted_actor(login, ctx) {
14831465
max_integrity(
14841466
repo_full_name,
14851467
enriched_floor,
@@ -1772,6 +1754,14 @@ pub fn is_trusted_user(username: &str, ctx: &PolicyContext) -> bool {
17721754
username_in_list(username, &ctx.trusted_users)
17731755
}
17741756

1757+
/// Returns `true` if `username` belongs to any trusted actor tier:
1758+
/// first-party bots, gateway-configured bots, or trusted users.
1759+
pub(crate) fn is_any_trusted_actor(username: &str, ctx: &PolicyContext) -> bool {
1760+
is_trusted_first_party_bot(username)
1761+
|| is_configured_trusted_bot(username, ctx)
1762+
|| is_trusted_user(username, ctx)
1763+
}
1764+
17751765

17761766
#[cfg(test)]
17771767
mod tests {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use super::helpers::{
1010
author_association_floor_from_str,
1111
elevate_via_collaborator_permission, ensure_integrity_baseline,
1212
extract_number_as_string, extract_repo_info, extract_repo_info_from_search_query,
13-
format_repo_id, is_configured_trusted_bot, is_default_branch_commit_context,
14-
is_default_branch_ref, is_trusted_first_party_bot, is_trusted_user, max_integrity,
13+
format_repo_id, is_any_trusted_actor, is_default_branch_commit_context,
14+
is_default_branch_ref, max_integrity,
1515
merged_integrity, policy_private_scope_label, private_user_label, project_github_label,
1616
reader_integrity, writer_integrity, PolicyContext,
1717
};
@@ -95,10 +95,7 @@ fn resolve_author_integrity(
9595
let mut floor = author_association_floor_from_str(repo_id, author_association, ctx);
9696

9797
if let Some(login) = author_login {
98-
if is_trusted_first_party_bot(login)
99-
|| is_configured_trusted_bot(login, ctx)
100-
|| is_trusted_user(login, ctx)
101-
{
98+
if is_any_trusted_actor(login, ctx) {
10299
floor = max_integrity(repo_id, floor, writer_integrity(repo_id, ctx), ctx);
103100
}
104101
let resource_id = format!("{}/{}#{}", owner, repo, resource_num);

0 commit comments

Comments
 (0)