Browse Source

GitHub Release webhook

tags/v0.3.0
Sam Steele 7 years ago
parent
commit
ba61397381
3 changed files with 64 additions and 0 deletions
  1. 4
    0
      examples/irccat.json
  2. 56
    0
      httplistener/github-releases.go
  3. 4
    0
      httplistener/httplistener.go

+ 4
- 0
examples/irccat.json View File

@@ -9,6 +9,10 @@
9 9
     "tls_cert": "",
10 10
     "listeners": {
11 11
       "grafana": "#channel"
12
+      "github-releases": {
13
+	    "irccat": "#irccat-dev"
14
+	    "go": "#go-nuts"
15
+	  }
12 16
     }
13 17
   },
14 18
   "irc": {

+ 56
- 0
httplistener/github-releases.go View File

@@ -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
+}

+ 4
- 0
httplistener/httplistener.go View File

@@ -27,6 +27,10 @@ func New(irc *irc.Connection) (*HTTPListener, error) {
27 27
 		mux.HandleFunc("/grafana", hl.grafanaAlertHandler)
28 28
 	}
29 29
 
30
+	if viper.IsSet("http.listeners.github-releases") {
31
+		mux.HandleFunc("/github-releases", hl.githubReleasesHandler)
32
+	}
33
+
30 34
 	hl.http.Handler = mux
31 35
 	log.Infof("Listening for HTTP requests on %s", viper.GetString("http.listen"))
32 36
 	if viper.GetBool("http.tls") {

Loading…
Cancel
Save