Skip to content

Commit 36ff56c

Browse files
authored
perf(rust-guard): precompute shared labels in response loops
1 parent 547a710 commit 36ff56c

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use super::constants::{field_names, label_constants, scope_names};
1414
use super::extract_mcp_response;
1515
use super::helpers::*;
16-
use crate::{LabeledItem, ResourceLabels};
16+
use crate::{LabeledItem, ResourceLabels, SharedLabels};
1717
use serde_json::Value;
1818

1919
/// Label individual items in a response (fine-grained labeling)
@@ -310,14 +310,16 @@ pub fn label_response_items(
310310
} else {
311311
writer_integrity(&repo_full_name, ctx)
312312
};
313+
let secrecy_shared: SharedLabels = secrecy.into();
314+
let file_integrity_shared: SharedLabels = file_integrity.into();
313315

314316
for &item in items_limited.iter() {
315317
labeled_items.push(LabeledItem {
316318
data: item.clone(),
317319
labels: ResourceLabels {
318320
description: format!("file:{}", repo_full_name),
319-
secrecy: secrecy.clone().into(),
320-
integrity: file_integrity.clone().into(),
321+
secrecy: secrecy_shared.clone(),
322+
integrity: file_integrity_shared.clone(),
321323
},
322324
});
323325
}
@@ -347,6 +349,7 @@ pub fn label_response_items(
347349
// requests, which should preserve merged-floor consistency with
348350
// list_commits-derived SHAs.
349351
let is_default_branch = is_default_branch_commit_context(tool_name, arg_branch);
352+
let secrecy_shared: SharedLabels = secrecy.into();
350353

351354
for item in items_limited.iter().copied() {
352355
let sha = item.get("sha").and_then(|v| v.as_str()).unwrap_or("");
@@ -359,7 +362,7 @@ pub fn label_response_items(
359362
data: item.clone(),
360363
labels: ResourceLabels {
361364
description: format!("commit:{}@{}", repo_full_name, short_sha),
362-
secrecy: secrecy.clone().into(),
365+
secrecy: secrecy_shared.clone(),
363366
integrity: integrity.into(),
364367
},
365368
});
@@ -374,6 +377,7 @@ pub fn label_response_items(
374377
let items_limited = limit_items_with_log(all_items.as_slice(), "list_gists");
375378

376379
let gist_integrity = reader_integrity(scope_names::USER, ctx);
380+
let gist_integrity_shared: SharedLabels = gist_integrity.into();
377381
for item in items_limited.iter().copied() {
378382
let is_public = get_bool_or(item, "public", true);
379383
let id = get_str_or(item, "id", "unknown");
@@ -390,7 +394,7 @@ pub fn label_response_items(
390394
labels: ResourceLabels {
391395
description: format!("gist:{}", id),
392396
secrecy: secrecy.into(),
393-
integrity: gist_integrity.clone().into(),
397+
integrity: gist_integrity_shared.clone(),
394398
},
395399
});
396400
}
@@ -403,14 +407,16 @@ pub fn label_response_items(
403407
if let Some(items) = items {
404408
let notif_secrecy = private_user_label();
405409
let notif_integrity = none_integrity("", ctx);
410+
let notif_secrecy_shared: SharedLabels = notif_secrecy.into();
411+
let notif_integrity_shared: SharedLabels = notif_integrity.into();
406412
for item in items.iter() {
407413
let id = get_str_or(item, "id", "unknown");
408414
labeled_items.push(LabeledItem {
409415
data: item.clone(),
410416
labels: ResourceLabels {
411417
description: format!("notification:{}", id),
412-
secrecy: notif_secrecy.clone().into(),
413-
integrity: notif_integrity.clone().into(),
418+
secrecy: notif_secrecy_shared.clone(),
419+
integrity: notif_integrity_shared.clone(),
414420
},
415421
});
416422
}
@@ -428,6 +434,8 @@ pub fn label_response_items(
428434
let secrecy = repo_visibility_secrecy(&arg_owner, &arg_repo, &repo_full_name, ctx);
429435

430436
let release_integrity = merged_integrity(&repo_full_name, ctx);
437+
let secrecy_shared: SharedLabels = secrecy.into();
438+
let release_integrity_shared: SharedLabels = release_integrity.into();
431439
for item in items_limited.iter().copied() {
432440
let tag = get_str_or(item, "tag_name", "unknown");
433441

@@ -436,8 +444,8 @@ pub fn label_response_items(
436444
data: item.clone(),
437445
labels: ResourceLabels {
438446
description: format!("release:{}@{}", repo_full_name, tag),
439-
secrecy: secrecy.clone().into(),
440-
integrity: release_integrity.clone().into(),
447+
secrecy: secrecy_shared.clone(),
448+
integrity: release_integrity_shared.clone(),
441449
},
442450
});
443451
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ pub fn label_response_paths(
350350

351351
// Commits on default branch (main/master) get merged-level integrity
352352
let is_default_branch = is_default_branch_ref(sha);
353+
let default_secrecy_shared: crate::SharedLabels = default_secrecy.clone().into();
353354

354355
let limited_items = limit_items_with_log(items, "list_commits");
355356
let mut labeled_paths = Vec::with_capacity(limited_items.len());
@@ -378,7 +379,7 @@ pub fn label_response_paths(
378379
path: format!("/{}", i),
379380
labels: crate::ResourceLabels {
380381
description: format!("commit:{}@{}", repo_for_labels, short_sha),
381-
secrecy: default_secrecy.clone().into(),
382+
secrecy: default_secrecy_shared.clone(),
382383
integrity: integrity.into(),
383384
},
384385
});
@@ -413,6 +414,8 @@ pub fn label_response_paths(
413414
} else {
414415
writer_integrity(&arg_repo_full, ctx)
415416
};
417+
let secrecy_shared: crate::SharedLabels = secrecy.clone().into();
418+
let file_integrity_shared: crate::SharedLabels = file_integrity.clone().into();
416419

417420
if let Some(items) = actual_response.as_array() {
418421
let limited_items = limit_items_with_log(items, "get_file_contents");
@@ -423,8 +426,8 @@ pub fn label_response_paths(
423426
path: format!("/{}", i),
424427
labels: crate::ResourceLabels {
425428
description: format!("file:{}", arg_repo_full),
426-
secrecy: secrecy.clone().into(),
427-
integrity: file_integrity.clone().into(),
429+
secrecy: secrecy_shared.clone(),
430+
integrity: file_integrity_shared.clone(),
428431
},
429432
});
430433
}
@@ -457,6 +460,7 @@ pub fn label_response_paths(
457460
};
458461
let default_secrecy =
459462
repo_visibility_secrecy(&arg_owner, &arg_repo, &default_repo, ctx);
463+
let default_secrecy_shared: crate::SharedLabels = default_secrecy.clone().into();
460464

461465
let limited_items = limit_items_with_log(items, "list_releases");
462466
let mut labeled_paths = Vec::with_capacity(limited_items.len());
@@ -476,7 +480,7 @@ pub fn label_response_paths(
476480
path: format!("/{}", i),
477481
labels: crate::ResourceLabels {
478482
description: format!("release:{}@{}", repo_for_labels, tag),
479-
secrecy: default_secrecy.clone().into(),
483+
secrecy: default_secrecy_shared.clone(),
480484
integrity: merged_integrity(repo_for_labels, ctx).into(),
481485
},
482486
});

0 commit comments

Comments
 (0)