From 34da1f046041285b743d88fe9dcca6b18bfb9983 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Sat, 2 Feb 2019 11:24:00 +0530 Subject: [PATCH 1/2] move issuesEvent to function --- goodfirstissue/handler.go | 47 ++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/goodfirstissue/handler.go b/goodfirstissue/handler.go index f2b0489..9cc0756 100644 --- a/goodfirstissue/handler.go +++ b/goodfirstissue/handler.go @@ -60,31 +60,36 @@ func Handle(w http.ResponseWriter, r *http.Request) { return } - msg := "" - if o, ok := e.(*github.IssuesEvent); ok && goodFirstIssue(o.Issue.Labels) { - switch { - case *o.Action == "opened": - msg = "a new #goodfirstissue opened" - case *o.Action == "reopened": - msg = "a #goodfirstissue got reopened" - case *o.Action == "labeled": - msg = "an issue just got labeled #goodfirstissue" - case *o.Action == "unassigned": - msg = "an issue just got available for assignment #goodfirstissue" - } - - if msg != "" { - msg += fmt.Sprintf(" for %s.\n#%s\n%s", *o.Repo.FullName, *o.Repo.Language, *o.Issue.HTMLURL) - twitterClient.Tweet(msg) - } + var msgs []string + if o, ok := e.(*github.IssuesEvent); ok { + msgs = handleIssuesEvent(o) } - if msg == "" { - //comment 1 - msg = "OK" + for _, msg := range msgs { + twitterClient.Tweet(msg) } w.WriteHeader(http.StatusOK) - w.Write([]byte(msg)) + w.Write([]byte("OK")) +} + +func handleIssuesEvent(o *github.IssuesEvent) []string { + msg := "" + switch { + case *o.Action == "opened": + msg = "a new #goodfirstissue opened" + case *o.Action == "reopened": + msg = "a #goodfirstissue got reopened" + case *o.Action == "labeled": + msg = "an issue just got labeled #goodfirstissue" + case *o.Action == "unassigned": + msg = "an issue just got available for assignment #goodfirstissue" + } + + if msg != "" { + msg += fmt.Sprintf(" for %s.\n#%s\n%s", *o.Repo.FullName, *o.Repo.Language, *o.Issue.HTMLURL) + } + + return []string{msg} } func goodFirstIssue(labels []github.Label) bool { From 01470a82381b0d78f171f912ce364803e3c1bba5 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Sat, 2 Feb 2019 11:28:31 +0530 Subject: [PATCH 2/2] add handler for installation & installation_repositories event --- goodfirstissue/handler.go | 70 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/goodfirstissue/handler.go b/goodfirstissue/handler.go index 9cc0756..0b46902 100644 --- a/goodfirstissue/handler.go +++ b/goodfirstissue/handler.go @@ -1,5 +1,7 @@ package function +// package main + import ( "fmt" "io/ioutil" @@ -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 { @@ -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()) @@ -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": @@ -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 +}