Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #49 from urcomputeringpal/count
Browse files Browse the repository at this point in the history
Use an app client to log installation events
  • Loading branch information
jnewland authored Sep 9, 2018
2 parents 29d1914 + f132c35 commit 7b1e794
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
62 changes: 38 additions & 24 deletions validator/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,43 @@ import (

// Context contains an event payload an a configured client
type Context struct {
Event interface{}
Github *github.Client
Ctx *context.Context
AppID *int
Event interface{}
Github *github.Client
Ctx *context.Context
AppID *int
AppGitHub *github.Client
}

// Process handles webhook events kinda like Probot does
func (c *Context) Process() bool {
switch e := c.Event.(type) {
case *github.InstallationEvent:
return c.ProcessInstallationEvent(c.Event.(*github.InstallationEvent))
case *github.CheckSuiteEvent:
c.ProcessCheckSuite(c.Event.(*github.CheckSuiteEvent))
return true
case *github.PullRequestEvent:
return c.ProcessPrEvent(c.Event.(*github.PullRequestEvent))
case *github.CheckRunEvent:
return c.ProcessCheckRunEvent(c.Event.(*github.CheckRunEvent))
case *github.InstallationEvent:
err := c.LogInstallationCount()
if err != nil {
log.Printf("%+v\n", err)
return false
}
return true
case *github.InstallationRepositoriesEvent:
err := c.LogInstallationCount()
if err != nil {
log.Printf("%+v\n", err)
return false
}
return true
default:
log.Printf("ignoring %s\n", reflect.TypeOf(e).String())
}
return false
}

// ProcessInstallationEvent helps determine the number of installations
func (c *Context) ProcessInstallationEvent(e *github.InstallationEvent) bool {
installations, _, err := c.Github.Apps.ListInstallations(*c.Ctx, &github.ListOptions{
PerPage: 251,
})
if err != nil {
log.Printf("%+v\n", err)
return false
}
installationCount := len(installations)
if installationCount > 250 {
log.Printf("%+v installations. get thee to the market!", installationCount)
} else {
log.Printf("%+v installations. keep it up!", installationCount)
}
return true
}

// ProcessCheckSuite validates the Kubernetes YAML that has changed on checks
// associated with PRs.
func (c *Context) ProcessCheckSuite(e *github.CheckSuiteEvent) {
Expand Down Expand Up @@ -138,3 +133,22 @@ func (c *Context) ProcessCheckRunEvent(e *github.CheckRunEvent) bool {
}
return false
}

// LogInstallationCount logs the number of installations to help keep track of
// eligibility for inclusion in the GitHub Marketplace.
// https://developer.github.com/apps/marketplace/creating-and-submitting-your-app-for-approval/requirements-for-listing-an-app-on-github-marketplace/
func (c *Context) LogInstallationCount() error {
installations, _, err := c.AppGitHub.Apps.ListInstallations(*c.Ctx, &github.ListOptions{
PerPage: 251,
})
if err != nil {
return err
}
installationCount := len(installations)
if installationCount > 250 {
log.Printf("%+v installations. get thee to the market!", installationCount)
} else {
log.Printf("%+v installations. keep it up!", installationCount)
}
return nil
}
21 changes: 12 additions & 9 deletions validator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@ func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
return
}

// 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
var installationTransport *ghinstallation.Transport
if ge.Installation != nil {
installationTransport, 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,
AppID: &s.AppID,
Github: github.NewClient(&http.Client{Transport: itr}),
Event: event,
Ctx: s.ctx,
AppID: &s.AppID,
Github: github.NewClient(&http.Client{Transport: installationTransport}),
AppGitHub: s.GitHubAppClient,
}

// TODO Return a 500 if we don't make it through the complete CheckRun cycle
Expand Down

0 comments on commit 7b1e794

Please sign in to comment.