Rename internals struct.
Signed-off-by: François de Metz <francois@stormz.me> Signed-off-by: Cyril Duez <cyril@stormz.me>
This commit is contained in:
parent
6775719975
commit
a1927ea6ed
|
@ -12,26 +12,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
webhooks_models.Add("github", func(path string) webhooks_models.Webhook { return NewGithubWebhooks(path) })
|
webhooks_models.Add("github", func(path string) webhooks_models.Webhook { return NewGithubWebhook(path) })
|
||||||
}
|
}
|
||||||
|
|
||||||
type GithubWebhooks struct {
|
type GithubWebhook struct {
|
||||||
Path string
|
Path string
|
||||||
acc telegraf.Accumulator
|
acc telegraf.Accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGithubWebhooks(path string) *GithubWebhooks {
|
func NewGithubWebhook(path string) *GithubWebhook {
|
||||||
return &GithubWebhooks{Path: path}
|
return &GithubWebhook{Path: path}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gh *GithubWebhooks) Register(router *mux.Router, acc telegraf.Accumulator) {
|
func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
||||||
router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST")
|
router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST")
|
||||||
log.Printf("Started the webhooks_github on %s\n", gh.Path)
|
log.Printf("Started the webhooks_github on %s\n", gh.Path)
|
||||||
gh.acc = acc
|
gh.acc = acc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles the / route
|
func (gh *GithubWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
func (gh *GithubWebhooks) eventHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
eventType := r.Header["X-Github-Event"][0]
|
eventType := r.Header["X-Github-Event"][0]
|
||||||
data, err := ioutil.ReadAll(r.Body)
|
data, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
func GithubWebhookRequest(event string, jsonString string, t *testing.T) {
|
func GithubWebhookRequest(event string, jsonString string, t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
gh := NewGithubWebhooks("/github")
|
gh := NewGithubWebhook("/github")
|
||||||
gh.acc = &acc
|
gh.acc = &acc
|
||||||
req, _ := http.NewRequest("POST", "/github", strings.NewReader(jsonString))
|
req, _ := http.NewRequest("POST", "/github", strings.NewReader(jsonString))
|
||||||
req.Header.Add("X-Github-Event", event)
|
req.Header.Add("X-Github-Event", event)
|
||||||
|
|
|
@ -14,26 +14,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
webhooks_models.Add("rollbar", func(path string) webhooks_models.Webhook { return NewRollbarWebhooks(path) })
|
webhooks_models.Add("rollbar", func(path string) webhooks_models.Webhook { return NewRollbarWebhook(path) })
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: rename
|
type RollbarWebhook struct {
|
||||||
type RollbarWebhooks struct {
|
|
||||||
Path string
|
Path string
|
||||||
acc telegraf.Accumulator
|
acc telegraf.Accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRollbarWebhooks(path string) *RollbarWebhooks {
|
func NewRollbarWebhook(path string) *RollbarWebhook {
|
||||||
return &RollbarWebhooks{Path: path}
|
return &RollbarWebhook{Path: path}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rb *RollbarWebhooks) Register(router *mux.Router, acc telegraf.Accumulator) {
|
func (rb *RollbarWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
||||||
router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST")
|
router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST")
|
||||||
log.Printf("Started the webhooks_rollbar on %s\n", rb.Path)
|
log.Printf("Started the webhooks_rollbar on %s\n", rb.Path)
|
||||||
rb.acc = acc
|
rb.acc = acc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rb *RollbarWebhooks) eventHandler(w http.ResponseWriter, r *http.Request) {
|
func (rb *RollbarWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
data, err := ioutil.ReadAll(r.Body)
|
data, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func postWebhooks(rb *RollbarWebhooks, eventBody string) *httptest.ResponseRecorder {
|
func postWebhooks(rb *RollbarWebhook, eventBody string) *httptest.ResponseRecorder {
|
||||||
req, _ := http.NewRequest("POST", "/", strings.NewReader(eventBody))
|
req, _ := http.NewRequest("POST", "/", strings.NewReader(eventBody))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
w.Code = 500
|
w.Code = 500
|
||||||
|
@ -21,7 +21,7 @@ func postWebhooks(rb *RollbarWebhooks, eventBody string) *httptest.ResponseRecor
|
||||||
|
|
||||||
func TestNewItem(t *testing.T) {
|
func TestNewItem(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
rb := NewRollbarWebhooks("/rollbar")
|
rb := NewRollbarWebhook("/rollbar")
|
||||||
rb.acc = &acc
|
rb.acc = &acc
|
||||||
resp := postWebhooks(rb, NewItemJSON())
|
resp := postWebhooks(rb, NewItemJSON())
|
||||||
if resp.Code != http.StatusOK {
|
if resp.Code != http.StatusOK {
|
||||||
|
@ -45,7 +45,7 @@ func TestNewItem(t *testing.T) {
|
||||||
|
|
||||||
func TestDeploy(t *testing.T) {
|
func TestDeploy(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
rb := NewRollbarWebhooks("/rollbar")
|
rb := NewRollbarWebhook("/rollbar")
|
||||||
rb.acc = &acc
|
rb.acc = &acc
|
||||||
resp := postWebhooks(rb, DeployJSON())
|
resp := postWebhooks(rb, DeployJSON())
|
||||||
if resp.Code != http.StatusOK {
|
if resp.Code != http.StatusOK {
|
||||||
|
@ -66,7 +66,7 @@ func TestDeploy(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnknowItem(t *testing.T) {
|
func TestUnknowItem(t *testing.T) {
|
||||||
rb := NewRollbarWebhooks("/rollbar")
|
rb := NewRollbarWebhook("/rollbar")
|
||||||
resp := postWebhooks(rb, UnknowJSON())
|
resp := postWebhooks(rb, UnknowJSON())
|
||||||
if resp.Code != http.StatusOK {
|
if resp.Code != http.StatusOK {
|
||||||
t.Errorf("POST unknow returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
t.Errorf("POST unknow returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
||||||
|
|
Loading…
Reference in New Issue