|
| 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 main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/google/go-github/v33/github" |
| 24 | + "github.com/palantir/go-githubapp/githubapp" |
| 25 | + |
| 26 | + "github.com/go-git/go-git/v5" |
| 27 | + "github.com/go-git/go-git/v5/plumbing" |
| 28 | + transport_http "github.com/go-git/go-git/v5/plumbing/transport/http" |
| 29 | + "github.com/go-git/go-git/v5/storage/memory" |
| 30 | + "github.com/pkg/errors" |
| 31 | + "github.com/rs/zerolog" |
| 32 | +) |
| 33 | + |
| 34 | +type PRReviewHandler struct { |
| 35 | + githubapp.ClientCreator |
| 36 | + |
| 37 | + preamble string |
| 38 | +} |
| 39 | + |
| 40 | +func (h *PRReviewHandler) Handles() []string { |
| 41 | + return []string{"pull_request"} |
| 42 | +} |
| 43 | + |
| 44 | +func (h *PRReviewHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error { |
| 45 | + var event github.IssueCommentEvent |
| 46 | + if err := json.Unmarshal(payload, &event); err != nil { |
| 47 | + return errors.Wrap(err, "failed to parse issue comment event payload") |
| 48 | + } |
| 49 | + |
| 50 | + if !event.GetIssue().IsPullRequest() { |
| 51 | + zerolog.Ctx(ctx).Debug().Msg("Issue comment event is not for a pull request") |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + repo := event.GetRepo() |
| 56 | + prNum := event.GetIssue().GetNumber() |
| 57 | + installationID := githubapp.GetInstallationIDFromEvent(&event) |
| 58 | + |
| 59 | + ctx, logger := githubapp.PreparePRContext(ctx, installationID, repo, event.GetIssue().GetNumber()) |
| 60 | + |
| 61 | + logger.Debug().Msgf("Event action is %s", event.GetAction()) |
| 62 | + if event.GetAction() != "created" { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + // Get Access Token |
| 67 | + client, ts, err := h.NewInstallationClient(installationID) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + token, err := ts.Token(context.Background()) |
| 72 | + |
| 73 | + // Clone the repository |
| 74 | + tokenAuth := &transport_http.BasicAuth{Username: "x-access-token", Password: token} |
| 75 | + storer := memory.NewStorage() |
| 76 | + gitRepo, err := git.Clone(storer, nil, &git.CloneOptions{ |
| 77 | + URL: "https://github.com/palantir/go-githubapp.git", |
| 78 | + Auth: tokenAuth, |
| 79 | + }) |
| 80 | + |
| 81 | + // Insert your own advanced Git scenario here: |
| 82 | + mainRef, _ := gitRepo.Reference(plumbing.NewBranchReferenceName(event.GetRepo().GetMasterBranch()), true) |
| 83 | + commit, _ := gitRepo.CommitObject(mainRef.Hash()) |
| 84 | + logger.Debug().Msgf("Last commit on master was by %s", commit.Author.Name) |
| 85 | + |
| 86 | + // Remainder of old logic... |
| 87 | + repoOwner := repo.GetOwner().GetLogin() |
| 88 | + repoName := repo.GetName() |
| 89 | + author := event.GetComment().GetUser().GetLogin() |
| 90 | + body := event.GetComment().GetBody() |
| 91 | + |
| 92 | + if strings.HasSuffix(author, "[bot]") { |
| 93 | + logger.Debug().Msg("Issue comment was created by a bot") |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + logger.Debug().Msgf("Echoing comment on %s/%s#%d by %s", repoOwner, repoName, prNum, author) |
| 98 | + msg := fmt.Sprintf("%s\n%s said\n```\n%s\n```\n", h.preamble, author, body) |
| 99 | + prComment := github.IssueComment{ |
| 100 | + Body: &msg, |
| 101 | + } |
| 102 | + |
| 103 | + if _, _, err := client.Issues.CreateComment(ctx, repoOwner, repoName, prNum, &prComment); err != nil { |
| 104 | + logger.Error().Err(err).Msg("Failed to comment on pull request") |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
0 commit comments