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

Add delay between pull request updates #278

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/c2h5oh/datasize"
"github.com/palantir/bulldozer/bulldozer"
"github.com/palantir/bulldozer/server/handler"
"github.com/palantir/go-baseapp/baseapp"
"github.com/palantir/go-baseapp/baseapp/datadog"
"github.com/palantir/go-githubapp/githubapp"
Expand Down Expand Up @@ -65,6 +66,8 @@ type Options struct {
SharedConfigurationPath string `yaml:"shared_configuration_path"`
DefaultRepositoryConfig *bulldozer.Config `yaml:"default_repository_config"`

handler.PullUpdateDelays `yaml:",inline"`

ConfigurationV0Paths []string `yaml:"configuration_v0_paths"`
}

Expand Down
62 changes: 58 additions & 4 deletions server/handler/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@ package handler
import (
"context"
"encoding/json"
"time"

"github.com/google/go-github/v41/github"
"github.com/palantir/bulldozer/bulldozer"
"github.com/palantir/bulldozer/pull"
"github.com/palantir/go-githubapp/githubapp"
"github.com/pkg/errors"
)

const (
DefaultPullUpdateQueryDelay = 250 * time.Millisecond
DefaultPullUpdateDelay = 2 * time.Second
)

type PullUpdateDelays struct {
PullUpdateQueryDelay *time.Duration `yaml:"pull_update_query_delay"`
PullUpdateDelay *time.Duration `yaml:"pull_update_delay"`
}

type Push struct {
Base

Delays PullUpdateDelays
}

func (h *Push) Handles() []string {
Expand Down Expand Up @@ -75,18 +89,58 @@ func (h *Push) Handle(ctx context.Context, eventType, deliveryID string, payload
return err
}

for _, pr := range prs {
if config == nil {
logger.Debug().Msg("Skipping pull request updates to missing configuration")
return nil
}

var toUpdate []updateCtx
for i, pr := range prs {
logger := logger.With().Int(githubapp.LogKeyPRNum, pr.GetNumber()).Logger()
logger.Debug().Msgf("Considering pull request for update")
logger.Debug().Msg("Checking if pull request should update")

ctx := logger.WithContext(ctx)
pullCtx := pull.NewGithubContext(client, pr)
if _, err := h.UpdatePullRequest(logger.WithContext(ctx), pullCtx, client, config, pr, baseRef); err != nil {
logger.Error().Err(errors.WithStack(err)).Msg("Error updating pull request")

shouldUpdate, err := bulldozer.ShouldUpdatePR(ctx, pullCtx, config.Update)
if err != nil {
logger.Error().Err(err).Msg("Error determining if pull request should update, skipping")
continue
}
if shouldUpdate {
toUpdate = append(toUpdate, updateCtx{
ctx: ctx,
pullCtx: pullCtx,
})
}

if i < len(prs)-1 {
time.Sleep(delay(h.Delays.PullUpdateQueryDelay, DefaultPullUpdateQueryDelay))
}
}

logger.Info().Msgf("Found %d pull requests that need updates", len(toUpdate))
for i, pr := range toUpdate {
bulldozer.UpdatePR(pr.ctx, pr.pullCtx, client, config.Update, baseRef)
if i < len(toUpdate)-1 {
time.Sleep(delay(h.Delays.PullUpdateDelay, DefaultPullUpdateDelay))
}
}

return nil
}

type updateCtx struct {
ctx context.Context
pullCtx pull.Context
}

func delay(opt *time.Duration, def time.Duration) time.Duration {
if opt != nil {
return *opt
}
return def
}

// type assertion
var _ githubapp.EventHandler = &Push{}
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func New(c *Config) (*Server, error) {
&handler.IssueComment{Base: baseHandler},
&handler.PullRequest{Base: baseHandler},
&handler.PullRequestReview{Base: baseHandler},
&handler.Push{Base: baseHandler},
&handler.Push{Base: baseHandler, Delays: c.Options.PullUpdateDelays},
&handler.Status{Base: baseHandler},
},
c.Github.App.WebhookSecret,
Expand Down