-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler_push.go
More file actions
80 lines (73 loc) · 2.3 KB
/
handler_push.go
File metadata and controls
80 lines (73 loc) · 2.3 KB
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
package handler
import (
"fmt"
"strings"
)
func preparePushData(data map[string]any, payload map[string]any) {
data["ref"] = payload["ref"]
data["compare_url"] = payload["compare"]
if commits, ok := payload["commits"].([]any); ok {
data["commits_count"] = len(commits)
data["commits"] = commits
var msgs []string
var authors []string
var authorsWithLinks []string
for _, c := range commits {
if cm, ok := c.(map[string]any); ok {
if m, ok := cm["message"].(string); ok {
msgs = append(msgs, m)
}
if author, ok := cm["author"].(map[string]any); ok {
if name, ok := author["name"].(string); ok {
authors = append(authors, name)
if uname, ok := author["username"].(string); ok && uname != "" {
authorsWithLinks = append(authorsWithLinks, fmt.Sprintf("[%s](https://github.com/%s)", name, uname))
} else {
authorsWithLinks = append(authorsWithLinks, name)
}
}
}
}
}
if len(msgs) > 0 {
data["commit_messages"] = msgs
// always prefix each commit message with a dash for consistent list formatting
joined := ""
for i, m := range msgs {
if i == 0 {
joined = "- " + m
} else {
joined = joined + "\n- " + m
}
}
data["commit_messages_joined"] = joined
// keep raw first commit message available separately
data["commit_message"] = msgs[0]
}
if len(authors) > 0 {
data["commit_authors"] = authors
data["commit_authors_joined"] = strings.Join(authors, ", ")
if len(authorsWithLinks) > 0 {
data["commit_authors_with_links"] = authorsWithLinks
data["commit_authors_with_links_joined"] = strings.Join(authorsWithLinks, ", ")
}
}
}
if pusher, ok := payload["pusher"].(map[string]any); ok {
data["pusher"] = pusher
if pname, ok := pusher["name"].(string); ok {
data["pusher_link_md"] = fmt.Sprintf("[%s](https://github.com/%s)", pname, pname)
}
}
data["forced"] = payload["forced"]
if ref, ok := payload["ref"].(string); ok {
branch := strings.TrimPrefix(ref, "refs/heads/")
data["branch_name"] = branch
if repo, ok := payload["repository"].(map[string]any); ok {
if url, ok2 := repo["html_url"].(string); ok2 {
data["branch_url"] = fmt.Sprintf("%s/tree/%s", url, branch)
data["branch_link_md"] = fmt.Sprintf("[%s](%s/tree/%s)", branch, url, branch)
}
}
}
}