Skip to content

Commit 0809cf2

Browse files
authored
add back following PR events (#60)
1 parent a34cd64 commit 0809cf2

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

server/handler/pull_request.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2018 Palantir Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package handler
16+
17+
import (
18+
"context"
19+
"encoding/json"
20+
21+
"github.com/google/go-github/github"
22+
"github.com/palantir/go-githubapp/githubapp"
23+
"github.com/pkg/errors"
24+
25+
"github.com/palantir/bulldozer/pull"
26+
)
27+
28+
type PullRequest struct {
29+
Base
30+
}
31+
32+
func (h *PullRequest) Handles() []string {
33+
return []string{"pull_request"}
34+
}
35+
36+
func (h *PullRequest) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
37+
var event github.PullRequestEvent
38+
if err := json.Unmarshal(payload, &event); err != nil {
39+
return errors.Wrap(err, "failed to parse pull request event payload")
40+
}
41+
42+
repo := event.GetRepo()
43+
owner := repo.GetOwner().GetLogin()
44+
repoName := repo.GetName()
45+
number := event.GetNumber()
46+
installationID := githubapp.GetInstallationIDFromEvent(&event)
47+
ctx, logger := githubapp.PreparePRContext(ctx, installationID, repo, number)
48+
49+
if event.GetAction() == "closed" {
50+
logger.Debug().Msg("Doing nothing since pull request is closed")
51+
return nil
52+
}
53+
54+
client, err := h.ClientCreator.NewInstallationClient(installationID)
55+
if err != nil {
56+
return errors.Wrap(err, "failed to instantiate github client")
57+
}
58+
59+
pr, _, err := client.PullRequests.Get(ctx, owner, repoName, number)
60+
if err != nil {
61+
return errors.Wrapf(err, "failed to get pull request %s/%s#%d", owner, repoName, number)
62+
}
63+
pullCtx := pull.NewGithubContext(client, pr, owner, repoName, number)
64+
65+
if err := h.ProcessPullRequest(ctx, pullCtx, client, pr); err != nil {
66+
logger.Error().Err(errors.WithStack(err)).Msg("Error processing pull request")
67+
}
68+
69+
return nil
70+
}
71+
72+
// type assertion
73+
var _ githubapp.EventHandler = &PullRequest{}

server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func New(c *Config) (*Server, error) {
7070

7171
webhookHandler := githubapp.NewDefaultEventDispatcher(c.Github,
7272
&handler.IssueComment{Base: baseHandler},
73+
&handler.PullRequest{Base: baseHandler},
7374
&handler.PullRequestReview{Base: baseHandler},
7475
&handler.Push{Base: baseHandler},
7576
&handler.Status{Base: baseHandler},

0 commit comments

Comments
 (0)