Commit ef415e0
authored
[rust-guard] Deduplicate owner/repo parsing paths in labels backend (#5548)
## ✨ Enhancement
This change removes two duplication hotspots in
`guards/github-guard/rust-guard/src/labels/backend.rs` without altering
behavior: repeated item-scan logic in owner-type extraction and repeated
`full_name`/`fullName` lookup logic in repo ID extraction. The result is
a smaller, more maintainable parsing path with unchanged field
precedence and fallback behavior.
**What does this improve?**
- **Owner-type lookup deduplication**
- Introduces `find_org_in_items(items, repo_id)` and reuses it for both
response shapes:
- `{ "items": [...] }`
- plain array `[...]`
- **Repo ID field lookup deduplication**
- Replaces duplicate `if` blocks for `"full_name"` and `"fullName"` with
a single ordered loop.
- **Regression coverage for refactor paths**
- Adds focused tests for:
- plain-array owner/org extraction path
- camelCase `fullName` repo ID extraction path
**Why is this valuable?**
- Reduces copy/paste control flow in high-traffic parsing helpers.
- Centralizes repo-ID match logic used by owner-type extraction.
- Makes future alias additions and edge-case coverage lower-risk.
**Implementation approach:**
- Extracted shared iterator-based helper:
```rust
fn find_org_in_items(items: &[Value], repo_id: &str) -> Option<bool> {
items.iter().find_map(|item| {
let item_repo_id = repo_id_from_repo_object(item)?;
if item_repo_id.eq_ignore_ascii_case(repo_id) {
owner_type_from_repo_object(item)
} else {
None
}
})
}
```
- Updated `owner_is_org_from_items` to delegate both array shapes to the
helper.
- Updated `repo_id_from_repo_object` to iterate `["full_name",
"fullName"]` in priority order, preserving existing behavior.1 file changed
Lines changed: 42 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1243 | 1243 | | |
1244 | 1244 | | |
1245 | 1245 | | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
1246 | 1287 | | |
1247 | 1288 | | |
1248 | 1289 | | |
| |||
1384 | 1425 | | |
1385 | 1426 | | |
1386 | 1427 | | |
1387 | | - | |
| 1428 | + | |
1388 | 1429 | | |
1389 | 1430 | | |
1390 | 1431 | | |
| |||
0 commit comments