From c3f92e99d093df37de66a56e3ffa231ea1a9a4fc Mon Sep 17 00:00:00 2001 From: okseby Date: Sat, 26 Aug 2023 16:33:04 -0400 Subject: [PATCH] Try to add a callback for all events on issues/PR's but I fail --- bot/go.mod | 8 ++++--- bot/go.sum | 2 +- bot/utils.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/bot/go.mod b/bot/go.mod index 532c32b..7f74909 100644 --- a/bot/go.mod +++ b/bot/go.mod @@ -2,18 +2,20 @@ module bot go 1.21 -require github.com/google/go-github/v54 v54.0.0 +require ( + github.com/bradleyfalzon/ghinstallation/v2 v2.6.0 + github.com/google/go-github/v54 v54.0.0 + golang.org/x/net v0.14.0 +) require ( github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 // indirect - github.com/bradleyfalzon/ghinstallation/v2 v2.6.0 // indirect github.com/cloudflare/circl v1.3.3 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-github/v53 v53.2.0 // indirect github.com/google/go-querystring v1.1.0 // indirect golang.org/x/crypto v0.12.0 // indirect - golang.org/x/net v0.14.0 // indirect golang.org/x/oauth2 v0.11.0 // indirect golang.org/x/sys v0.11.0 // indirect google.golang.org/appengine v1.6.7 // indirect diff --git a/bot/go.sum b/bot/go.sum index 32654ef..83a66b5 100644 --- a/bot/go.sum +++ b/bot/go.sum @@ -1,5 +1,4 @@ cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= -github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA= github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g= github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 h1:KLq8BE0KwCL+mmXnjLWEAOYO+2l2AE4YMmqG1ZpZHBs= github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= @@ -20,6 +19,7 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github/v53 v53.2.0 h1:wvz3FyF53v4BK+AsnvCmeNhf8AkTaeh2SoYu/XUvTtI= github.com/google/go-github/v53 v53.2.0/go.mod h1:XhFRObz+m/l+UCm9b7KSIC3lT3NWSXGt7mOsAWEloao= diff --git a/bot/utils.go b/bot/utils.go index 704abc4..9edf42d 100644 --- a/bot/utils.go +++ b/bot/utils.go @@ -10,11 +10,7 @@ import ( func initGitHubClient() { // Wrap the shared transport for use with the integration ID and authenticating with installation ID. - itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 381312, 41105280, "theopenestsource.2023-08-25.private-key.pem") - // secret = 57d9b2f565aedc5a5d658b190555ff379701a86c - - // Or for endpoints that require JWT authentication - // itr, err := ghinstallation.NewAppsTransportKeyFromFile(http.DefaultTransport, 1, "2016-10-19.private-key.pem") + itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 381312, 41105280, "theopenestsource.2023-08-26.private-key.pem") if err != nil { // Handle error. @@ -43,5 +39,58 @@ func initGitHubClient() { } func listenForWebhook() { + fmt.Println("Listening on :3333......") + http.HandleFunc("/", webhookHandler) + + err := http.ListenAndServe(":3333", nil) + + if err != nil { + panic(err) + } +} + +func webhookHandler(w http.ResponseWriter, r *http.Request) { + payload, err := github.ValidatePayload(r, nil) + if err != nil { + panic(err) + } + event, err := github.ParseWebHook(github.WebHookType(r), payload) + if err != nil { + panic(err) + } + + switch event := event.(type) { + case *github.PullRequestEvent: + processPullRequestEvent(event) + case *github.IssueCommentEvent: + processIssueCommentEvent(event) + } +} + +func processPullRequestEvent(event *github.PullRequestEvent) { + fmt.Println(event.PullRequest.Comments) +} + +func processIssueCommentEvent(event *github.IssueCommentEvent) { + fmt.Println(event) + // Wrap the shared transport for use with the integration ID and authenticating with installation ID. + itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 381312, 41105280, "theopenestsource.2023-08-26.private-key.pem") + + if err != nil { + // Handle error. + } + + // Use installation transport with client. + client := github.NewClient(&http.Client{Transport: itr}) + + ctx := context.Background() + + s := "test from bot" + + comment := github.IssueComment{ + Body: &s, + } + + client.Issues.CreateComment(ctx, event.GetRepo().GetOwner().GetName(), event.GetRepo().GetName(), event.GetIssue().GetNumber(), &comment) }