forked from muesli/ircflu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
99 lines (82 loc) · 2.45 KB
/
gitlab.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
// A GitLab web-hook sending messages when new commits arrive.
package gitlab
import (
"encoding/json"
"fmt"
"github.com/hoisie/web"
"github.com/muesli/ircflu/msgsystem"
"github.com/muesli/ircflu/msgsystem/irc/irctools"
"github.com/muesli/ircflu/msgsystem/web/hooks"
"io/ioutil"
"strconv"
"strings"
)
var ()
type GitLabHook struct {
name string
path string
messages chan msgsystem.Message
}
func init() {
hooks.RegisterWebHook(&GitLabHook{name: "GitLab", path: "/gitlab"})
}
func (hook *GitLabHook) Name() string {
return hook.name
}
func (hook *GitLabHook) Path() string {
return hook.path
}
func (hook *GitLabHook) SetMessageChan(channel chan msgsystem.Message) {
hook.messages = channel
}
func (hook *GitLabHook) Request(ctx *web.Context) {
b, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Params:", string(b))
var payload interface{}
err = json.Unmarshal(b, &payload)
if err != nil {
fmt.Println("Error:", err)
return
}
data := payload.(map[string]interface{})
before := data["before"].(string)
after := data["after"].(string)
ref := data["ref"].(string)
ref = ref[strings.LastIndex(ref, "/")+1:]
user := data["user_name"].(string)
commitData := data["commits"].([]interface{})
commitCount := int(data["total_commits_count"].(float64))
repoData := data["repository"].(map[string]interface{})
repo := repoData["name"].(string)
url := repoData["homepage"].(string) + "/compare/" + before[:8] + "..." + after[:8]
commitToken := "commits"
if commitCount == 1 {
commitToken = "commit"
}
ircmsg := msgsystem.Message{
Msg: fmt.Sprintf("[%s] %s pushed %s new %s to %s: %s", irctools.Colored(repo, "lightblue"), irctools.Colored(user, "teal"), irctools.Bold(strconv.Itoa(commitCount)), commitToken, irctools.Colored(ref, "purple"), url),
}
hook.messages <- ircmsg
for _, c := range commitData {
commit := c.(map[string]interface{})
commitId := commit["id"].(string)
if commitId == before {
continue
}
author := commit["author"]
authorName := ""
if author != nil {
authorMap := author.(map[string]interface{})
authorName = authorMap["name"].(string)
}
message := commit["message"].(string)
ircmsg := msgsystem.Message{
Msg: fmt.Sprintf("%s/%s %s %s: %s", irctools.Colored(repo, "lightblue"), irctools.Colored(ref, "purple"), irctools.Colored(commitId[:8], "grey"), irctools.Colored(authorName, "teal"), message),
}
hook.messages <- ircmsg
}
}