Skip to content

Commit

Permalink
add handler for installation & installation_repositories event
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatjindal committed Feb 2, 2019
1 parent 34da1f0 commit 01470a8
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion goodfirstissue/handler.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package function

// package main

import (
"fmt"
"io/ioutil"
Expand All @@ -21,6 +23,13 @@ func init() {
twitterClient, twitterClientInitErr = twitter.NewClient()
}

// func main() {
// http.HandleFunc("/", Handle)
// if err := http.ListenAndServe(":8080", nil); err != nil {
// panic(err)
// }
// }

//Handle handles the function call
func Handle(w http.ResponseWriter, r *http.Request) {
if twitterClient == nil {
Expand Down Expand Up @@ -52,6 +61,17 @@ func Handle(w http.ResponseWriter, r *http.Request) {
}
logrus.Tracef("%s", string(body))

//per https://developer.github.com/webhooks/#events integration_installation and integration_installation_repositories events are
// deprecated and replaced by installation and installation_repositories. But github is still sending integration_installation and
// integration_installation_repositories and go-github don't understand these.

switch t {
case "integration_installation":
t = "installation"
case "integration_installation_repositories":
t = "installation_repositories"
}

e, err := github.ParseWebHook(t, body)
if err != nil {
logrus.Error("failed to parsepayload. error: ", err.Error())
Expand All @@ -61,18 +81,29 @@ func Handle(w http.ResponseWriter, r *http.Request) {
}

var msgs []string
if o, ok := e.(*github.IssuesEvent); ok {

switch o := e.(type) {
case *github.IssuesEvent:
msgs = handleIssuesEvent(o)
case *github.InstallationEvent:
msgs = handleInstallationEvent(o)
case *github.InstallationRepositoriesEvent:
msgs = handleInstallationRepositoriesEvent(o)
}

for _, msg := range msgs {
twitterClient.Tweet(msg)
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}

func handleIssuesEvent(o *github.IssuesEvent) []string {
if !goodFirstIssue(o.Issue.Labels) {
return nil
}

msg := ""
switch {
case *o.Action == "opened":
Expand Down Expand Up @@ -107,3 +138,40 @@ func goodFirstIssue(labels []github.Label) bool {

return false
}

func handleInstallationEvent(o *github.InstallationEvent) []string {
if stringValue(o.Action) != "created" {
return nil
}

msgs := []string{}

for _, r := range o.Repositories {
htmlURL := fmt.Sprintf("%s/%s", stringValue(o.Installation.Account.HTMLURL), stringValue(r.Name))
msgs = append(msgs, fmt.Sprintf("yay!! Lets welcome %s to #goodfirstissue", htmlURL))
}

return msgs
}

func handleInstallationRepositoriesEvent(o *github.InstallationRepositoriesEvent) []string {
if stringValue(o.Action) != "added" {
return nil
}

msgs := []string{}
for _, r := range o.RepositoriesAdded {
htmlURL := fmt.Sprintf("%s/%s", stringValue(o.Installation.Account.HTMLURL), stringValue(r.Name))
msgs = append(msgs, fmt.Sprintf("yay!! Lets welcome %s to #goodfirstissue", htmlURL))
}

return msgs
}

func stringValue(s *string) string {
if s == nil {
return ""
}

return *s
}

0 comments on commit 01470a8

Please sign in to comment.