Browse Source

More GitHub webhook work

tags/v0.3.0
Russ Garrett 5 years ago
parent
commit
bc860f7941
No account linked to committer's email address
4 changed files with 59 additions and 17 deletions
  1. 42
    11
      README.md
  2. 3
    1
      examples/irccat.json
  3. 9
    3
      httplistener/github.go
  4. 5
    2
      httplistener/httplistener.go

+ 42
- 11
README.md View File

@@ -39,22 +39,53 @@ IRC formatting is supported (see a full [list of codes](tcplistener/colours.go#L
39 39
     echo "Status is%GREEN OK %NORMAL" | nc irccat-host 12345
40 40
 
41 41
 ## HTTP → IRC
42
-There's a similar HTTP endpoint for sending messages. You can use curl in lieu
43
-of netcat, with "-d @-" to read POST data from stdin, like so:
44
-
45
-    echo "Hello world" | curl -d @- http://irccat-host/send
46 42
 
47
-Everything that works via netcat also works by POST to /send.
43
+HTTP listeners are configured by setting keys under `http.listeners`.
48 44
 
49
-There are also endpoints which support app-specific webhooks, currently:
45
+### Generic HTTP Endpoint
46
+```json
47
+	"generic": true
48
+```
50 49
 
51
-* Grafana alerts can be sent to `/grafana`. They will be sent to the
52
-  channel defined in `http.listeners.grafana`.
50
+An endpoint for sending messages similar to the TCP port. You can use curl in lieu
51
+of netcat, with "-d @-" to read POST data from stdin, like so:
53 52
 
54
-More HTTP listeners welcome!
53
+    echo "Hello world" | curl -d @- http://irccat-host/send
55 54
 
56
-Note that there is (currently) no authentication on the HTTP endpoints,
57
-so you should make sure you firewall them from the world.
55
+Everything that works via netcat also works by POST to /send. Note that this endpoint
56
+is unauthenticated.
57
+
58
+### Grafana Webhook
59
+```json
60
+	"grafana": "#channel"
61
+```
62
+
63
+Grafana alerts can be sent to `/grafana`. They will be sent to the
64
+channel defined in `http.listeners.grafana`. Note that this endpoint is currently
65
+unauthenticated.
66
+
67
+### GitHub Webhooks
68
+```json
69
+	"github": {
70
+		"secret": "my_secret",
71
+		"default_channel": "#channel",
72
+		"repositories": {
73
+		    "irccat": "#irccat-dev"
74
+		}
75
+       	}
76
+```
77
+
78
+Receives GitHub webhooks at `/github`. Currently supports issues, issue comments,
79
+pull requests, pushes, and releases. The webhook needs to be configured to post data
80
+as JSON, not as form-encoded.
81
+
82
+The destination channel for notifications from each respository is set in
83
+`http.listeners.github.repositories.repo_name`, where `repo_name` is the name of the
84
+repository, lowercased.
85
+
86
+If `http.listeners.github.default_channel` is set, received notifications will be
87
+sent to this channel unless overriden in `http.listeners.github.repositories`. Otherwise,
88
+unrecognised repositories will be ignored.
58 89
 
59 90
 ## IRC → Shell
60 91
 You can use irccat to execute commands from IRC:

+ 3
- 1
examples/irccat.json View File

@@ -8,9 +8,11 @@
8 8
     "tls_key": "",
9 9
     "tls_cert": "",
10 10
     "listeners": {
11
+      "generic": true,
11 12
       "grafana": "#channel",
12 13
       "github": {
13
-	"secret": "test",
14
+	"secret": "my_secret",
15
+	"default_channel": "#irccat-dev",
14 16
 	"repositories": {
15 17
 	    "irccat": "#irccat-dev"
16 18
 	}

+ 9
- 3
httplistener/github.go View File

@@ -21,7 +21,9 @@ func (hl *HTTPListener) githubHandler(w http.ResponseWriter, request *http.Reque
21 21
 		http.NotFound(w, request)
22 22
 		return
23 23
 	}
24
-	hook, err := github.New() // TODO: webhook secret
24
+
25
+	hook, err := github.New(github.Options.Secret(viper.GetString("http.listeners.github.secret")))
26
+
25 27
 	if err != nil {
26 28
 		return
27 29
 	}
@@ -83,12 +85,16 @@ func (hl *HTTPListener) githubHandler(w http.ResponseWriter, request *http.Reque
83 85
 
84 86
 	if send {
85 87
 		repo = strings.ToLower(repo)
86
-		channelKey := fmt.Sprintf("http.listeners.github.repositories.%s", repo)
87
-		channel := viper.GetString(channelKey)
88
+		channel := viper.GetString(fmt.Sprintf("http.listeners.github.repositories.%s", repo))
89
+		if channel == "" {
90
+			channel = viper.GetString("http.listeners.github.default_channel")
91
+		}
92
+
88 93
 		if channel == "" {
89 94
 			log.Infof("%s GitHub event for unrecognised repository %s", request.RemoteAddr, repo)
90 95
 			return
91 96
 		}
97
+
92 98
 		log.Infof("%s [%s -> %s] GitHub event received", request.RemoteAddr, repo, channel)
93 99
 		for _, msg := range msgs {
94 100
 			hl.irc.Privmsgf(channel, msg)

+ 5
- 2
httplistener/httplistener.go View File

@@ -21,10 +21,14 @@ func New(irc *irc.Connection) (*HTTPListener, error) {
21 21
 	hl.irc = irc
22 22
 	hl.http = http.Server{Addr: viper.GetString("http.listen")}
23 23
 	hl.tpls = parseTemplates()
24
+	log.Infof("Listening for HTTP requests on %s", viper.GetString("http.listen"))
24 25
 
25 26
 	mux := http.NewServeMux()
26 27
 
27
-	mux.HandleFunc("/send", hl.genericHandler)
28
+	if viper.GetBool("http.listeners.generic") {
29
+		log.Infof("Listening for HTTP POST requests at /send")
30
+		mux.HandleFunc("/send", hl.genericHandler)
31
+	}
28 32
 
29 33
 	if viper.IsSet("http.listeners.grafana") {
30 34
 		log.Infof("Listening for Grafana webhooks at /grafana")
@@ -37,7 +41,6 @@ func New(irc *irc.Connection) (*HTTPListener, error) {
37 41
 	}
38 42
 
39 43
 	hl.http.Handler = mux
40
-	log.Infof("Listening for HTTP requests on %s", viper.GetString("http.listen"))
41 44
 	if viper.GetBool("http.tls") {
42 45
 		go hl.http.ListenAndServeTLS(viper.GetString("http.tls_cert"), viper.GetString("http.tls_key"))
43 46
 	} else {

Loading…
Cancel
Save