-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
161 lines (117 loc) · 3.15 KB
/
Copy pathmain.go
File metadata and controls
161 lines (117 loc) · 3.15 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
)
// Release holds data from the releases response
type Release struct {
Tag string `json:"tag_name"`
}
// ReleaseEvent holds data from the github release webhook payload
type ReleaseEvent struct {
Action string `json:"action"`
}
// Diff holds data from the compare response
type Diff struct {
Commits []struct {
Commit struct {
Message string `json:"message"`
} `json:"commit"`
} `json:"commits"`
}
// Req is the json payload sent to the webhook
type Req struct {
Content string `json:"content"`
}
func main() {
port := os.Getenv("port")
secret := os.Getenv("secret")
owner := os.Getenv("github_owner")
repo := os.Getenv("github_repo")
webhook := os.Getenv("webhook")
if port == "" || secret == "" || owner == "" || repo == "" || webhook == "" {
log.Fatal("Missing environment variables")
}
http.HandleFunc("/github/release", func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
log.Printf("error: %v", r)
}
}()
if r.Method != "POST" {
http.Error(w, "Wrong HTTP method", http.StatusBadRequest)
return
}
secrets, ok := r.URL.Query()["secret"]
if !ok || len(secrets) < 1 {
http.Error(w, "Missing secret", http.StatusUnauthorized)
return
}
if secrets[0] != secret {
http.Error(w, "Wrong secret", http.StatusForbidden)
return
}
var releaseEvent ReleaseEvent
err := json.NewDecoder(r.Body).Decode(&releaseEvent)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if releaseEvent.Action != "published" {
http.Error(w, "Action must be published", http.StatusBadRequest)
return
}
// Wait 5 seconds to make sure github's api is up to date
time.Sleep(time.Second * 5)
// Get releases
releasesRes, err := http.Get("https://api.github.com/repos/" + owner + "/" + repo + "/releases")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var releases []Release
err = json.NewDecoder(releasesRes.Body).Decode(&releases)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Compare commits
diffRes, err := http.Get("https://api.github.com/repos/" + owner + "/" + repo + "/compare/" + releases[1].Tag + "..." + releases[0].Tag)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var diff Diff
err = json.NewDecoder(diffRes.Body).Decode(&diff)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
message := "**" + releases[0].Tag + " release**\n```"
for _, c := range diff.Commits {
commit := strings.Split(c.Commit.Message, "\n")
if len(commit) > 0 {
message += "\n• " + commit[0]
}
}
message += "\n```"
req, err := json.Marshal(Req{Content: message})
if err != nil {
return
}
resp, err := http.Post(webhook, "application/json", bytes.NewBuffer(req))
if err != nil {
log.Fatal(err.Error())
}
resp.Body.Close()
fmt.Fprint(w, "Success")
})
log.Println("Listening on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}