-
Notifications
You must be signed in to change notification settings - Fork 127
Custom properties predicate #1054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bluekeyes
merged 8 commits into
palantir:develop
from
mercari:custom-properties-predicate
Nov 20, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25953a3
Implement custom properties
south-mer c0decb7
Use common.Regexp instead
south-mer 2932048
Update README.md
south-mer f2c2880
Refactoring
south-mer fe408b0
Fix verify
south-mer ea7bbfc
Accept some suggestions
south-mer baf70ef
Accept some suggestions with change
south-mer ec35069
Add comment
south-mer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| // Copyright 2025 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" | ||
| "maps" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/palantir/policy-bot/policy/common" | ||
| "github.com/palantir/policy-bot/pull" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| type CustomPropertyIsNull []string | ||
| type CustomPropertyIsNotNull []string | ||
| type CustomPropertyMatchesAnyOf map[string][]common.Regexp | ||
| type CustomPropertyMatchesNoneOf map[string][]common.Regexp | ||
|
|
||
| var _ Predicate = (CustomPropertyIsNull)(nil) | ||
| var _ Predicate = (CustomPropertyIsNotNull)(nil) | ||
| var _ Predicate = (CustomPropertyMatchesAnyOf)(nil) | ||
| var _ Predicate = (CustomPropertyMatchesNoneOf)(nil) | ||
|
|
||
| func formatCustomProperties(customProperties map[string]pull.CustomProperty) []string { | ||
| result := []string{} | ||
| keys := slices.Sorted(maps.Keys(customProperties)) | ||
| for _, k := range keys { | ||
| v := customProperties[k] | ||
| formatted := "(null)" | ||
| if v.String != nil { | ||
| formatted = *v.String | ||
| } | ||
| if v.Array != nil { | ||
| formatted = "[" + strings.Join(v.Array, ", ") + "]" | ||
| } | ||
| result = append(result, fmt.Sprintf("%s: %s", k, formatted)) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func (pred CustomPropertyIsNotNull) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) { | ||
| customProperties, err := prctx.RepositoryCustomProperties() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to get repository custom properties") | ||
| } | ||
|
|
||
| predicateResult := common.PredicateResult{ | ||
| ValuePhrase: "custom properties", | ||
| Values: formatCustomProperties(customProperties), | ||
| ConditionPhrase: "contain", | ||
| ConditionValues: pred, | ||
| Satisfied: true, | ||
| } | ||
|
|
||
| for _, property := range pred { | ||
| // For custom properties, empty strings and empty arrays are considered unset, and are not returned from the API. | ||
| if _, ok := customProperties[property]; !ok { | ||
| predicateResult.Satisfied = false | ||
| return &predicateResult, nil | ||
| } | ||
| } | ||
|
|
||
| return &predicateResult, nil | ||
| } | ||
|
|
||
| func (pred CustomPropertyIsNull) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) { | ||
| customProperties, err := prctx.RepositoryCustomProperties() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to get repository custom properties") | ||
| } | ||
|
|
||
| predicateResult := common.PredicateResult{ | ||
| ValuePhrase: "custom properties", | ||
| Values: formatCustomProperties(customProperties), | ||
| ReverseSkipPhrase: true, | ||
| ConditionPhrase: "contain", | ||
| ConditionValues: pred, | ||
| Satisfied: true, | ||
| } | ||
|
|
||
| for _, property := range pred { | ||
| // For custom properties, empty strings and empty arrays are considered unset, and are not returned from the API. | ||
| if _, ok := customProperties[property]; ok { | ||
| predicateResult.Satisfied = false | ||
| return &predicateResult, nil | ||
| } | ||
| } | ||
|
|
||
| return &predicateResult, nil | ||
| } | ||
|
|
||
| func (pred CustomPropertyMatchesAnyOf) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) { | ||
| customProperties, err := prctx.RepositoryCustomProperties() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to get repository custom properties") | ||
| } | ||
|
|
||
| conditionsMap := make(map[string][]string, len(pred)) | ||
| for property, allowedValues := range pred { | ||
| strValues := make([]string, len(allowedValues)) | ||
| for i, v := range allowedValues { | ||
| strValues[i] = v.String() | ||
| } | ||
| conditionsMap[property] = strValues | ||
| } | ||
|
|
||
| predicateResult := common.PredicateResult{ | ||
| ValuePhrase: "custom properties", | ||
| Values: formatCustomProperties(customProperties), | ||
| ConditionPhrase: "match one or more of the patterns", | ||
| ConditionsMap: conditionsMap, | ||
| Satisfied: true, | ||
| } | ||
|
|
||
| for property, allowedValues := range pred { | ||
| propValue, ok := customProperties[property] | ||
| if !ok { | ||
| predicateResult.Satisfied = false | ||
| return &predicateResult, nil | ||
| } | ||
|
|
||
| if propValue.String == nil || !anyMatches(allowedValues, *propValue.String) { | ||
| predicateResult.Satisfied = false | ||
| return &predicateResult, nil | ||
| } | ||
| } | ||
|
|
||
| return &predicateResult, nil | ||
| } | ||
|
|
||
| func (pred CustomPropertyMatchesNoneOf) Evaluate(ctx context.Context, prctx pull.Context) (*common.PredicateResult, error) { | ||
| customProperties, err := prctx.RepositoryCustomProperties() | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to get repository custom properties") | ||
| } | ||
|
|
||
| conditionsMap := make(map[string][]string, len(pred)) | ||
| for property, allowedValues := range pred { | ||
| strValues := make([]string, len(allowedValues)) | ||
| for i, v := range allowedValues { | ||
| strValues[i] = v.String() | ||
| } | ||
| conditionsMap[property] = strValues | ||
| } | ||
|
|
||
| predicateResult := common.PredicateResult{ | ||
| ValuePhrase: "custom properties", | ||
| Values: formatCustomProperties(customProperties), | ||
| ReverseSkipPhrase: true, | ||
| ConditionPhrase: "match one or more of the patterns", | ||
| ConditionsMap: conditionsMap, | ||
| Satisfied: true, | ||
| } | ||
|
|
||
| for property, disallowedValues := range pred { | ||
| propValue, ok := customProperties[property] | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| if propValue.String != nil && anyMatches(disallowedValues, *propValue.String) { | ||
| predicateResult.Satisfied = false | ||
| return &predicateResult, nil | ||
| } | ||
| } | ||
|
|
||
| return &predicateResult, nil | ||
| } | ||
|
|
||
| func (pred CustomPropertyIsNotNull) Trigger() common.Trigger { return common.TriggerStatic } | ||
| func (pred CustomPropertyIsNull) Trigger() common.Trigger { return common.TriggerStatic } | ||
| func (pred CustomPropertyMatchesAnyOf) Trigger() common.Trigger { return common.TriggerStatic } | ||
| func (pred CustomPropertyMatchesNoneOf) Trigger() common.Trigger { return common.TriggerStatic } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.