-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (113 loc) · 3.01 KB
/
main.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
//
// main.go
// Copyright (C) 2020 forseason <[email protected]>
//
// Distributed under terms of the MIT license.
//
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"os"
"os/exec"
)
var Configs []RepositoryConfig
func main() {
loadConfig()
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
apiv1 := r.Group("/api/v1")
{
apiv1.POST("webhook", webhook)
}
r.Run(":8000")
}
func webhook(c *gin.Context) {
message,err := c.GetRawData()
if err != nil{
fmt.Println(err.Error())
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(message))
var form WebhookForm
if c.ShouldBind(&form) != nil {
c.JSON(400, gin.H{"message": "invalid parameters"})
return
}
form.Signature=c.GetHeader("X-Hub-Signature")
found := false
for _, config := range Configs {
signature:="sha256="+HmacSha256(string(message),config.Secret)
fmt.Print(signature)
if signature == form.Signature && config.FullName == form.Repository.FullName && config.Ref == form.Ref {
cmd := exec.Command("bash", "-c", "cd "+config.Dir+" && git pull")
if stdout, err := cmd.CombinedOutput(); err != nil {
c.JSON(500, gin.H{"message": stdout})
return
}
if config.Exec != "" {
cmd = exec.Command("bash", "-c", config.Exec)
if stdout, err := cmd.CombinedOutput(); err != nil {
c.JSON(500, gin.H{"message": stdout})
return
}
}
found = true
}
}
if !found {
c.JSON(404, gin.H{"message": "repository full_name not found in config.json"})
return
}
c.JSON(200, gin.H{"status": "ok"})
}
func loadConfig() {
file, err := os.Open("config.json")
if err != nil {
panic(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
var rawConfigs []RepositoryConfig
if decoder.Decode(&rawConfigs) != nil {
fmt.Println("cannot decode config.json")
}
for _, config := range rawConfigs {
if config.Secret == "" || config.Dir == "" || config.FullName == "" || config.Ref == "" {
fmt.Println("wrong format with config.json")
}
}
Configs = rawConfigs
}
func HmacSha256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
// fmt.Println(h.Sum(nil))
//sha := hex.EncodeToString(h.Sum(nil))
// fmt.Println(sha)
return hex.EncodeToString(h.Sum(nil))
//return base64.StdEncoding.EncodeToString([]byte(nil))
}
type WebhookForm struct {
Signature string `json:"X-Hub-Signature"`
Ref string `json:"ref" binding:"required"`
Repository WebhookRepositoryForm `json:"repository" binding:"required"`
}
type WebhookRepositoryForm struct {
FullName string `json:"full_name" binding:"required"`
}
type RepositoryConfig struct {
Secret string `json:"secret" binding:"required"`
Ref string `json:"ref" binding:"required"`
Dir string `json:"dir" binding:"required"`
FullName string `json:"full_name" binding:"required"`
Exec string `json:"exec"`
}