Skip to content

rust-guard: extract first_matching_label helper and add label-check unit tests#8047

Merged
lpcox merged 2 commits into
mainfrom
copilot/rust-guard-extract-first-matching-label-helper
Jun 24, 2026
Merged

rust-guard: extract first_matching_label helper and add label-check unit tests#8047
lpcox merged 2 commits into
mainfrom
copilot/rust-guard-extract-first-matching-label-helper

Conversation

Copilot AI commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

first_matching_approval_label and first_matching_refusal_label were near-identical — differing only in which PolicyContext field they accessed. Adds a shared private helper and direct unit tests for all four label-check functions.

Refactor: extract first_matching_label

Replaces the duplicated body in both functions with a single helper that takes the label list directly:

fn first_matching_label<'a>(item: &'a Value, label_list: &[String]) -> Option<&'a str> {
    if label_list.is_empty() {
        return None;
    }
    extract_github_label_names(item)
        .into_iter()
        .find(|name| label_list.iter().any(|l| l.eq_ignore_ascii_case(name)))
}

fn first_matching_approval_label<'a>(item: &'a Value, ctx: &PolicyContext) -> Option<&'a str> {
    first_matching_label(item, &ctx.approval_labels)
}

fn first_matching_refusal_label<'a>(item: &'a Value, ctx: &PolicyContext) -> Option<&'a str> {
    first_matching_label(item, &ctx.refusal_labels)
}

Tests: direct unit tests for label-check helpers in helpers.rs

Adds 10 tests covering has_approval_label, has_refusal_label, has_promotion_label, and has_demotion_label:

  • Case-insensitive matching
  • Empty list / empty string → feature disabled (false)
  • Missing labels field on the item → false

GitHub Advanced Security started work on behalf of lpcox June 24, 2026 14:56 View session
GitHub Advanced Security finished work on behalf of lpcox June 24, 2026 14:57
GitHub Advanced Security started work on behalf of lpcox June 24, 2026 15:03 View session
Copilot AI changed the title [WIP] Extract shared first_matching_label helper and add label-check tests rust-guard: extract first_matching_label helper and add label-check unit tests Jun 24, 2026
Copilot AI requested a review from lpcox June 24, 2026 15:03
Copilot finished work on behalf of lpcox June 24, 2026 15:03
GitHub Advanced Security finished work on behalf of lpcox June 24, 2026 15:03
@lpcox lpcox marked this pull request as ready for review June 24, 2026 15:09
Copilot AI review requested due to automatic review settings June 24, 2026 15:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the rust-guard label-matching logic by extracting duplicated approval/refusal label search into a shared helper, and adds targeted unit tests for the label-check helpers to validate behavior like case-insensitive matching and “feature disabled” defaults.

Changes:

  • Extracted a shared first_matching_label helper used by both first_matching_approval_label and first_matching_refusal_label.
  • Added unit tests for has_approval_label, has_refusal_label, has_promotion_label, and has_demotion_label covering key edge cases (case-insensitivity, disabled config, missing labels).
Show a summary per file
File Description
guards/github-guard/rust-guard/src/labels/helpers.rs Deduplicates approval/refusal label matching via a shared helper and adds direct unit tests for label-checking helpers.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

@lpcox lpcox merged commit 3780159 into main Jun 24, 2026
40 checks passed
@lpcox lpcox deleted the copilot/rust-guard-extract-first-matching-label-helper branch June 24, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants