Skip to content

Commit 2ccbe21

Browse files
authored
Fetch configuration once when updating PRs (#277)
Previously, we fetched the same configuration file for every PR that might be updated. This was a lot of redundant requests, especially if the repository uses remote configuration.
1 parent 3ae63f8 commit 2ccbe21

8 files changed

Lines changed: 25 additions & 23 deletions

File tree

server/handler/base.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ type Base struct {
3232
PushRestrictionUserToken string
3333
}
3434

35-
func (b *Base) FetchConfig(ctx context.Context, client *github.Client, pr *github.PullRequest) (*bulldozer.Config, error) {
35+
func (b *Base) FetchConfigForPR(ctx context.Context, client *github.Client, pr *github.PullRequest) (*bulldozer.Config, error) {
36+
owner := pr.GetBase().GetRepo().GetOwner().GetLogin()
37+
repo := pr.GetBase().GetRepo().GetName()
38+
ref := pr.GetBase().GetRef()
39+
return b.FetchConfig(ctx, client, owner, repo, ref)
40+
}
41+
42+
func (b *Base) FetchConfig(ctx context.Context, client *github.Client, owner, repo, ref string) (*bulldozer.Config, error) {
3643
logger := zerolog.Ctx(ctx)
3744

38-
fc := b.ConfigFetcher.ConfigForPR(ctx, client, pr)
45+
fc := b.ConfigFetcher.Config(ctx, client, owner, repo, ref)
3946
switch {
4047
case fc.LoadError != nil:
4148
return nil, errors.Wrapf(fc.LoadError, "failed to load configuration: %s: %s", fc.Source, fc.Path)

server/handler/check_run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (h *CheckRun) Handle(ctx context.Context, eventType, deliveryID string, pay
7373
}
7474
pullCtx := pull.NewGithubContext(client, fullPR)
7575

76-
config, err := h.FetchConfig(ctx, client, fullPR)
76+
config, err := h.FetchConfigForPR(ctx, client, fullPR)
7777
if err != nil {
7878
return err
7979
}

server/handler/fetcher.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ func NewConfigFetcher(loader *appconfig.Loader, defaultConfig *bulldozer.Config)
4444
}
4545
}
4646

47-
func (cf *ConfigFetcher) ConfigForPR(ctx context.Context, client *github.Client, pr *github.PullRequest) FetchedConfig {
47+
func (cf *ConfigFetcher) Config(ctx context.Context, client *github.Client, owner, repo, ref string) FetchedConfig {
4848
logger := zerolog.Ctx(ctx)
4949

50-
owner := pr.GetBase().GetRepo().GetOwner().GetLogin()
51-
repo := pr.GetBase().GetRepo().GetName()
52-
ref := pr.GetBase().GetRef()
53-
5450
c, err := cf.loader.LoadConfig(ctx, client, owner, repo, ref)
5551
fc := FetchedConfig{
5652
Source: c.Source,

server/handler/issue_comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (h *IssueComment) Handle(ctx context.Context, eventType, deliveryID string,
5656
}
5757
pullCtx := pull.NewGithubContext(client, pr)
5858

59-
config, err := h.FetchConfig(ctx, client, pr)
59+
config, err := h.FetchConfigForPR(ctx, client, pr)
6060
if err != nil {
6161
return err
6262
}

server/handler/pull_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (h *PullRequest) Handle(ctx context.Context, eventType, deliveryID string,
6363
}
6464
pullCtx := pull.NewGithubContext(client, pr)
6565

66-
config, err := h.FetchConfig(ctx, client, pr)
66+
config, err := h.FetchConfigForPR(ctx, client, pr)
6767
if err != nil {
6868
return err
6969
}

server/handler/pull_request_review.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (h *PullRequestReview) Handle(ctx context.Context, eventType, deliveryID st
5656
}
5757
pullCtx := pull.NewGithubContext(client, pr)
5858

59-
config, err := h.FetchConfig(ctx, client, pr)
59+
config, err := h.FetchConfigForPR(ctx, client, pr)
6060
if err != nil {
6161
return errors.Wrap(err, "failed to fetch configuration")
6262
}

server/handler/push.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (h *Push) Handle(ctx context.Context, eventType, deliveryID string, payload
5353
}
5454

5555
ctx, logger := githubapp.PrepareRepoContext(ctx, installationID, ghRepo)
56+
logger.Debug().Msgf("Received push event with base ref %s", baseRef)
5657

5758
client, err := h.ClientCreator.NewInstallationClient(installationID)
5859
if err != nil {
@@ -63,24 +64,22 @@ func (h *Push) Handle(ctx context.Context, eventType, deliveryID string, payload
6364
if err != nil {
6465
return errors.Wrap(err, "failed to determine open pull requests matching the push change")
6566
}
66-
67-
logger.Debug().Msgf("received push event with base ref %s", baseRef)
68-
6967
if len(prs) == 0 {
70-
logger.Debug().Msg("Doing nothing since push event affects no open pull requests")
68+
logger.Debug().Msgf("Doing nothing since push to %s affects no open pull requests", baseRef)
7169
return nil
7270
}
7371

72+
// Fetch configuration once, since we know all PRs target the same ref
73+
config, err := h.FetchConfig(ctx, client, owner, repoName, baseRef)
74+
if err != nil {
75+
return err
76+
}
77+
7478
for _, pr := range prs {
75-
pullCtx := pull.NewGithubContext(client, pr)
7679
logger := logger.With().Int(githubapp.LogKeyPRNum, pr.GetNumber()).Logger()
80+
logger.Debug().Msgf("Considering pull request for update")
7781

78-
config, err := h.FetchConfig(ctx, client, pr)
79-
if err != nil {
80-
return errors.Wrap(err, "failed to fetch configuration")
81-
}
82-
83-
logger.Debug().Msgf("checking status for updated sha %s", baseRef)
82+
pullCtx := pull.NewGithubContext(client, pr)
8483
if _, err := h.UpdatePullRequest(logger.WithContext(ctx), pullCtx, client, config, pr, baseRef); err != nil {
8584
logger.Error().Err(errors.WithStack(err)).Msg("Error updating pull request")
8685
}

server/handler/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (h *Status) Handle(ctx context.Context, eventType, deliveryID string, paylo
6767
for _, pr := range prs {
6868
pullCtx := pull.NewGithubContext(client, pr)
6969
logger := logger.With().Int(githubapp.LogKeyPRNum, pr.GetNumber()).Logger()
70-
config, err := h.FetchConfig(ctx, client, pr)
70+
config, err := h.FetchConfigForPR(ctx, client, pr)
7171
if err != nil {
7272
return err
7373
}

0 commit comments

Comments
 (0)