Last active
September 26, 2017 08:10
-
-
Save shixzie/94f3126841612b0092404d11b7636d50 to your computer and use it in GitHub Desktop.
Travis webhooks for discord!!
This file contains 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" | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
"github.com/Shixzie/travis" | |
) | |
type err struct { | |
Message string `json:"message,omitempty"` | |
} | |
type discordPayload struct { | |
Username string `json:"username,omitempty"` | |
AvatarURL string `json:"avatar_url,omitempty"` | |
Embeds []embed `json:"embeds,omitempty"` | |
} | |
type embed struct { | |
Color int `json:"color,omitempty"` | |
Author *author `json:"author,omitempty"` | |
Title string `json:"title,omitempty"` | |
URL string `json:"url,omitempty"` | |
Description string `json:"description,omitempty"` | |
Timestamp time.Time `json:"timestamp,omitempty"` | |
} | |
type author struct { | |
Name string `json:"name,omitempty"` | |
URL string `json:"url,omitempty"` | |
} | |
func main() { | |
addr := os.Getenv("ADDRESS") | |
port := os.Getenv("PORT") | |
durl := os.Getenv("DISCORD_URL") | |
if addr == "" { | |
flag.StringVar(&addr, "a", "", "Sets the address to lsiten to") | |
} | |
if port == "" { | |
flag.StringVar(&port, "p", "8080", "Sets the listening port") | |
} | |
if durl == "" { | |
flag.StringVar(&durl, "d", "", "The discord webhook url") | |
} | |
flag.Parse() | |
if port == "" { | |
port = "8080" | |
} | |
if durl == "" { | |
log.Fatal("missing discord webhook url") | |
} | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
payload, e := travis.GetPayloadFromRequest(r) | |
if e != nil { | |
w.WriteHeader(http.StatusBadRequest) | |
log.Println(e) | |
json.NewEncoder(w).Encode(err{Message: e.Error()}) | |
return | |
} | |
w.Header().Set("Content-Type", "application/json") | |
color := travis.Passed | |
tim := payload.FinishedAt | |
switch { | |
case payload.Pending(): | |
color = travis.InProgress | |
tim = payload.StartedAt | |
case payload.Broken(): | |
color = travis.Fail | |
case payload.Failed(): | |
color = travis.Fail | |
case payload.StillFailing(): | |
color = travis.Fail | |
case payload.Canceled(): | |
color = travis.Cancel | |
} | |
resultString := payload.ResultMessage | |
if resultString == "" { | |
resultString = payload.StatusMessage | |
} | |
commitURL := fmt.Sprintf("https://github.com/%s/%s/commit/%s", payload.Repository.OwnerName, payload.Repository.Name, payload.Commit) | |
dPayload := &discordPayload{ | |
Username: "TravisCI", | |
AvatarURL: "http://i.imgur.com/kOfUGNS.png", | |
Embeds: []embed{ | |
embed{ | |
Color: int(color), | |
Author: &author{ | |
Name: fmt.Sprintf("Build #%s %s - %s", payload.Number, resultString, payload.AuthorName), | |
URL: payload.BuildURL, | |
}, | |
Title: fmt.Sprintf("[%s:%s]", payload.Repository.Name, payload.Branch), | |
URL: payload.Repository.URL, | |
Description: fmt.Sprintf("[`%s`](%s) %s", payload.Commit[:8], commitURL, payload.Message), | |
Timestamp: tim, | |
}, | |
}, | |
} | |
buf := bytes.NewBuffer([]byte{}) | |
e = json.NewEncoder(buf).Encode(dPayload) | |
if e != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
log.Println(e) | |
json.NewEncoder(w).Encode(err{Message: e.Error()}) | |
return | |
} | |
req, e := http.NewRequest("POST", durl, buf) | |
if e != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
log.Println(e) | |
json.NewEncoder(w).Encode(err{Message: e.Error()}) | |
return | |
} | |
req.Header.Set("Content-Type", "application/json") | |
_, e = http.DefaultClient.Do(req) | |
if e != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
log.Println(e) | |
json.NewEncoder(w).Encode(err{Message: e.Error()}) | |
return | |
} | |
}) | |
http.ListenAndServe(addr+":"+port, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment