|
@@ -0,0 +1,56 @@
|
|
1
|
+package httplistener
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "bytes"
|
|
5
|
+ "encoding/json"
|
|
6
|
+ "fmt"
|
|
7
|
+ "github.com/spf13/viper"
|
|
8
|
+ "net/http"
|
|
9
|
+)
|
|
10
|
+
|
|
11
|
+type githubRepository struct {
|
|
12
|
+ Name string
|
|
13
|
+ Description string
|
|
14
|
+}
|
|
15
|
+
|
|
16
|
+type githubRelease struct {
|
|
17
|
+ Html_url string
|
|
18
|
+ Tag_name string
|
|
19
|
+ Name string
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+type githubPayload struct {
|
|
23
|
+ Action string
|
|
24
|
+ Release githubRelease
|
|
25
|
+ Repository githubRepository
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+func (hl *HTTPListener) githubReleasesHandler(w http.ResponseWriter, request *http.Request) {
|
|
29
|
+ if request.Method != "POST" {
|
|
30
|
+ http.NotFound(w, request)
|
|
31
|
+ return
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ var payload githubPayload
|
|
35
|
+ buf := new(bytes.Buffer)
|
|
36
|
+ buf.ReadFrom(request.Body)
|
|
37
|
+ json.Unmarshal(buf.Bytes(), &payload)
|
|
38
|
+
|
|
39
|
+ if payload.Action == "published" {
|
|
40
|
+ name := payload.Repository.Description
|
|
41
|
+ if name == "" {
|
|
42
|
+ name = payload.Repository.Name
|
|
43
|
+ }
|
|
44
|
+ release := payload.Release.Name
|
|
45
|
+ if release == "" {
|
|
46
|
+ release = payload.Release.Tag_name
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ msg := fmt.Sprintf("[\x02Release\x0f] \x02%s\x0f version \x02%s\x0f has been published: %s", name, release, payload.Release.Html_url)
|
|
50
|
+
|
|
51
|
+ channelKey := fmt.Sprintf("http.listeners.github-releases.%s", payload.Repository.Name)
|
|
52
|
+ channel := viper.getString(channelKey)
|
|
53
|
+ log.Infof("%s [%s] GitHub Release", request.RemoteAddr, channel)
|
|
54
|
+ hl.irc.Privmsgf(channel, msg)
|
|
55
|
+ }
|
|
56
|
+}
|