Last active
November 27, 2016 09:34
-
-
Save kushal/d729f64cf9c1033e0df387145c9c5832 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"strconv" | |
"strings" | |
"github.com/bmatsuo/go-jsontree" | |
) | |
func getUserMap() map[string]string { | |
m := make(map[string]string) | |
for _, pair := range strings.Split(os.Getenv("USERMAP"), ";") { | |
z := strings.Split(pair, ",") | |
m[z[0]] = z[1] | |
} | |
return m | |
} | |
func fromAssigned(request *jsontree.JsonTree) (string, string) { | |
pullRequest := request.Get("pull_request") | |
rawNumber, _ := pullRequest.Get("number").Number() | |
number := strconv.FormatInt(int64(rawNumber), 10) | |
user, _ := pullRequest.Get("user").Get("login").String() | |
fmt.Println(number) | |
fmt.Println(user) | |
assignee, _ := pullRequest.Get("assignee").Get("login").String() | |
fmt.Println(assignee) | |
return assignee, number | |
} | |
func fromComment(request *jsontree.JsonTree) (string, string) { | |
issue := request.Get("issue") | |
rawNumber, _ := issue.Get("number").Number() | |
number := strconv.FormatInt(int64(rawNumber), 10) | |
user, _ := issue.Get("user").Get("login").String() | |
fmt.Println(number) | |
fmt.Println(user) | |
assignee, _ := issue.Get("assignee").Get("login").String() | |
fmt.Println(assignee) | |
sender, _ := request.Get("sender").Get("login").String() | |
fmt.Println(sender) | |
var toNotify string | |
if sender != assignee { | |
toNotify = assignee | |
} else { | |
toNotify = user | |
} | |
return toNotify, number | |
} | |
type SlackMessage struct { | |
Channel string `json:"channel"` | |
Text string `json:"text"` | |
} | |
func sendMessage(recipient string, reviewUrl string) { | |
fmt.Println(recipient) | |
b := new(bytes.Buffer) | |
json.NewEncoder(b).Encode(SlackMessage{Channel: recipient, Text: reviewUrl}) | |
resp, err := http.Post(os.Getenv("SLACKURL"), "application/json; charset=utf-8", b) | |
defer resp.Body.Close() | |
body, _ := ioutil.ReadAll(resp.Body) | |
fmt.Println(err) | |
fmt.Println(string(body)) | |
} | |
func handler(w http.ResponseWriter, req *http.Request) { | |
defer req.Body.Close() | |
body, err := ioutil.ReadAll(req.Body) | |
fmt.Println(string(body[:])) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
request := jsontree.New() | |
err = request.UnmarshalJSON(body) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
event, _ := request.Get("action").String() | |
fmt.Println(event) | |
var toNotify string | |
var number string | |
if event == "assigned" { | |
toNotify, number = fromAssigned(request) | |
} else if event == "created" { | |
toNotify, number = fromComment(request) | |
} | |
if toNotify != "" { | |
repositoryName, _ := request.Get("repository").Get("full_name").String() | |
recipient := "@" + getUserMap()[toNotify] | |
reviewUrl := "https://reviewable.io/reviews/" + repositoryName + "/" + number | |
sendMessage(recipient, reviewUrl) | |
} | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":"+os.Getenv("PORT"), nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment