Permalink
Browse files

continue to rewrite probot in golang

  • Loading branch information...
jnewland committed Aug 5, 2018
1 parent 8b1bd43 commit 5b470fd7505a5f54766828c8853bfef4f803b783
Showing with 93 additions and 28 deletions.
  1. +1 −1 main.go
  2. +26 −0 validator/context.go
  3. +31 −27 validator/server.go
  4. +35 −0 validator/types.go
@@ -35,7 +35,7 @@ func runWithContext(ctx context.Context) error {
return errors.New("PRIVATE_KEY_FILE required")
}

v := &validator.Validator{
v := &validator.Server{
Port: portInt,
WebhookSecret: webhookSecret,
AppID: appIDInt,
@@ -0,0 +1,26 @@
package validator

import (
"log"

"github.com/google/go-github/github"
)

// Process handles webhook events kinda like Probot does
func (c *Context) Process() {
switch e := c.event.(type) {
case *github.CheckSuiteEvent:
c.ProcessCheckSuite(c.event.(*github.CheckSuiteEvent))
return
default:
log.Printf("ignoring %s\n", e)
return
}
}

// ProcessCheckSuite validates the Kubernetes YAML that has changed on checks
// associated with PRs.
func (c *Context) ProcessCheckSuite(e *github.CheckSuiteEvent) {
log.Printf("processing %s\n", e)
return
}
@@ -2,6 +2,7 @@ package validator

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
@@ -10,37 +11,26 @@ import (
"github.com/google/go-github/github"
)

// Validator contains the logic to dispatch PRs to kubeval
type Validator struct {
Port int
WebhookSecret string
PrivateKeyFile string
AppID int
GitHubAppClient *github.Client
tr http.RoundTripper
ctx *context.Context
}

// Run starts a http server on the configured port
func (v *Validator) Run(ctx context.Context) error {
v.tr = http.DefaultTransport
func (s *Server) Run(ctx context.Context) error {
s.tr = &http.DefaultTransport

itr, err := ghinstallation.NewAppsTransportKeyFromFile(v.tr, v.AppID, v.PrivateKeyFile)
itr, err := ghinstallation.NewAppsTransportKeyFromFile(*s.tr, s.AppID, s.PrivateKeyFile)
if err != nil {
return err
}

v.ctx = &ctx
v.GitHubAppClient = github.NewClient(&http.Client{Transport: itr})
s.ctx = &ctx
s.GitHubAppClient = github.NewClient(&http.Client{Transport: itr})

http.HandleFunc("/webhook", v.handle)
http.HandleFunc("/", v.health)
http.HandleFunc("/webhook", s.handle)
http.HandleFunc("/", s.health)
log.Println("hi")
return http.ListenAndServe(fmt.Sprintf(":%d", v.Port), nil)
return http.ListenAndServe(fmt.Sprintf(":%d", s.Port), nil)
}

func (v *Validator) handle(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, []byte(v.WebhookSecret))
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, []byte(s.WebhookSecret))
if err != nil {
log.Println(err)
return
@@ -53,16 +43,30 @@ func (v *Validator) handle(w http.ResponseWriter, r *http.Request) {
return
}

switch e := event.(type) {
case *github.CheckSuiteEvent:
log.Printf("received %s\n", event)
ge := &GenericEvent{}
err = json.Unmarshal(payload, &ge)
if err != nil {
log.Println(err)
return
default:
log.Printf("ignoring %s\n", e)
}

// TODO what happens if the event doesn't have an installation ID?
itr, err := ghinstallation.NewKeyFromFile(*s.tr, s.AppID, int(ge.Installation.GetID()), s.PrivateKeyFile)
if err != nil {
log.Println(err)
return
}

c := &Context{
event: event,
ctx: s.ctx,
github: github.NewClient(&http.Client{Transport: itr}),
}

c.Process()
return
}

func (v *Validator) health(w http.ResponseWriter, r *http.Request) {
func (s *Server) health(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hi")
}
@@ -0,0 +1,35 @@
package validator

import (
"context"
"net/http"

"github.com/google/go-github/github"
)

// Server contains the logic to process webhooks, kinda like probot
type Server struct {
Port int
WebhookSecret string
PrivateKeyFile string
AppID int
GitHubAppClient *github.Client
tr *http.RoundTripper
ctx *context.Context
}

// GenericEvent contains just enough inforamation about webhook to handle
// authentication
type GenericEvent struct {
// Repo *github.Repository `json:"repository,omitempty"`
// Org *github.Organization `json:"organization,omitempty"`
// Sender *github.User `json:"sender,omitempty"`
Installation *github.Installation `json:"installation,omitempty"`
}

// Context contains an event payload an a configured client
type Context struct {
event interface{}
github *github.Client
ctx *context.Context
}

0 comments on commit 5b470fd

Please sign in to comment.