Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions config/policy-examples/new-predicates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Example policy demonstrating the three new predicates:
# - changed_files_count: Count changed files with filters
# - commit_count: Count total commits in a PR
# - commit_messages: Match commit message patterns

policy:
approval:
- name: Small changes auto-approve
description: PRs with fewer than 20 files are automatically approved
requires:
count: 0 # Auto-approve
if:
changed_files_count:
total: "< 20"

- name: Large changes with exception label
description: PRs with 20+ files but have exception label are approved
requires:
count: 0 # Auto-approve with label
if:
has_labels:
labels:
- "exception"
changed_files_count:
total: ">= 20"

- name: Large changes without exception need approval
description: PRs with 20+ files without exception label need admin approval
requires:
count: 1
admins: true
if:
changed_files_count:
total: ">= 20"

- name: Keep PRs small - auto approve
description: PRs with 10 or fewer commits are automatically approved
requires:
count: 0 # Auto-approve
if:
commit_count:
total: "<= 10"

- name: General approval not requiring JIRA
description: Standard PRs don't need JIRA tickets in commits
requires:
count: 1
admins: true

- name: Check for breaking changes
description: PRs with breaking changes need special review
requires:
count: 2
teams:
- "org/breaking-change-reviewers"
if:
commit_messages:
mode: any
scope: body
matches:
- "(?i)breaking change"

- name: Documentation changes need fewer reviews
description: Documentation-only changes need just one approval
requires:
count: 1
admins: true
if:
changed_files_count:
total: "< 100"
files:
include:
- "^docs/.*"
exclude:
- "^src/.*"
- "^test/.*"
164 changes: 164 additions & 0 deletions policy/predicate/changed_files_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright 2018 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package predicate

import (
"context"
"fmt"

"github.com/palantir/policy-bot/policy/common"
"github.com/palantir/policy-bot/pull"
"github.com/pkg/errors"
)

type ChangedFilesCount struct {
Total ComparisonExpr `yaml:"total,omitempty"`
Added ComparisonExpr `yaml:"added,omitempty"`
Modified ComparisonExpr `yaml:"modified,omitempty"`
Deleted ComparisonExpr `yaml:"deleted,omitempty"`
Files ChangedFilesCountFileFilter `yaml:"files,omitempty"`
}

type ChangedFilesCountFileFilter struct {
Include []common.Regexp `yaml:"include,omitempty"`
Exclude []common.Regexp `yaml:"exclude,omitempty"`
}

func (ff ChangedFilesCountFileFilter) IsZero() bool {
return len(ff.Include) == 0 && len(ff.Exclude) == 0
}

func (ff ChangedFilesCountFileFilter) MatchesFile(filename string) bool {
if len(ff.Exclude) > 0 && anyMatches(ff.Exclude, filename) {
return false
}
if len(ff.Include) > 0 && !anyMatches(ff.Include, filename) {
return false
}
return true
}

var _ Predicate = &ChangedFilesCount{}

func (pred *ChangedFilesCount) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) {
predicateResult := common.PredicateResult{
ValuePhrase: "changed files",
ConditionPhrase: "meet",
ConditionsMap: make(map[string][]string),
}

files, err := prctx.ChangedFiles()
if err != nil {
return nil, errors.Wrap(err, "failed to list changed files")
}

// Count files by status
var totalCount, addedCount, modifiedCount, deletedCount int64
for _, f := range files {
if !pred.Files.MatchesFile(f.Filename) {
continue
}

totalCount++
switch f.Status {
case pull.FileAdded:
addedCount++
case pull.FileModified:
modifiedCount++
case pull.FileDeleted:
deletedCount++
}
}

// Add file filter information if specified
if len(pred.Files.Include) > 0 {
predicateResult.ConditionsMap["in files matching"] = getPathStrings(pred.Files.Include)
}
if len(pred.Files.Exclude) > 0 {
predicateResult.ConditionsMap["excluding files matching"] = getPathStrings(pred.Files.Exclude)
}

const conditionKey = "the file count conditions"

// Check total condition
if !pred.Total.IsEmpty() {
value := fmt.Sprintf("total %d", totalCount)
cond := fmt.Sprintf("total files %s", pred.Total.String())

if pred.Total.Evaluate(totalCount) {
predicateResult.Values = []string{value}
predicateResult.ConditionsMap[conditionKey] = []string{cond}
predicateResult.Satisfied = true
return &predicateResult, nil
}

predicateResult.Values = append(predicateResult.Values, value)
predicateResult.ConditionsMap[conditionKey] = append(predicateResult.ConditionsMap[conditionKey], cond)
}

// Check added condition
if !pred.Added.IsEmpty() {
value := fmt.Sprintf("added %d", addedCount)
cond := fmt.Sprintf("added files %s", pred.Added.String())

if pred.Added.Evaluate(addedCount) {
predicateResult.Values = []string{value}
predicateResult.ConditionsMap[conditionKey] = []string{cond}
predicateResult.Satisfied = true
return &predicateResult, nil
}

predicateResult.Values = append(predicateResult.Values, value)
predicateResult.ConditionsMap[conditionKey] = append(predicateResult.ConditionsMap[conditionKey], cond)
}

// Check modified condition
if !pred.Modified.IsEmpty() {
value := fmt.Sprintf("modified %d", modifiedCount)
cond := fmt.Sprintf("modified files %s", pred.Modified.String())

if pred.Modified.Evaluate(modifiedCount) {
predicateResult.Values = []string{value}
predicateResult.ConditionsMap[conditionKey] = []string{cond}
predicateResult.Satisfied = true
return &predicateResult, nil
}

predicateResult.Values = append(predicateResult.Values, value)
predicateResult.ConditionsMap[conditionKey] = append(predicateResult.ConditionsMap[conditionKey], cond)
}

// Check deleted condition
if !pred.Deleted.IsEmpty() {
value := fmt.Sprintf("deleted %d", deletedCount)
cond := fmt.Sprintf("deleted files %s", pred.Deleted.String())

if pred.Deleted.Evaluate(deletedCount) {
predicateResult.Values = []string{value}
predicateResult.ConditionsMap[conditionKey] = []string{cond}
predicateResult.Satisfied = true
return &predicateResult, nil
}

predicateResult.Values = append(predicateResult.Values, value)
predicateResult.ConditionsMap[conditionKey] = append(predicateResult.ConditionsMap[conditionKey], cond)
}

return &predicateResult, nil
}

func (pred *ChangedFilesCount) Trigger() common.Trigger {
return common.TriggerCommit
}
Loading