Skip to content

Conversation

@Kamalesh-Seervi
Copy link

@Kamalesh-Seervi Kamalesh-Seervi commented Nov 13, 2025

Before this PR

Policy-bot lacked predicates to enforce policies based on:

  • Number of changed files in a PR (total, added, modified, deleted)
  • Number of commits in a PR
  • Commit message patterns and conventions

This made it difficult to implement common policies like:

  • Auto-approving small PRs
  • Requiring additional review for large changes
  • Enforcing commit message conventions (e.g., JIRA ticket references)
  • Detecting breaking changes mentioned in commit messages

Referenced issue: #1062

After this PR

Adds three new built-in predicates to enable more granular PR policies:

1. changed_files_count - Count changed files with advanced filtering

changed_files_count:
  total: "< 20"      # Total changed files
  added: "<= 5"      # Newly added files
  modified: "> 0"    # Modified files
  deleted: "< 2"     # Deleted files
  files:
    include:
      - "^src/.*"    # Only count files matching patterns
    exclude:
      - "^docs/.*"   # Exclude files matching patterns

2. commit_count - Count total commits in a PR

commit_count:
  total: "<= 10"     # Limit number of commits

3. commit_messages - Match commit message patterns

commit_messages:
  mode: all          # "all" (every commit) or "any" (at least one)
  scope: subject     # "subject", "body", or "full"
  matches:
    - "^JIRA-[0-9]+:"  # Required patterns
  not_matches:
    - "(?i)WIP|fixup"  # Forbidden patterns

Implementation Details:

  • All predicates follow existing code patterns and architecture
  • Added MessageHeadline and MessageBody fields to Commit struct for commit_messages predicate
  • Updated GraphQL queries to fetch commit message data
  • Comprehensive test coverage for all three predicates
  • All tests pass: ./godelw verify
  • Example policy configuration provided in new-predicates.yml

Example Use Cases:

  • Auto-approve PRs with < 20 files
  • Auto-approve PRs with ≤ 10 commits
  • Require admin approval for large PRs (≥ 20 files) unless they have an "exception" label
  • Enforce JIRA ticket references in commit messages
  • Flag PRs with breaking changes mentioned in commit bodies
  • Different approval requirements for documentation vs code changes

==COMMIT_MSG==
feat: Add changed_files_count, commit_count, and commit_messages predicates

Implements three new predicates requested in issue #1062:

  • changed_files_count: Count changed files with filtering by status and path patterns
  • commit_count: Count total commits in a PR with comparison operators
  • commit_messages: Match commit message patterns with configurable mode and scope

Also adds MessageHeadline and MessageBody fields to Commit struct to support
commit message pattern matching.

All predicates include comprehensive tests and follow existing code patterns.
==COMMIT_MSG==

Possible downsides?

Breaking Changes:

  • None. The new predicates are opt-in and don't affect existing policies.

GraphQL Query Changes:

  • Added messageHeadline and messageBody fields to commit queries. This increases the data fetched per commit but is necessary for the commit_messages predicate functionality.

Performance Considerations:

  • File filtering in changed_files_count iterates through all changed files, which should have minimal impact for typical PRs (< 1000 files).
  • Commit message matching in commit_messages iterates through all commits, efficient for typical PRs (< 100 commits).

API Compatibility:

  • The Commit struct now includes two new fields (MessageHeadline, MessageBody). This is backward compatible as Go's zero values (empty strings) are safe defaults.

@palantirtech
Copy link
Member

Thanks for your interest in palantir/policy-bot, @Kamalesh-Seervi! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@changelog-app
Copy link

changelog-app bot commented Nov 13, 2025

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

feat(go-predicate): customize

Check the box to generate changelog(s)

  • Generate changelog entry

@Kamalesh-Seervi
Copy link
Author

image

working sample

@asvoboda
Copy link
Member

As mentioned on the issue, it would be easier to review each of these predicate additions in separate PRs. Can you please split this up?

@Kamalesh-Seervi
Copy link
Author

hi @asvoboda I created 3 predicate pr separately 1105 to 1107

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