Skip to content

Commit 46629a4

Browse files
committed
(WIP) example of using TokenSource
1 parent 1e9f88c commit 46629a4

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed

example/pr_review.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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"
27+
"github.com/go-git/go-git/storage/memory"
28+
transport_http "github.com/go-git/go-git/v5/plumbing/transport/http"
29+
"github.com/pkg/errors"
30+
"github.com/rs/zerolog"
31+
)
32+
33+
type PRReviewHandler struct {
34+
githubapp.ClientCreator
35+
36+
preamble string
37+
}
38+
39+
func (h *PRReviewHandler) Handles() []string {
40+
return []string{"pull_request"}
41+
}
42+
43+
func (h *PRReviewHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
44+
var event github.IssueCommentEvent
45+
if err := json.Unmarshal(payload, &event); err != nil {
46+
return errors.Wrap(err, "failed to parse issue comment event payload")
47+
}
48+
49+
if !event.GetIssue().IsPullRequest() {
50+
zerolog.Ctx(ctx).Debug().Msg("Issue comment event is not for a pull request")
51+
return nil
52+
}
53+
54+
repo := event.GetRepo()
55+
prNum := event.GetIssue().GetNumber()
56+
installationID := githubapp.GetInstallationIDFromEvent(&event)
57+
58+
ctx, logger := githubapp.PreparePRContext(ctx, installationID, repo, event.GetIssue().GetNumber())
59+
60+
logger.Debug().Msgf("Event action is %s", event.GetAction())
61+
if event.GetAction() != "created" {
62+
return nil
63+
}
64+
65+
client, ts, err := h.NewInstallationClient(installationID)
66+
if err != nil {
67+
return err
68+
}
69+
70+
token, err := ts.Token(context.Background())
71+
tokenAuth := &transport_http.TokenAuth{Token: token}
72+
storer := memory.NewStorage()
73+
gitRepo, err := git.Clone(storer, nil, &git.CloneOptions{
74+
URL: "https://github.com/palantir/go-githubapp.git",
75+
Auth: tokenAuth,
76+
})
77+
78+
repoOwner := repo.GetOwner().GetLogin()
79+
repoName := repo.GetName()
80+
author := event.GetComment().GetUser().GetLogin()
81+
body := event.GetComment().GetBody()
82+
83+
if strings.HasSuffix(author, "[bot]") {
84+
logger.Debug().Msg("Issue comment was created by a bot")
85+
return nil
86+
}
87+
88+
logger.Debug().Msgf("Echoing comment on %s/%s#%d by %s", repoOwner, repoName, prNum, author)
89+
msg := fmt.Sprintf("%s\n%s said\n```\n%s\n```\n", h.preamble, author, body)
90+
prComment := github.IssueComment{
91+
Body: &msg,
92+
}
93+
94+
if _, _, err := client.Issues.CreateComment(ctx, repoOwner, repoName, prNum, &prComment); err != nil {
95+
logger.Error().Err(err).Msg("Failed to comment on pull request")
96+
}
97+
98+
return nil
99+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.13
55
require (
66
github.com/alexedwards/scs v1.4.1
77
github.com/bradleyfalzon/ghinstallation v1.1.1
8+
github.com/go-git/go-git/v5 v5.3.0 // indirect
89
github.com/google/go-github/v29 v29.0.3 // indirect
910
github.com/google/go-github/v33 v33.0.0
1011
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
@@ -17,5 +18,5 @@ require (
1718
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
1819
goji.io v2.0.2+incompatible
1920
golang.org/x/oauth2 v0.0.0-20210113205817-d3ed898aa8a3
20-
gopkg.in/yaml.v2 v2.2.8
21+
gopkg.in/yaml.v2 v2.3.0
2122
)

0 commit comments

Comments
 (0)