|
| 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{} |
0 commit comments