1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
checkConfig()
hook := Webhook{}
hook.Target = ""
hook.Join = os.Getenv("JOIN")
hook.Message = fmt.Sprintf("Update git repo '%s' to '%s' by %s. | Commit message: '%s' | Url: %s | Date: %s", os.Getenv("GL_REPO"), os.Getenv("REFNAME"), os.Getenv("GL_USER"), os.Getenv("COMMIT_MSG"), os.Getenv("REPO_URL"), os.Getenv("DATE"))
json, err := json.Marshal(hook)
if err != nil {
log.Fatal("Error encoding JSON.", err)
}
request, err := http.NewRequest("POST", os.Getenv("WEBHOOK_URL"), bytes.NewBuffer(json))
if err != nil {
log.Fatal("Error creating new http request.", os.Getenv("WEBHOOK_URL"), err)
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
request.Header.Add("User-Agent", "Gitolite webhook")
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("Response Status:", resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
if string(body) != "" {
fmt.Println("Response Body:", string(body))
}
}
|