Skip to content

Commit

Permalink
(WIP) example of using TokenSource
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanbanken committed Apr 15, 2021
1 parent 1e9f88c commit 46629a4
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
99 changes: 99 additions & 0 deletions example/pr_review.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2018 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/google/go-github/v33/github"
"github.com/palantir/go-githubapp/githubapp"

"github.com/go-git/go-git"
"github.com/go-git/go-git/storage/memory"
transport_http "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)

type PRReviewHandler struct {
githubapp.ClientCreator

preamble string
}

func (h *PRReviewHandler) Handles() []string {
return []string{"pull_request"}
}

func (h *PRReviewHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
var event github.IssueCommentEvent
if err := json.Unmarshal(payload, &event); err != nil {
return errors.Wrap(err, "failed to parse issue comment event payload")
}

if !event.GetIssue().IsPullRequest() {
zerolog.Ctx(ctx).Debug().Msg("Issue comment event is not for a pull request")
return nil
}

repo := event.GetRepo()
prNum := event.GetIssue().GetNumber()
installationID := githubapp.GetInstallationIDFromEvent(&event)

ctx, logger := githubapp.PreparePRContext(ctx, installationID, repo, event.GetIssue().GetNumber())

logger.Debug().Msgf("Event action is %s", event.GetAction())
if event.GetAction() != "created" {
return nil
}

client, ts, err := h.NewInstallationClient(installationID)
if err != nil {
return err
}

token, err := ts.Token(context.Background())
tokenAuth := &transport_http.TokenAuth{Token: token}
storer := memory.NewStorage()
gitRepo, err := git.Clone(storer, nil, &git.CloneOptions{
URL: "https://github.com/palantir/go-githubapp.git",
Auth: tokenAuth,
})

repoOwner := repo.GetOwner().GetLogin()
repoName := repo.GetName()
author := event.GetComment().GetUser().GetLogin()
body := event.GetComment().GetBody()

if strings.HasSuffix(author, "[bot]") {
logger.Debug().Msg("Issue comment was created by a bot")
return nil
}

logger.Debug().Msgf("Echoing comment on %s/%s#%d by %s", repoOwner, repoName, prNum, author)
msg := fmt.Sprintf("%s\n%s said\n```\n%s\n```\n", h.preamble, author, body)
prComment := github.IssueComment{
Body: &msg,
}

if _, _, err := client.Issues.CreateComment(ctx, repoOwner, repoName, prNum, &prComment); err != nil {
logger.Error().Err(err).Msg("Failed to comment on pull request")
}

return nil
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/alexedwards/scs v1.4.1
github.com/bradleyfalzon/ghinstallation v1.1.1
github.com/go-git/go-git/v5 v5.3.0 // indirect
github.com/google/go-github/v29 v29.0.3 // indirect
github.com/google/go-github/v33 v33.0.0
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
Expand All @@ -17,5 +18,5 @@ require (
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
goji.io v2.0.2+incompatible
golang.org/x/oauth2 v0.0.0-20210113205817-d3ed898aa8a3
gopkg.in/yaml.v2 v2.2.8
gopkg.in/yaml.v2 v2.3.0
)

0 comments on commit 46629a4

Please sign in to comment.