Last active
July 17, 2018 12:08
-
-
Save zekroTJA/c794c19ef7f8cb1f018357f748fc03d8 to your computer and use it in GitHub Desktop.
Simple program which can be set into a git hook to pulish commit message as embed on discord with a channel webhook
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 ( | |
| "fmt" | |
| . "strings" | |
| "os/exec" | |
| "strconv" | |
| args "github.com/zekroTJA/argparser" | |
| "encoding/json" | |
| "net/http" | |
| "bytes" | |
| "io/ioutil" | |
| ) | |
| func main() { | |
| _, verbose := args.CheckForKey("--verbose", "-v") | |
| repo, ok := args.GetValueByKey("--repo", "-r") | |
| if !ok { | |
| fmt.Println("ERROR | Please enter a valid repository path") | |
| return | |
| } | |
| webhook, ok := args.GetValueByKey("--webhook", "-w") | |
| if !ok { | |
| fmt.Println("ERROR | Please enter a valid webhook url") | |
| return | |
| } | |
| var color_s string | |
| color_s, ok = args.GetValueByKey("--color", "-c") | |
| if !ok { | |
| color_s = "315117" | |
| } | |
| color, err := strconv.Atoi(color_s) | |
| if err != nil { | |
| fmt.Println("ERROR | Color must be a valid integer number") | |
| return | |
| } | |
| gitlog, err := exec.Command("git", "-C", repo, "log", "-1").Output() | |
| check(err) | |
| gitlogsplit := Split(string(gitlog), "\n") | |
| data := map[string]interface{} { | |
| "embeds": []interface{} { | |
| map[string]interface{} { | |
| "title": gitlogsplit[0], | |
| "description": "```\n" + Join(gitlogsplit[1:], "\n") + "\n```", | |
| "color": color, | |
| }, | |
| }, | |
| } | |
| jsondata, err := json.Marshal(data) | |
| check(err) | |
| req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsondata)) | |
| check(err) | |
| req.Header.Set("Content-Type", "application/json") | |
| client := &http.Client{} | |
| resp, err := client.Do(req) | |
| check(err) | |
| defer resp.Body.Close() | |
| if verbose { | |
| fmt.Println("response Status:", resp.Status) | |
| fmt.Println("response Headers:", resp.Header) | |
| body, _ := ioutil.ReadAll(resp.Body) | |
| fmt.Println("response Body:", string(body)) | |
| } | |
| } | |
| func check(err error) { | |
| if err != nil { | |
| panic(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment