Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add installation hook #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 93 additions & 20 deletions 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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be removed?

// 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about removal events? (are they correctly ignored?)

This code should be a function and tested 👍

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 @@ -60,31 +80,47 @@ 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"
}
var msgs []string

if msg != "" {
msg += fmt.Sprintf(" for %s.\n#%s\n%s", *o.Repo.FullName, *o.Repo.Language, *o.Issue.HTMLURL)
twitterClient.Tweet(msg)
}
switch o := e.(type) {
case *github.IssuesEvent:
msgs = handleIssuesEvent(o)
case *github.InstallationEvent:
msgs = handleInstallationEvent(o)
case *github.InstallationRepositoriesEvent:
msgs = handleInstallationRepositoriesEvent(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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if !goodFirstIssue(o.Issue.Labels) {
return nil
}

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 {
Expand All @@ -102,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
}