Skip to content
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

Adding Simulation API #725

Merged
merged 15 commits into from Mar 14, 2024
8 changes: 4 additions & 4 deletions policy/common/actor.go
Expand Up @@ -26,17 +26,17 @@ import (
// team and organization memberships. The set of allowed actors is the union of
// all conditions in this structure.
type Actors struct {
Users []string `yaml:"users"`
Teams []string `yaml:"teams"`
Organizations []string `yaml:"organizations"`
Users []string `yaml:"users" json:"users"`
Teams []string `yaml:"teams" json:"teams"`
Organizations []string `yaml:"organizations" json:"organizations"`

// Deprecated: use Permissions with "admin" or "write"
Admins bool `yaml:"admins"`
WriteCollaborators bool `yaml:"write_collaborators"`
atatkin marked this conversation as resolved.
Show resolved Hide resolved

// A list of GitHub collaborator permissions that are allowed. Values may
// be any of "admin", "maintain", "write", "triage", and "read".
Permissions []pull.Permission
Permissions []pull.Permission `json:"permissions"`
atatkin marked this conversation as resolved.
Show resolved Hide resolved
}

// IsEmpty returns true if no conditions for actors are defined.
Expand Down
135 changes: 135 additions & 0 deletions policy/simulated/context.go
@@ -0,0 +1,135 @@
// 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 simulated

import (
"context"
"fmt"

"github.com/palantir/policy-bot/pull"
)

type Context struct {
pull.Context
ctx context.Context
options Options
}

func NewContext(ctx context.Context, pullContext pull.Context, options Options) *Context {
return &Context{Context: pullContext, options: options}
}

func (c *Context) Comments() ([]*pull.Comment, error) {
comments, err := c.Context.Comments()
if err != nil {
return nil, err
}

comments, err = c.filterIgnoredComments(c.Context, comments)
if err != nil {
return nil, err
}

comments = c.addApprovalComment(comments)
return comments, nil
}

func (c *Context) filterIgnoredComments(prCtx pull.Context, comments []*pull.Comment) ([]*pull.Comment, error) {
if c.options.IgnoreComments == nil {
return comments, nil
}

var filteredComments []*pull.Comment
for _, comment := range comments {
isActor, err := c.options.IgnoreComments.IsActor(c.ctx, prCtx, comment.Author)
if err != nil {
return nil, err
}

if isActor {
continue
}

filteredComments = append(filteredComments, comment)
}

return filteredComments, nil
}

func (c *Context) addApprovalComment(comments []*pull.Comment) []*pull.Comment {
var commentsToAdd []*pull.Comment
for _, comment := range c.options.AddComments {
commentsToAdd = append(commentsToAdd, &comment)
}

return append(comments, commentsToAdd...)
}

func (c *Context) Reviews() ([]*pull.Review, error) {
reviews, err := c.Context.Reviews()
if err != nil {
return nil, err
}

reviews, err = c.filterIgnoredReviews(c.Context, reviews)
if err != nil {
return nil, err
}

reviews = c.addApprovalReview(reviews)
return reviews, nil
}

func (c *Context) filterIgnoredReviews(prCtx pull.Context, reviews []*pull.Review) ([]*pull.Review, error) {
if c.options.IgnoreReviews == nil {
return reviews, nil
}

var filteredReviews []*pull.Review
for _, review := range reviews {
isActor, err := c.options.IgnoreReviews.IsActor(c.ctx, prCtx, review.Author)
if err != nil {
return nil, err
}

if isActor {
continue
}

filteredReviews = append(filteredReviews, review)
}

return filteredReviews, nil
}

func (c *Context) addApprovalReview(reviews []*pull.Review) []*pull.Review {
var reviewsToAdd []*pull.Review
for i, review := range c.options.AddReviews {
review.ID = fmt.Sprintf("simulated-reviewID-%d", i)
review.SHA = fmt.Sprintf("simulated-reviewSHA-%d", i)
reviewsToAdd = append(reviewsToAdd, &review)
}

return append(reviews, reviewsToAdd...)
}

func (c *Context) Branches() (string, string) {
base, head := c.Context.Branches()
if c.options.BaseBranch != "" {
return c.options.BaseBranch, head
}

return base, head
}