diff --git a/validator/context.go b/validator/context.go index 3e9e990..2b52cf4 100644 --- a/validator/context.go +++ b/validator/context.go @@ -12,17 +12,16 @@ 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 @@ -30,30 +29,26 @@ func (c *Context) Process() bool { 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) { @@ -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 +} diff --git a/validator/server.go b/validator/server.go index 59f64a5..e9c28e7 100644 --- a/validator/server.go +++ b/validator/server.go @@ -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