Skip to content

Commit 1d67412

Browse files
committed
Example of using TokenSource
1 parent 1e9f88c commit 1d67412

File tree

3 files changed

+166
-1
lines changed

3 files changed

+166
-1
lines changed

example/pr_review.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

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)