Skip to content

Commit 4f31aee

Browse files
committed
initial commit
0 parents  commit 4f31aee

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed

.gitignore

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Custom
2+
3+
/log.log
4+
/config.json
5+
6+
# Created by http://www.gitignore.io
7+
8+
### Go ###
9+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
10+
*.o
11+
*.a
12+
*.so
13+
14+
# Folders
15+
_obj
16+
_test
17+
18+
# Architecture specific extensions/prefixes
19+
*.[568vq]
20+
[568vq].out
21+
22+
*.cgo1.go
23+
*.cgo2.c
24+
_cgo_defun.c
25+
_cgo_gotypes.go
26+
_cgo_export.*
27+
28+
_testmain.go
29+
30+
*.exe
31+
*.test
32+
33+
34+
### OSX ###
35+
.DS_Store
36+
.AppleDouble
37+
.LSOverride
38+
39+
# Icon must end with two \r
40+
Icon
41+
42+
43+
# Thumbnails
44+
._*
45+
46+
# Files that might appear on external disk
47+
.Spotlight-V100
48+
.Trashes
49+
50+
# Directories potentially created on remote AFP share
51+
.AppleDB
52+
.AppleDesktop
53+
Network Trash Folder
54+
Temporary Items
55+
.apdisk
56+
57+
58+
/gitlab-webhook

config.example.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"logfile": "log.log",
3+
"address": "0.0.0.0",
4+
"port": 3344,
5+
"repositories": [
6+
{
7+
"name": "foo",
8+
"commands": [
9+
"/home/bar/deploy.sh",
10+
"/home/bar/cleanup.sh"
11+
]
12+
},
13+
{
14+
"name": "foobar",
15+
"commands": [
16+
"/usr/bin/true"
17+
]
18+
}
19+
]
20+
}

gitlab-webhook.go

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package main
2+
3+
import(
4+
"net/http"
5+
"encoding/json"
6+
"io/ioutil"
7+
"os/exec"
8+
"os"
9+
"log"
10+
"errors"
11+
"strconv"
12+
)
13+
14+
//GitlabRepository represents repository information from the webhook
15+
type GitlabRepository struct {
16+
Name, Url, Description, Home string
17+
}
18+
19+
//Commit represents commit information from the webhook
20+
type Commit struct {
21+
Id, Message, Timestamp, Url string
22+
Author Author
23+
}
24+
25+
//Author represents author information from the webhook
26+
type Author struct {
27+
Name, Email string
28+
}
29+
30+
//Webhook represents push information from the webhook
31+
type Webhook struct {
32+
Before, After, Ref, User_name string
33+
User_id, Project_id int
34+
Repository GitlabRepository
35+
Commits []Commit
36+
Total_commits_count int
37+
}
38+
39+
//ConfigRepository represents a repository from the config file
40+
type ConfigRepository struct {
41+
Name string
42+
Commands []string
43+
}
44+
45+
//Config represents the config file
46+
type Config struct {
47+
Logfile string
48+
Address string
49+
Port int64
50+
Repositories []ConfigRepository
51+
}
52+
53+
func PanicIf(err error, what ...string) {
54+
if(err != nil) {
55+
if(len(what) == 0) {
56+
panic(err)
57+
}
58+
59+
panic(errors.New(err.Error() + what[0]))
60+
}
61+
}
62+
63+
var config Config
64+
65+
func main() {
66+
//load config
67+
config := loadConfig()
68+
69+
//open log file
70+
writer, err := os.OpenFile(config.Logfile, os.O_RDWR|os.O_APPEND, 0666)
71+
PanicIf(err)
72+
73+
//close logfile on exit
74+
defer func() {
75+
writer.Close()
76+
}()
77+
78+
//setting logging output
79+
log.SetOutput(writer)
80+
81+
//setting handler
82+
http.HandleFunc("/", hookHandler)
83+
84+
address := config.Address + ":" + strconv.FormatInt(config.Port, 10)
85+
86+
log.Println("Listening on " + address)
87+
88+
//starting server
89+
err = http.ListenAndServe(address, nil)
90+
if(err != nil) {
91+
log.Println(err)
92+
}
93+
}
94+
95+
func loadConfig() Config {
96+
var file, err = os.Open("config.json")
97+
PanicIf(err)
98+
99+
// close file on exit and check for its returned error
100+
defer func() {
101+
err := file.Close()
102+
PanicIf(err)
103+
}()
104+
105+
buffer := make([]byte, 1024)
106+
count := 0
107+
108+
count, err = file.Read(buffer)
109+
PanicIf(err)
110+
111+
err = json.Unmarshal(buffer[:count], &config)
112+
PanicIf(err)
113+
114+
return config
115+
}
116+
117+
func hookHandler(w http.ResponseWriter, r *http.Request) {
118+
defer func() {
119+
if r := recover(); r != nil {
120+
log.Println(r)
121+
}
122+
}()
123+
124+
var hook Webhook
125+
126+
//read request body
127+
var data, err = ioutil.ReadAll(r.Body)
128+
PanicIf(err, "while reading request")
129+
130+
//unmarshal request body
131+
err = json.Unmarshal(data, &hook)
132+
PanicIf(err, "while unmarshaling request")
133+
134+
//find matching config for repository name
135+
for _, repo := range config.Repositories {
136+
if(repo.Name != hook.Repository.Name) { continue }
137+
138+
//execute commands for repository
139+
for _, cmd := range repo.Commands {
140+
var command = exec.Command(cmd)
141+
err = command.Run()
142+
if(err != nil) {
143+
log.Println(err)
144+
} else {
145+
log.Println("Executed: " + cmd)
146+
}
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)