-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
171 lines (146 loc) · 3.76 KB
/
webhook.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"strings"
"github.com/robfig/cron"
)
type webhook struct {
Url string
Repo string
Mirror_repo string
Name string
Force bool
Dir string
}
func (hook *webhook) init() {
if hook.Name == "" {
parts := strings.Split(hook.Repo, "/")
hook.Name = parts[len(parts)-1]
}
if hook.Url == "" {
parts := strings.Split(hook.Name, ".git")
hook.Url = "/" + parts[0]
}
if hook.Repo == "" || hook.Mirror_repo == "" {
err := errors.New("webhook configuration must contain both repo and mirror_repo options")
log.Fatal(err)
}
go hook.createCron()
hook.createRoute()
hook.setUpRepo()
}
func (hook webhook) createCron() {
interval := envDefault("CRON", "* * 1 * * *")
if strings.ToLower(interval) == "false" {
return
}
c := cron.New()
c.AddFunc(interval, hook.mirrorRepo)
c.Start()
}
func (hook webhook) createRoute() {
http.HandleFunc(hook.Url, hook.ServeHTTP)
}
func (hook *webhook) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if verifyRequest(req) {
go hook.mirrorRepo()
fmt.Fprintf(res, hook.Repo)
} else {
http.Error(res, "400 Bad Request - Missing X-GitHub-Event Header", http.StatusBadRequest)
return
}
}
func (hook *webhook) setUpRepo() {
repoExist, _ := exists(hook.savePath())
if !repoExist {
fmt.Println("Cloning", hook.Repo)
runCmd("git", []string{"clone", "--mirror", hook.Repo}, hook.saveDir())
} else {
fmt.Println("Setting pull remote to ", hook.Mirror_repo)
runCmd("git", []string{"remote", "set-url", "origin", hook.Repo}, hook.savePath())
}
fmt.Println("Setting push remote to ", hook.Mirror_repo)
runCmd("git", []string{"remote", "set-url", "--push", "origin", hook.Mirror_repo}, hook.savePath())
hook.mirrorRepo()
}
func (hook *webhook) mirrorRepo() {
fmt.Println("Pulling", hook.Repo)
runCmd("git", []string{"fetch", "-p", "origin"}, hook.savePath())
fmt.Println("Pushing", hook.Mirror_repo)
gitPushArgs := []string{"push", "--mirror"}
if hook.Force {
gitPushArgs = append(gitPushArgs, "-f")
}
runCmd("git", gitPushArgs, hook.savePath())
}
func (hook *webhook) saveDir() (dir string) {
dir = hook.Dir
if dir == "" {
dir = envDefault("BASEDIR", "repos")
}
os.MkdirAll(dir, 0755)
return
}
func (hook *webhook) savePath() string {
return path.Join(hook.saveDir(), hook.Name)
}
func verifyRequest(req *http.Request) bool {
body, err := ioutil.ReadAll(req.Body)
handleError(err)
secret := os.Getenv("SECRET")
if secret != "" {
const signaturePrefix = "sha1="
const signatureLength = 45 // len(SignaturePrefix) + len(hex(sha1))
sig := req.Header.Get("X-Hub-Signature")
gitlabToken := req.Header.Get("X-Gitlab-Token")
if sig == "" && gitlabToken != "" {
if gitlabToken == secret {
return true
} else {
return false
}
} else if len(sig) != signatureLength || !strings.HasPrefix(sig, signaturePrefix) {
return false
}
mac := hmac.New(sha1.New, []byte(secret))
mac.Write(body)
expectedMac := mac.Sum(nil)
expectedSig := "sha1=" + hex.EncodeToString(expectedMac)
return hmac.Equal([]byte(expectedSig), []byte(sig))
}
return true
}
func printCommand(cmd *exec.Cmd) {
fmt.Printf("==> Executing: %s\n", strings.Join(cmd.Args, " "))
}
func printError(err error) {
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("==> Error: %s\n", err.Error()))
}
}
func printOutput(outs []byte) {
if len(outs) > 0 {
fmt.Printf("Git output: %s\n", string(outs))
}
}
func runCmd(cmd string, args []string, dir ...string) {
command := exec.Command(cmd, args...)
if len(dir) > 0 && dir[0] != "" {
command.Dir = dir[0]
}
output, err := command.CombinedOutput()
handleError(err)
if err != nil {
printOutput(output)
}
}