Skip to content

Commit

Permalink
Fix nil pointer and register webhook with json response
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Matt committed Feb 25, 2018
1 parent 39bbc8e commit 9b39fcc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 10 additions & 2 deletions frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func frontend(w http.ResponseWriter, r *http.Request) {
hook := github.Hook{
Name: &name, Events: []string{"pull_request"},
Config: map[string]interface{}{
"content_type": "json",
"url": serverDomain + "/hook",
"secret": secret,
},
Expand All @@ -80,8 +81,15 @@ func frontend(w http.ResponseWriter, r *http.Request) {
return
}

fmt.Fprintf(w, `Success :) You can undo it by simply deleting the
<a href="https://github.com/%s/settings/hooks">webhook</a>.`, repo)
fmt.Fprintf(w, `<!DOCTYPE html>
<html>
<body>
<p>
Success :) You can undo it by simply deleting the
<a href="https://github.com/%s/settings/hooks">webhook</a>.
</p>
</body>
</html>`, repo)
return
}

Expand Down
6 changes: 4 additions & 2 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ func webhook(w http.ResponseWriter, r *http.Request) {

var event github.PullRequestReviewEvent
err = json.Unmarshal(b, &event)
if err != nil {
pr := event.PullRequest
if err != nil || pr == nil {
logger.Println("Not supported event type", string(b))
fmt.Fprintf(w, `{"error":"unsupported event type"}`)
return
}
pr := event.PullRequest

// skip all events except for open PRs
if *pr.State != "open" {
logger.Println("Ignore closed pull request")
fmt.Fprintf(w, `{}`)
Expand Down

0 comments on commit 9b39fcc

Please sign in to comment.