chore: Fix linter findings for `revive:exported` in `plugins/inputs/webhooks/*` (#16411)

This commit is contained in:
Paweł Żak 2025-01-22 18:08:13 +01:00 committed by GitHub
parent abc3a5ed10
commit 4b49d9fbb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 326 additions and 839 deletions

View File

@ -13,14 +13,15 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
) )
type ArtifactoryWebhook struct { type Webhook struct {
Path string Path string
Secret string Secret string
acc telegraf.Accumulator acc telegraf.Accumulator
log telegraf.Logger log telegraf.Logger
} }
func (awh *ArtifactoryWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) { // Register registers the webhook with the provided router
func (awh *Webhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
router.HandleFunc(awh.Path, awh.eventHandler).Methods("POST") router.HandleFunc(awh.Path, awh.eventHandler).Methods("POST")
awh.log = log awh.log = log
@ -28,7 +29,7 @@ func (awh *ArtifactoryWebhook) Register(router *mux.Router, acc telegraf.Accumul
awh.acc = acc awh.acc = acc
} }
func (awh *ArtifactoryWebhook) eventHandler(rw http.ResponseWriter, r *http.Request) { func (awh *Webhook) eventHandler(rw http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
data, err := io.ReadAll(r.Body) data, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
@ -49,13 +50,13 @@ func (awh *ArtifactoryWebhook) eventHandler(rw http.ResponseWriter, r *http.Requ
} }
et := fmt.Sprintf("%v", bodyFields["event_type"]) et := fmt.Sprintf("%v", bodyFields["event_type"])
ed := fmt.Sprintf("%v", bodyFields["domain"]) ed := fmt.Sprintf("%v", bodyFields["domain"])
ne, err := awh.NewEvent(data, et, ed) ne, err := awh.newEvent(data, et, ed)
if err != nil { if err != nil {
rw.WriteHeader(http.StatusBadRequest) rw.WriteHeader(http.StatusBadRequest)
} }
if ne != nil { if ne != nil {
nm := ne.NewMetric() nm := ne.newMetric()
awh.acc.AddFields("artifactory_webhooks", nm.Fields(), nm.Tags(), nm.Time()) awh.acc.AddFields("artifactory_webhooks", nm.Fields(), nm.Tags(), nm.Time())
} }
@ -70,7 +71,7 @@ func (e *newEventError) Error() string {
return e.s return e.s
} }
func (awh *ArtifactoryWebhook) NewEvent(data []byte, et, ed string) (event, error) { func (awh *Webhook) newEvent(data []byte, et, ed string) (event, error) {
awh.log.Debugf("New %v domain %v event received", ed, et) awh.log.Debugf("New %v domain %v event received", ed, et)
switch ed { switch ed {
case "artifact": case "artifact":

View File

@ -1,6 +1,6 @@
package artifactory package artifactory
func UnsupportedEventJSON() string { func unsupportedEventJSON() string {
return ` return `
{ {
"domain": "not_supported", "domain": "not_supported",
@ -15,7 +15,7 @@ func UnsupportedEventJSON() string {
}` }`
} }
func ArtifactDeployedEventJSON() string { func artifactDeployedEventJSON() string {
return ` return `
{ {
"domain": "artifact", "domain": "artifact",
@ -30,7 +30,7 @@ func ArtifactDeployedEventJSON() string {
}` }`
} }
func ArtifactDeletedEventJSON() string { func artifactDeletedEventJSON() string {
return ` return `
{ {
"domain": "artifact", "domain": "artifact",
@ -45,7 +45,7 @@ func ArtifactDeletedEventJSON() string {
}` }`
} }
func ArtifactMovedEventJSON() string { func artifactMovedEventJSON() string {
return ` return `
{ {
"domain": "artifact", "domain": "artifact",
@ -62,7 +62,7 @@ func ArtifactMovedEventJSON() string {
}` }`
} }
func ArtifactCopiedEventJSON() string { func artifactCopiedEventJSON() string {
return ` return `
{ {
"domain": "artifact", "domain": "artifact",
@ -79,7 +79,7 @@ func ArtifactCopiedEventJSON() string {
}` }`
} }
func ArtifactPropertiesAddedEventJSON() string { func artifactPropertiesAddedEventJSON() string {
return ` return `
{ {
"domain": "artifact_property", "domain": "artifact_property",
@ -98,7 +98,7 @@ func ArtifactPropertiesAddedEventJSON() string {
}` }`
} }
func ArtifactPropertiesDeletedEventJSON() string { func artifactPropertiesDeletedEventJSON() string {
return ` return `
{ {
"domain": "artifact_property", "domain": "artifact_property",
@ -117,7 +117,7 @@ func ArtifactPropertiesDeletedEventJSON() string {
}` }`
} }
func DockerPushedEventJSON() string { func dockerPushedEventJSON() string {
return ` return `
{ {
"domain": "docker", "domain": "docker",
@ -140,7 +140,7 @@ func DockerPushedEventJSON() string {
}` }`
} }
func DockerDeletedEventJSON() string { func dockerDeletedEventJSON() string {
return ` return `
{ {
"domain": "docker", "domain": "docker",
@ -163,7 +163,7 @@ func DockerDeletedEventJSON() string {
}` }`
} }
func DockerPromotedEventJSON() string { func dockerPromotedEventJSON() string {
return ` return `
{ {
"domain": "docker", "domain": "docker",
@ -186,7 +186,7 @@ func DockerPromotedEventJSON() string {
}` }`
} }
func BuildUploadedEventJSON() string { func buildUploadedEventJSON() string {
return ` return `
{ {
"domain": "build", "domain": "build",
@ -199,7 +199,7 @@ func BuildUploadedEventJSON() string {
}` }`
} }
func BuildDeletedEventJSON() string { func buildDeletedEventJSON() string {
return ` return `
{ {
"domain": "build", "domain": "build",
@ -212,7 +212,7 @@ func BuildDeletedEventJSON() string {
}` }`
} }
func BuildPromotedEventJSON() string { func buildPromotedEventJSON() string {
return ` return `
{ {
"domain": "build", "domain": "build",
@ -225,7 +225,7 @@ func BuildPromotedEventJSON() string {
}` }`
} }
func ReleaseBundleCreatedEventJSON() string { func releaseBundleCreatedEventJSON() string {
return ` return `
{ {
"domain": "release_bundle", "domain": "release_bundle",
@ -240,7 +240,7 @@ func ReleaseBundleCreatedEventJSON() string {
}` }`
} }
func ReleaseBundleSignedEventJSON() string { func releaseBundleSignedEventJSON() string {
return ` return `
{ {
"domain": "release_bundle", "domain": "release_bundle",
@ -255,7 +255,7 @@ func ReleaseBundleSignedEventJSON() string {
}` }`
} }
func ReleaseBundleDeletedEventJSON() string { func releaseBundleDeletedEventJSON() string {
return ` return `
{ {
"domain": "release_bundle", "domain": "release_bundle",
@ -270,7 +270,7 @@ func ReleaseBundleDeletedEventJSON() string {
}` }`
} }
func DistributionStartedEventJSON() string { func distributionStartedEventJSON() string {
return ` return `
{ {
"domain": "distribution", "domain": "distribution",
@ -297,7 +297,7 @@ func DistributionStartedEventJSON() string {
}` }`
} }
func DistributionCompletedEventJSON() string { func distributionCompletedEventJSON() string {
return ` return `
{ {
"domain": "distribution", "domain": "distribution",
@ -324,7 +324,7 @@ func DistributionCompletedEventJSON() string {
}` }`
} }
func DistributionAbortedEventJSON() string { func distributionAbortedEventJSON() string {
return ` return `
{ {
"domain": "distribution", "domain": "distribution",
@ -351,7 +351,7 @@ func DistributionAbortedEventJSON() string {
}` }`
} }
func DistributionFailedEventJSON() string { func distributionFailedEventJSON() string {
return ` return `
{ {
"domain": "distribution", "domain": "distribution",
@ -378,7 +378,7 @@ func DistributionFailedEventJSON() string {
}` }`
} }
func DestinationReceivedEventJSON() string { func destinationReceivedEventJSON() string {
return ` return `
{ {
"domain": "destination", "domain": "destination",
@ -393,7 +393,7 @@ func DestinationReceivedEventJSON() string {
}` }`
} }
func DestinationDeleteStartedEventJSON() string { func destinationDeleteStartedEventJSON() string {
return ` return `
{ {
"domain": "destination", "domain": "destination",
@ -408,7 +408,7 @@ func DestinationDeleteStartedEventJSON() string {
}` }`
} }
func DestinationDeleteCompletedEventJSON() string { func destinationDeleteCompletedEventJSON() string {
return ` return `
{ {
"domain": "destination", "domain": "destination",
@ -423,7 +423,7 @@ func DestinationDeleteCompletedEventJSON() string {
}` }`
} }
func DestinationDeleteFailedEventJSON() string { func destinationDeleteFailedEventJSON() string {
return ` return `
{ {
"domain": "destination", "domain": "destination",

View File

@ -11,7 +11,7 @@ import (
const meas = "artifactory_webhooks" const meas = "artifactory_webhooks"
type event interface { type event interface {
NewMetric() telegraf.Metric newMetric() telegraf.Metric
} }
type artifactDeploymentOrDeletedEvent struct { type artifactDeploymentOrDeletedEvent struct {
@ -26,7 +26,7 @@ type artifactDeploymentOrDeletedEvent struct {
} `json:"data"` } `json:"data"`
} }
func (e artifactDeploymentOrDeletedEvent) NewMetric() telegraf.Metric { func (e artifactDeploymentOrDeletedEvent) newMetric() telegraf.Metric {
t := map[string]string{ t := map[string]string{
"domain": e.Domain, "domain": e.Domain,
"event_type": e.Event, "event_type": e.Event,
@ -55,7 +55,7 @@ type artifactMovedOrCopiedEvent struct {
} `json:"data"` } `json:"data"`
} }
func (e artifactMovedOrCopiedEvent) NewMetric() telegraf.Metric { func (e artifactMovedOrCopiedEvent) newMetric() telegraf.Metric {
t := map[string]string{ t := map[string]string{
"domain": e.Domain, "domain": e.Domain,
"event_type": e.Event, "event_type": e.Event,
@ -85,7 +85,7 @@ type artifactPropertiesEvent struct {
} }
} }
func (e artifactPropertiesEvent) NewMetric() telegraf.Metric { func (e artifactPropertiesEvent) newMetric() telegraf.Metric {
t := map[string]string{ t := map[string]string{
"domain": e.Domain, "domain": e.Domain,
"event_type": e.Event, "event_type": e.Event,
@ -120,7 +120,7 @@ type dockerEvent struct {
} `json:"data"` } `json:"data"`
} }
func (e dockerEvent) NewMetric() telegraf.Metric { func (e dockerEvent) newMetric() telegraf.Metric {
t := map[string]string{ t := map[string]string{
"domain": e.Domain, "domain": e.Domain,
"event_type": e.Event, "event_type": e.Event,
@ -149,7 +149,7 @@ type buildEvent struct {
} `json:"data"` } `json:"data"`
} }
func (e buildEvent) NewMetric() telegraf.Metric { func (e buildEvent) newMetric() telegraf.Metric {
t := map[string]string{ t := map[string]string{
"domain": e.Domain, "domain": e.Domain,
"event_type": e.Event, "event_type": e.Event,
@ -175,7 +175,7 @@ type releaseBundleEvent struct {
JpdOrigin string `json:"jpd_origin"` JpdOrigin string `json:"jpd_origin"`
} }
func (e releaseBundleEvent) NewMetric() telegraf.Metric { func (e releaseBundleEvent) newMetric() telegraf.Metric {
t := map[string]string{ t := map[string]string{
"domain": e.Domain, "domain": e.Domain,
"event_type": e.Event, "event_type": e.Event,
@ -209,7 +209,7 @@ type distributionEvent struct {
OriginURL string `json:"jpd_origin"` OriginURL string `json:"jpd_origin"`
} }
func (e distributionEvent) NewMetric() telegraf.Metric { func (e distributionEvent) newMetric() telegraf.Metric {
t := map[string]string{ t := map[string]string{
"domain": e.Domain, "domain": e.Domain,
"event_type": e.Event, "event_type": e.Event,
@ -239,7 +239,7 @@ type destinationEvent struct {
OriginURL string `json:"jpd_origin"` OriginURL string `json:"jpd_origin"`
} }
func (e destinationEvent) NewMetric() telegraf.Metric { func (e destinationEvent) newMetric() telegraf.Metric {
t := map[string]string{ t := map[string]string{
"domain": e.Domain, "domain": e.Domain,
"event_type": e.Event, "event_type": e.Event,

View File

@ -11,9 +11,9 @@ import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
) )
func ArtifactoryWebhookRequest(t *testing.T, domain, event, jsonString string) { func artifactoryWebhookRequest(t *testing.T, domain, event, jsonString string) {
var acc testutil.Accumulator var acc testutil.Accumulator
awh := &ArtifactoryWebhook{Path: "/artifactory", acc: &acc, log: testutil.Logger{}} awh := &Webhook{Path: "/artifactory", acc: &acc, log: testutil.Logger{}}
req, err := http.NewRequest("POST", "/artifactory", strings.NewReader(jsonString)) req, err := http.NewRequest("POST", "/artifactory", strings.NewReader(jsonString))
require.NoError(t, err) require.NoError(t, err)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -23,9 +23,9 @@ func ArtifactoryWebhookRequest(t *testing.T, domain, event, jsonString string) {
} }
} }
func ArtifactoryWebhookRequestWithSignature(t *testing.T, event, jsonString, signature string, expectedStatus int) { func artifactoryWebhookRequestWithSignature(t *testing.T, event, jsonString, signature string, expectedStatus int) {
var acc testutil.Accumulator var acc testutil.Accumulator
awh := &ArtifactoryWebhook{Path: "/artifactory", acc: &acc, log: testutil.Logger{}} awh := &Webhook{Path: "/artifactory", acc: &acc, log: testutil.Logger{}}
req, err := http.NewRequest("POST", "/artifactory", strings.NewReader(jsonString)) req, err := http.NewRequest("POST", "/artifactory", strings.NewReader(jsonString))
require.NoError(t, err) require.NoError(t, err)
req.Header.Add("x-jfrog-event-auth", signature) req.Header.Add("x-jfrog-event-auth", signature)
@ -38,8 +38,8 @@ func ArtifactoryWebhookRequestWithSignature(t *testing.T, event, jsonString, sig
func TestUnsupportedEvent(t *testing.T) { func TestUnsupportedEvent(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
awh := &ArtifactoryWebhook{Path: "/artifactory", acc: &acc, log: testutil.Logger{}} awh := &Webhook{Path: "/artifactory", acc: &acc, log: testutil.Logger{}}
req, err := http.NewRequest("POST", "/artifactory", strings.NewReader(UnsupportedEventJSON())) req, err := http.NewRequest("POST", "/artifactory", strings.NewReader(unsupportedEventJSON()))
require.NoError(t, err) require.NoError(t, err)
w := httptest.NewRecorder() w := httptest.NewRecorder()
awh.eventHandler(w, req) awh.eventHandler(w, req)
@ -49,103 +49,103 @@ func TestUnsupportedEvent(t *testing.T) {
} }
func TestArtifactDeployedEvent(t *testing.T) { func TestArtifactDeployedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "artifact", "deployed", ArtifactDeployedEventJSON()) artifactoryWebhookRequest(t, "artifact", "deployed", artifactDeployedEventJSON())
} }
func TestArtifactDeleted(t *testing.T) { func TestArtifactDeleted(t *testing.T) {
ArtifactoryWebhookRequest(t, "artifact", "deleted", ArtifactDeletedEventJSON()) artifactoryWebhookRequest(t, "artifact", "deleted", artifactDeletedEventJSON())
} }
func TestArtifactMovedEvent(t *testing.T) { func TestArtifactMovedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "artifact", "moved", ArtifactMovedEventJSON()) artifactoryWebhookRequest(t, "artifact", "moved", artifactMovedEventJSON())
} }
func TestArtifactCopiedEvent(t *testing.T) { func TestArtifactCopiedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "artifact", "copied", ArtifactCopiedEventJSON()) artifactoryWebhookRequest(t, "artifact", "copied", artifactCopiedEventJSON())
} }
func TestArtifactPropertiesAddedEvent(t *testing.T) { func TestArtifactPropertiesAddedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "artifact_property", "added", ArtifactPropertiesAddedEventJSON()) artifactoryWebhookRequest(t, "artifact_property", "added", artifactPropertiesAddedEventJSON())
} }
func TestArtifactPropertiesDeletedEvent(t *testing.T) { func TestArtifactPropertiesDeletedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "artifact_property", "deleted", ArtifactPropertiesDeletedEventJSON()) artifactoryWebhookRequest(t, "artifact_property", "deleted", artifactPropertiesDeletedEventJSON())
} }
func TestDockerPushedEvent(t *testing.T) { func TestDockerPushedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "docker", "pushed", DockerPushedEventJSON()) artifactoryWebhookRequest(t, "docker", "pushed", dockerPushedEventJSON())
} }
func TestDockerDeletedEvent(t *testing.T) { func TestDockerDeletedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "docker", "deleted", DockerDeletedEventJSON()) artifactoryWebhookRequest(t, "docker", "deleted", dockerDeletedEventJSON())
} }
func TestDockerPromotedEvent(t *testing.T) { func TestDockerPromotedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "docker", "promoted", DockerPromotedEventJSON()) artifactoryWebhookRequest(t, "docker", "promoted", dockerPromotedEventJSON())
} }
func TestBuildUploadedEvent(t *testing.T) { func TestBuildUploadedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "build", "uploaded", BuildUploadedEventJSON()) artifactoryWebhookRequest(t, "build", "uploaded", buildUploadedEventJSON())
} }
func TestBuildDeletedEvent(t *testing.T) { func TestBuildDeletedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "build", "deleted", BuildDeletedEventJSON()) artifactoryWebhookRequest(t, "build", "deleted", buildDeletedEventJSON())
} }
func TestBuildPromotedEvent(t *testing.T) { func TestBuildPromotedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "build", "promoted", BuildPromotedEventJSON()) artifactoryWebhookRequest(t, "build", "promoted", buildPromotedEventJSON())
} }
func TestReleaseBundleCreatedEvent(t *testing.T) { func TestReleaseBundleCreatedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "release_bundle", "created", ReleaseBundleCreatedEventJSON()) artifactoryWebhookRequest(t, "release_bundle", "created", releaseBundleCreatedEventJSON())
} }
func TestReleaseBundleSignedEvent(t *testing.T) { func TestReleaseBundleSignedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "release_bundle", "signed", ReleaseBundleSignedEventJSON()) artifactoryWebhookRequest(t, "release_bundle", "signed", releaseBundleSignedEventJSON())
} }
func TestReleaseBundleDeletedEvent(t *testing.T) { func TestReleaseBundleDeletedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "release_bundle", "deleted", ReleaseBundleDeletedEventJSON()) artifactoryWebhookRequest(t, "release_bundle", "deleted", releaseBundleDeletedEventJSON())
} }
func TestDistributionStartedEvent(t *testing.T) { func TestDistributionStartedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "distribution", "distribute_started", DistributionStartedEventJSON()) artifactoryWebhookRequest(t, "distribution", "distribute_started", distributionStartedEventJSON())
} }
func TestDistributionCompletedEvent(t *testing.T) { func TestDistributionCompletedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "distribution", "distribute_started", DistributionCompletedEventJSON()) artifactoryWebhookRequest(t, "distribution", "distribute_started", distributionCompletedEventJSON())
} }
func TestDistributionAbortedEvent(t *testing.T) { func TestDistributionAbortedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "distribution", "distribute_aborted", DistributionAbortedEventJSON()) artifactoryWebhookRequest(t, "distribution", "distribute_aborted", distributionAbortedEventJSON())
} }
func TestDistributionFailedEvent(t *testing.T) { func TestDistributionFailedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "distribution", "distribute_failed", DistributionFailedEventJSON()) artifactoryWebhookRequest(t, "distribution", "distribute_failed", distributionFailedEventJSON())
} }
func TestDestinationReceivedEvent(t *testing.T) { func TestDestinationReceivedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "destination", "received", DestinationReceivedEventJSON()) artifactoryWebhookRequest(t, "destination", "received", destinationReceivedEventJSON())
} }
func TestDestinationDeletedStartedEvent(t *testing.T) { func TestDestinationDeletedStartedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "destination", "delete_started", DestinationDeleteStartedEventJSON()) artifactoryWebhookRequest(t, "destination", "delete_started", destinationDeleteStartedEventJSON())
} }
func TestDestinationDeletedCompletedEvent(t *testing.T) { func TestDestinationDeletedCompletedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "destination", "delete_completed", DestinationDeleteCompletedEventJSON()) artifactoryWebhookRequest(t, "destination", "delete_completed", destinationDeleteCompletedEventJSON())
} }
func TestDestinationDeleteFailedEvent(t *testing.T) { func TestDestinationDeleteFailedEvent(t *testing.T) {
ArtifactoryWebhookRequest(t, "destination", "delete_failed", DestinationDeleteFailedEventJSON()) artifactoryWebhookRequest(t, "destination", "delete_failed", destinationDeleteFailedEventJSON())
} }
func TestEventWithSignatureSuccess(t *testing.T) { func TestEventWithSignatureSuccess(t *testing.T) {
ArtifactoryWebhookRequestWithSignature( artifactoryWebhookRequestWithSignature(
t, t,
"watch", "watch",
ArtifactDeployedEventJSON(), artifactDeployedEventJSON(),
generateSignature("signature", []byte(ArtifactDeployedEventJSON())), generateSignature("signature", []byte(artifactDeployedEventJSON())),
http.StatusOK, http.StatusOK,
) )
} }

View File

@ -12,14 +12,15 @@ import (
"github.com/influxdata/telegraf/plugins/common/auth" "github.com/influxdata/telegraf/plugins/common/auth"
) )
type FilestackWebhook struct { type Webhook struct {
Path string Path string
acc telegraf.Accumulator acc telegraf.Accumulator
log telegraf.Logger log telegraf.Logger
auth.BasicAuth auth.BasicAuth
} }
func (fs *FilestackWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) { // Register registers the webhook with the provided router
func (fs *Webhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
router.HandleFunc(fs.Path, fs.eventHandler).Methods("POST") router.HandleFunc(fs.Path, fs.eventHandler).Methods("POST")
fs.log = log fs.log = log
@ -27,7 +28,7 @@ func (fs *FilestackWebhook) Register(router *mux.Router, acc telegraf.Accumulato
fs.acc = acc fs.acc = acc
} }
func (fs *FilestackWebhook) eventHandler(w http.ResponseWriter, r *http.Request) { func (fs *Webhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
if !fs.Verify(r) { if !fs.Verify(r) {
@ -41,14 +42,14 @@ func (fs *FilestackWebhook) eventHandler(w http.ResponseWriter, r *http.Request)
return return
} }
event := &FilestackEvent{} event := &filestackEvent{}
err = json.Unmarshal(body, event) err = json.Unmarshal(body, event)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
fs.acc.AddFields("filestack_webhooks", event.Fields(), event.Tags(), time.Unix(event.TimeStamp, 0)) fs.acc.AddFields("filestack_webhooks", event.fields(), event.tags(), time.Unix(event.TimeStamp, 0))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

View File

@ -2,19 +2,19 @@ package filestack
import "strconv" import "strconv"
type FilestackEvent struct { type filestackEvent struct {
Action string `json:"action"` Action string `json:"action"`
TimeStamp int64 `json:"timestamp"` TimeStamp int64 `json:"timestamp"`
ID int `json:"id"` ID int `json:"id"`
} }
func (fe *FilestackEvent) Tags() map[string]string { func (fe *filestackEvent) tags() map[string]string {
return map[string]string{ return map[string]string{
"action": fe.Action, "action": fe.Action,
} }
} }
func (fe *FilestackEvent) Fields() map[string]interface{} { func (fe *filestackEvent) fields() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"id": strconv.Itoa(fe.ID), "id": strconv.Itoa(fe.ID),
} }

View File

@ -13,7 +13,7 @@ import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
) )
func postWebhooks(t *testing.T, md *FilestackWebhook, eventBodyFile io.Reader) *httptest.ResponseRecorder { func postWebhooks(t *testing.T, md *Webhook, eventBodyFile io.Reader) *httptest.ResponseRecorder {
req, err := http.NewRequest("POST", "/filestack", eventBodyFile) req, err := http.NewRequest("POST", "/filestack", eventBodyFile)
require.NoError(t, err) require.NoError(t, err)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -25,7 +25,7 @@ func postWebhooks(t *testing.T, md *FilestackWebhook, eventBodyFile io.Reader) *
func TestDialogEvent(t *testing.T) { func TestDialogEvent(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
fs := &FilestackWebhook{Path: "/filestack", acc: &acc} fs := &Webhook{Path: "/filestack", acc: &acc}
resp := postWebhooks(t, fs, getFile(t, "testdata/dialog_open.json")) resp := postWebhooks(t, fs, getFile(t, "testdata/dialog_open.json"))
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
@ -43,7 +43,7 @@ func TestDialogEvent(t *testing.T) {
} }
func TestParseError(t *testing.T) { func TestParseError(t *testing.T) {
fs := &FilestackWebhook{Path: "/filestack"} fs := &Webhook{Path: "/filestack"}
resp := postWebhooks(t, fs, strings.NewReader("")) resp := postWebhooks(t, fs, strings.NewReader(""))
if resp.Code != http.StatusBadRequest { if resp.Code != http.StatusBadRequest {
t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusBadRequest) t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusBadRequest)
@ -52,7 +52,7 @@ func TestParseError(t *testing.T) {
func TestUploadEvent(t *testing.T) { func TestUploadEvent(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
fs := &FilestackWebhook{Path: "/filestack", acc: &acc} fs := &Webhook{Path: "/filestack", acc: &acc}
resp := postWebhooks(t, fs, getFile(t, "testdata/upload.json")) resp := postWebhooks(t, fs, getFile(t, "testdata/upload.json"))
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
@ -71,7 +71,7 @@ func TestUploadEvent(t *testing.T) {
func TestVideoConversionEvent(t *testing.T) { func TestVideoConversionEvent(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
fs := &FilestackWebhook{Path: "/filestack", acc: &acc} fs := &Webhook{Path: "/filestack", acc: &acc}
resp := postWebhooks(t, fs, getFile(t, "testdata/video_conversion.json")) resp := postWebhooks(t, fs, getFile(t, "testdata/video_conversion.json"))
if resp.Code != http.StatusBadRequest { if resp.Code != http.StatusBadRequest {
t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusBadRequest) t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusBadRequest)

View File

@ -14,15 +14,16 @@ import (
"github.com/influxdata/telegraf/plugins/common/auth" "github.com/influxdata/telegraf/plugins/common/auth"
) )
type GithubWebhook struct { type Webhook struct {
Path string Path string
Secret string secret string
acc telegraf.Accumulator acc telegraf.Accumulator
log telegraf.Logger log telegraf.Logger
auth.BasicAuth auth.BasicAuth
} }
func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) { // Register registers the webhook with the provided router
func (gh *Webhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST") router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST")
gh.log = log gh.log = log
@ -30,7 +31,7 @@ func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator,
gh.acc = acc gh.acc = acc
} }
func (gh *GithubWebhook) eventHandler(w http.ResponseWriter, r *http.Request) { func (gh *Webhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
if !gh.Verify(r) { if !gh.Verify(r) {
@ -45,19 +46,19 @@ func (gh *GithubWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if gh.Secret != "" && !checkSignature(gh.Secret, data, r.Header.Get("X-Hub-Signature")) { if gh.secret != "" && !checkSignature(gh.secret, data, r.Header.Get("X-Hub-Signature")) {
gh.log.Error("Fail to check the github webhook signature") gh.log.Error("Fail to check the github webhook signature")
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
e, err := gh.NewEvent(data, eventType) e, err := gh.newEvent(data, eventType)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
if e != nil { if e != nil {
p := e.NewMetric() p := e.newMetric()
gh.acc.AddFields("github_webhooks", p.Fields(), p.Tags(), p.Time()) gh.acc.AddFields("github_webhooks", p.Fields(), p.Tags(), p.Time())
} }
@ -80,7 +81,7 @@ func (e *newEventError) Error() string {
return e.s return e.s
} }
func (gh *GithubWebhook) NewEvent(data []byte, name string) (event, error) { func (gh *Webhook) newEvent(data []byte, name string) (event, error) {
gh.log.Debugf("New %v event received", name) gh.log.Debugf("New %v event received", name)
switch name { switch name {
case "commit_comment": case "commit_comment":

View File

@ -1,6 +1,6 @@
package github package github
func CommitCommentEventJSON() string { func commitCommentEventJSON() string {
return `{ return `{
"action": "created", "action": "created",
"comment": { "comment": {
@ -143,123 +143,7 @@ func CommitCommentEventJSON() string {
}` }`
} }
func CreateEventJSON() string { func deleteEventJSON() string {
return `{
"ref": "0.0.1",
"ref_type": "tag",
"master_branch": "master",
"description": "",
"pusher_type": "user",
"repository": {
"id": 35129377,
"name": "public-repo",
"full_name": "baxterthehacker/public-repo",
"owner": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/baxterthehacker/public-repo",
"description": "",
"fork": false,
"url": "https://api.github.com/repos/baxterthehacker/public-repo",
"forks_url": "https://api.github.com/repos/baxterthehacker/public-repo/forks",
"keys_url": "https://api.github.com/repos/baxterthehacker/public-repo/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/baxterthehacker/public-repo/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/baxterthehacker/public-repo/teams",
"hooks_url": "https://api.github.com/repos/baxterthehacker/public-repo/hooks",
"issue_events_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/events{/number}",
"events_url": "https://api.github.com/repos/baxterthehacker/public-repo/events",
"assignees_url": "https://api.github.com/repos/baxterthehacker/public-repo/assignees{/user}",
"branches_url": "https://api.github.com/repos/baxterthehacker/public-repo/branches{/branch}",
"tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/tags",
"blobs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/{sha}",
"languages_url": "https://api.github.com/repos/baxterthehacker/public-repo/languages",
"stargazers_url": "https://api.github.com/repos/baxterthehacker/public-repo/stargazers",
"contributors_url": "https://api.github.com/repos/baxterthehacker/public-repo/contributors",
"subscribers_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscribers",
"subscription_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscription",
"commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/baxterthehacker/public-repo/contents/{+path}",
"compare_url": "https://api.github.com/repos/baxterthehacker/public-repo/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/baxterthehacker/public-repo/merges",
"archive_url": "https://api.github.com/repos/baxterthehacker/public-repo/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/baxterthehacker/public-repo/downloads",
"issues_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues{/number}",
"pulls_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}",
"milestones_url": "https://api.github.com/repos/baxterthehacker/public-repo/milestones{/number}",
"notifications_url": "https://api.github.com/repos/baxterthehacker/public-repo/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/baxterthehacker/public-repo/labels{/name}",
"releases_url": "https://api.github.com/repos/baxterthehacker/public-repo/releases{/id}",
"created_at": "2015-05-05T23:40:12Z",
"updated_at": "2015-05-05T23:40:30Z",
"pushed_at": "2015-05-05T23:40:38Z",
"git_url": "git://github.com/baxterthehacker/public-repo.git",
"ssh_url": "git@github.com:baxterthehacker/public-repo.git",
"clone_url": "https://github.com/baxterthehacker/public-repo.git",
"svn_url": "https://github.com/baxterthehacker/public-repo",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 0,
"mirror_url": null,
"open_issues_count": 2,
"forks": 0,
"open_issues": 2,
"watchers": 0,
"default_branch": "master"
},
"sender": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
}
}`
}
func DeleteEventJSON() string {
return `{ return `{
"ref": "simple-tag", "ref": "simple-tag",
"ref_type": "tag", "ref_type": "tag",
@ -373,7 +257,7 @@ func DeleteEventJSON() string {
}` }`
} }
func DeploymentEventJSON() string { func deploymentEventJSON() string {
return `{ return `{
"deployment": { "deployment": {
"url": "https://api.github.com/repos/baxterthehacker/public-repo/deployments/710692", "url": "https://api.github.com/repos/baxterthehacker/public-repo/deployments/710692",
@ -518,7 +402,7 @@ func DeploymentEventJSON() string {
}` }`
} }
func DeploymentStatusEventJSON() string { func deploymentStatusEventJSON() string {
return `{ return `{
"deployment": { "deployment": {
"url": "https://api.github.com/repos/baxterthehacker/public-repo/deployments/710692", "url": "https://api.github.com/repos/baxterthehacker/public-repo/deployments/710692",
@ -694,7 +578,7 @@ func DeploymentStatusEventJSON() string {
` `
} }
func ForkEventJSON() string { func forkEventJSON() string {
return `{ return `{
"forkee": { "forkee": {
"id": 35129393, "id": 35129393,
@ -893,7 +777,7 @@ func ForkEventJSON() string {
}` }`
} }
func GollumEventJSON() string { func gollumEventJSON() string {
return `{ return `{
"pages": [ "pages": [
{ {
@ -1014,7 +898,7 @@ func GollumEventJSON() string {
}` }`
} }
func IssueCommentEventJSON() string { func issueCommentEventJSON() string {
return `{ return `{
"action": "created", "action": "created",
"issue": { "issue": {
@ -1199,7 +1083,7 @@ func IssueCommentEventJSON() string {
}` }`
} }
func IssuesEventJSON() string { func issuesEventJSON() string {
return `{ return `{
"action": "opened", "action": "opened",
"issue": { "issue": {
@ -1356,7 +1240,7 @@ func IssuesEventJSON() string {
}` }`
} }
func MemberEventJSON() string { func memberEventJSON() string {
return `{ return `{
"action": "added", "action": "added",
"member": { "member": {
@ -1487,7 +1371,7 @@ func MemberEventJSON() string {
}` }`
} }
func MembershipEventJSON() string { func membershipEventJSON() string {
return `{ return `{
"action": "added", "action": "added",
"scope": "team", "scope": "team",
@ -1551,7 +1435,7 @@ func MembershipEventJSON() string {
}` }`
} }
func PageBuildEventJSON() string { func pageBuildEventJSON() string {
return `{ return `{
"id": 15995382, "id": 15995382,
"build": { "build": {
@ -1693,7 +1577,7 @@ func PageBuildEventJSON() string {
}` }`
} }
func PublicEventJSON() string { func publicEventJSON() string {
return `{ return `{
"repository": { "repository": {
"id": 35129377, "id": 35129377,
@ -1804,7 +1688,7 @@ func PublicEventJSON() string {
}` }`
} }
func PullRequestReviewCommentEventJSON() string { func pullRequestReviewCommentEventJSON() string {
return `{ return `{
"action": "created", "action": "created",
"comment": { "comment": {
@ -2253,422 +2137,7 @@ func PullRequestReviewCommentEventJSON() string {
}` }`
} }
func PullRequestEventJSON() string { func pushEventJSON() string {
return `{
"action": "opened",
"number": 1,
"pull_request": {
"url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/1",
"id": 34778301,
"html_url": "https://github.com/baxterthehacker/public-repo/pull/1",
"diff_url": "https://github.com/baxterthehacker/public-repo/pull/1.diff",
"patch_url": "https://github.com/baxterthehacker/public-repo/pull/1.patch",
"issue_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/1",
"number": 1,
"state": "open",
"locked": false,
"title": "Update the README with new information",
"user": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
},
"body": "This is a pretty simple change that we need to pull into master.",
"created_at": "2015-05-05T23:40:27Z",
"updated_at": "2015-05-05T23:40:27Z",
"closed_at": null,
"merged_at": null,
"merge_commit_sha": null,
"assignee": null,
"milestone": null,
"commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/1/commits",
"review_comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/1/comments",
"review_comment_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/comments{/number}",
"comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/1/comments",
"statuses_url": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
"head": {
"label": "baxterthehacker:changes",
"ref": "changes",
"sha": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
"user": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
},
"repo": {
"id": 35129377,
"name": "public-repo",
"full_name": "baxterthehacker/public-repo",
"owner": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/baxterthehacker/public-repo",
"description": "",
"fork": false,
"url": "https://api.github.com/repos/baxterthehacker/public-repo",
"forks_url": "https://api.github.com/repos/baxterthehacker/public-repo/forks",
"keys_url": "https://api.github.com/repos/baxterthehacker/public-repo/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/baxterthehacker/public-repo/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/baxterthehacker/public-repo/teams",
"hooks_url": "https://api.github.com/repos/baxterthehacker/public-repo/hooks",
"issue_events_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/events{/number}",
"events_url": "https://api.github.com/repos/baxterthehacker/public-repo/events",
"assignees_url": "https://api.github.com/repos/baxterthehacker/public-repo/assignees{/user}",
"branches_url": "https://api.github.com/repos/baxterthehacker/public-repo/branches{/branch}",
"tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/tags",
"blobs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/{sha}",
"languages_url": "https://api.github.com/repos/baxterthehacker/public-repo/languages",
"stargazers_url": "https://api.github.com/repos/baxterthehacker/public-repo/stargazers",
"contributors_url": "https://api.github.com/repos/baxterthehacker/public-repo/contributors",
"subscribers_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscribers",
"subscription_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscription",
"commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/baxterthehacker/public-repo/contents/{+path}",
"compare_url": "https://api.github.com/repos/baxterthehacker/public-repo/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/baxterthehacker/public-repo/merges",
"archive_url": "https://api.github.com/repos/baxterthehacker/public-repo/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/baxterthehacker/public-repo/downloads",
"issues_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues{/number}",
"pulls_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}",
"milestones_url": "https://api.github.com/repos/baxterthehacker/public-repo/milestones{/number}",
"notifications_url": "https://api.github.com/repos/baxterthehacker/public-repo/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/baxterthehacker/public-repo/labels{/name}",
"releases_url": "https://api.github.com/repos/baxterthehacker/public-repo/releases{/id}",
"created_at": "2015-05-05T23:40:12Z",
"updated_at": "2015-05-05T23:40:12Z",
"pushed_at": "2015-05-05T23:40:26Z",
"git_url": "git://github.com/baxterthehacker/public-repo.git",
"ssh_url": "git@github.com:baxterthehacker/public-repo.git",
"clone_url": "https://github.com/baxterthehacker/public-repo.git",
"svn_url": "https://github.com/baxterthehacker/public-repo",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 0,
"mirror_url": null,
"open_issues_count": 1,
"forks": 0,
"open_issues": 1,
"watchers": 0,
"default_branch": "master"
}
},
"base": {
"label": "baxterthehacker:master",
"ref": "master",
"sha": "9049f1265b7d61be4a8904a9a27120d2064dab3b",
"user": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
},
"repo": {
"id": 35129377,
"name": "public-repo",
"full_name": "baxterthehacker/public-repo",
"owner": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/baxterthehacker/public-repo",
"description": "",
"fork": false,
"url": "https://api.github.com/repos/baxterthehacker/public-repo",
"forks_url": "https://api.github.com/repos/baxterthehacker/public-repo/forks",
"keys_url": "https://api.github.com/repos/baxterthehacker/public-repo/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/baxterthehacker/public-repo/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/baxterthehacker/public-repo/teams",
"hooks_url": "https://api.github.com/repos/baxterthehacker/public-repo/hooks",
"issue_events_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/events{/number}",
"events_url": "https://api.github.com/repos/baxterthehacker/public-repo/events",
"assignees_url": "https://api.github.com/repos/baxterthehacker/public-repo/assignees{/user}",
"branches_url": "https://api.github.com/repos/baxterthehacker/public-repo/branches{/branch}",
"tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/tags",
"blobs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/{sha}",
"languages_url": "https://api.github.com/repos/baxterthehacker/public-repo/languages",
"stargazers_url": "https://api.github.com/repos/baxterthehacker/public-repo/stargazers",
"contributors_url": "https://api.github.com/repos/baxterthehacker/public-repo/contributors",
"subscribers_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscribers",
"subscription_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscription",
"commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/baxterthehacker/public-repo/contents/{+path}",
"compare_url": "https://api.github.com/repos/baxterthehacker/public-repo/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/baxterthehacker/public-repo/merges",
"archive_url": "https://api.github.com/repos/baxterthehacker/public-repo/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/baxterthehacker/public-repo/downloads",
"issues_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues{/number}",
"pulls_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}",
"milestones_url": "https://api.github.com/repos/baxterthehacker/public-repo/milestones{/number}",
"notifications_url": "https://api.github.com/repos/baxterthehacker/public-repo/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/baxterthehacker/public-repo/labels{/name}",
"releases_url": "https://api.github.com/repos/baxterthehacker/public-repo/releases{/id}",
"created_at": "2015-05-05T23:40:12Z",
"updated_at": "2015-05-05T23:40:12Z",
"pushed_at": "2015-05-05T23:40:26Z",
"git_url": "git://github.com/baxterthehacker/public-repo.git",
"ssh_url": "git@github.com:baxterthehacker/public-repo.git",
"clone_url": "https://github.com/baxterthehacker/public-repo.git",
"svn_url": "https://github.com/baxterthehacker/public-repo",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 0,
"mirror_url": null,
"open_issues_count": 1,
"forks": 0,
"open_issues": 1,
"watchers": 0,
"default_branch": "master"
}
},
"_links": {
"self": {
"href": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/1"
},
"html": {
"href": "https://github.com/baxterthehacker/public-repo/pull/1"
},
"issue": {
"href": "https://api.github.com/repos/baxterthehacker/public-repo/issues/1"
},
"comments": {
"href": "https://api.github.com/repos/baxterthehacker/public-repo/issues/1/comments"
},
"review_comments": {
"href": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/1/comments"
},
"review_comment": {
"href": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/comments{/number}"
},
"commits": {
"href": "https://api.github.com/repos/baxterthehacker/public-repo/pulls/1/commits"
},
"statuses": {
"href": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"
}
},
"merged": false,
"mergeable": null,
"mergeable_state": "unknown",
"merged_by": null,
"comments": 0,
"review_comments": 0,
"commits": 1,
"additions": 1,
"deletions": 1,
"changed_files": 1
},
"repository": {
"id": 35129377,
"name": "public-repo",
"full_name": "baxterthehacker/public-repo",
"owner": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/baxterthehacker/public-repo",
"description": "",
"fork": false,
"url": "https://api.github.com/repos/baxterthehacker/public-repo",
"forks_url": "https://api.github.com/repos/baxterthehacker/public-repo/forks",
"keys_url": "https://api.github.com/repos/baxterthehacker/public-repo/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/baxterthehacker/public-repo/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/baxterthehacker/public-repo/teams",
"hooks_url": "https://api.github.com/repos/baxterthehacker/public-repo/hooks",
"issue_events_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/events{/number}",
"events_url": "https://api.github.com/repos/baxterthehacker/public-repo/events",
"assignees_url": "https://api.github.com/repos/baxterthehacker/public-repo/assignees{/user}",
"branches_url": "https://api.github.com/repos/baxterthehacker/public-repo/branches{/branch}",
"tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/tags",
"blobs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/{sha}",
"languages_url": "https://api.github.com/repos/baxterthehacker/public-repo/languages",
"stargazers_url": "https://api.github.com/repos/baxterthehacker/public-repo/stargazers",
"contributors_url": "https://api.github.com/repos/baxterthehacker/public-repo/contributors",
"subscribers_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscribers",
"subscription_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscription",
"commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/baxterthehacker/public-repo/contents/{+path}",
"compare_url": "https://api.github.com/repos/baxterthehacker/public-repo/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/baxterthehacker/public-repo/merges",
"archive_url": "https://api.github.com/repos/baxterthehacker/public-repo/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/baxterthehacker/public-repo/downloads",
"issues_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues{/number}",
"pulls_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}",
"milestones_url": "https://api.github.com/repos/baxterthehacker/public-repo/milestones{/number}",
"notifications_url": "https://api.github.com/repos/baxterthehacker/public-repo/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/baxterthehacker/public-repo/labels{/name}",
"releases_url": "https://api.github.com/repos/baxterthehacker/public-repo/releases{/id}",
"created_at": "2015-05-05T23:40:12Z",
"updated_at": "2015-05-05T23:40:12Z",
"pushed_at": "2015-05-05T23:40:26Z",
"git_url": "git://github.com/baxterthehacker/public-repo.git",
"ssh_url": "git@github.com:baxterthehacker/public-repo.git",
"clone_url": "https://github.com/baxterthehacker/public-repo.git",
"svn_url": "https://github.com/baxterthehacker/public-repo",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 0,
"mirror_url": null,
"open_issues_count": 1,
"forks": 0,
"open_issues": 1,
"watchers": 0,
"default_branch": "master"
},
"sender": {
"login": "baxterthehacker",
"id": 6752317,
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/baxterthehacker",
"html_url": "https://github.com/baxterthehacker",
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
"type": "User",
"site_admin": false
}
}`
}
func PushEventJSON() string {
return `{ return `{
"ref": "refs/heads/changes", "ref": "refs/heads/changes",
"before": "9049f1265b7d61be4a8904a9a27120d2064dab3b", "before": "9049f1265b7d61be4a8904a9a27120d2064dab3b",
@ -2832,7 +2301,7 @@ func PushEventJSON() string {
}` }`
} }
func RepositoryEventJSON() string { func repositoryEventJSON() string {
return `{ return `{
"action": "created", "action": "created",
"repository": { "repository": {
@ -2954,7 +2423,7 @@ func RepositoryEventJSON() string {
}` }`
} }
func ReleaseEventJSON() string { func releaseEventJSON() string {
return `{ return `{
"action": "published", "action": "published",
"release": { "release": {
@ -3105,7 +2574,7 @@ func ReleaseEventJSON() string {
}` }`
} }
func StatusEventJSON() string { func statusEventJSON() string {
return `{ return `{
"id": 214015194, "id": 214015194,
"sha": "9049f1265b7d61be4a8904a9a27120d2064dab3b", "sha": "9049f1265b7d61be4a8904a9a27120d2064dab3b",
@ -3314,7 +2783,7 @@ func StatusEventJSON() string {
}` }`
} }
func TeamAddEventJSON() string { func teamAddEventJSON() string {
return `{ return `{
"team": { "team": {
"name": "github", "name": "github",
@ -3446,7 +2915,7 @@ func TeamAddEventJSON() string {
}` }`
} }
func WatchEventJSON() string { func watchEventJSON() string {
return `{ return `{
"action": "started", "action": "started",
"repository": { "repository": {

View File

@ -11,7 +11,7 @@ import (
const meas = "github_webhooks" const meas = "github_webhooks"
type event interface { type event interface {
NewMetric() telegraf.Metric newMetric() telegraf.Metric
} }
type repository struct { type repository struct {
@ -90,7 +90,7 @@ type commitCommentEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s commitCommentEvent) NewMetric() telegraf.Metric { func (s commitCommentEvent) newMetric() telegraf.Metric {
event := "commit_comment" event := "commit_comment"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -117,7 +117,7 @@ type createEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s createEvent) NewMetric() telegraf.Metric { func (s createEvent) newMetric() telegraf.Metric {
event := "create" event := "create"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -144,7 +144,7 @@ type deleteEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s deleteEvent) NewMetric() telegraf.Metric { func (s deleteEvent) newMetric() telegraf.Metric {
event := "delete" event := "delete"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -170,7 +170,7 @@ type deploymentEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s deploymentEvent) NewMetric() telegraf.Metric { func (s deploymentEvent) newMetric() telegraf.Metric {
event := "deployment" event := "deployment"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -199,7 +199,7 @@ type deploymentStatusEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s deploymentStatusEvent) NewMetric() telegraf.Metric { func (s deploymentStatusEvent) newMetric() telegraf.Metric {
event := "delete" event := "delete"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -229,7 +229,7 @@ type forkEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s forkEvent) NewMetric() telegraf.Metric { func (s forkEvent) newMetric() telegraf.Metric {
event := "fork" event := "fork"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -255,7 +255,7 @@ type gollumEvent struct {
} }
// REVIEW: Going to be lazy and not deal with the pages. // REVIEW: Going to be lazy and not deal with the pages.
func (s gollumEvent) NewMetric() telegraf.Metric { func (s gollumEvent) newMetric() telegraf.Metric {
event := "gollum" event := "gollum"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -280,7 +280,7 @@ type issueCommentEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s issueCommentEvent) NewMetric() telegraf.Metric { func (s issueCommentEvent) newMetric() telegraf.Metric {
event := "issue_comment" event := "issue_comment"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -309,7 +309,7 @@ type issuesEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s issuesEvent) NewMetric() telegraf.Metric { func (s issuesEvent) newMetric() telegraf.Metric {
event := "issue" event := "issue"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -337,7 +337,7 @@ type memberEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s memberEvent) NewMetric() telegraf.Metric { func (s memberEvent) newMetric() telegraf.Metric {
event := "member" event := "member"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -364,7 +364,7 @@ type membershipEvent struct {
Team team `json:"team"` Team team `json:"team"`
} }
func (s membershipEvent) NewMetric() telegraf.Metric { func (s membershipEvent) newMetric() telegraf.Metric {
event := "membership" event := "membership"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -385,7 +385,7 @@ type pageBuildEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s pageBuildEvent) NewMetric() telegraf.Metric { func (s pageBuildEvent) newMetric() telegraf.Metric {
event := "page_build" event := "page_build"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -408,7 +408,7 @@ type publicEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s publicEvent) NewMetric() telegraf.Metric { func (s publicEvent) newMetric() telegraf.Metric {
event := "public" event := "public"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -433,7 +433,7 @@ type pullRequestEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s pullRequestEvent) NewMetric() telegraf.Metric { func (s pullRequestEvent) newMetric() telegraf.Metric {
event := "pull_request" event := "pull_request"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -467,7 +467,7 @@ type pullRequestReviewCommentEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s pullRequestReviewCommentEvent) NewMetric() telegraf.Metric { func (s pullRequestReviewCommentEvent) newMetric() telegraf.Metric {
event := "pull_request_review_comment" event := "pull_request_review_comment"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -503,7 +503,7 @@ type pushEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s pushEvent) NewMetric() telegraf.Metric { func (s pushEvent) newMetric() telegraf.Metric {
event := "push" event := "push"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -530,7 +530,7 @@ type releaseEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s releaseEvent) NewMetric() telegraf.Metric { func (s releaseEvent) newMetric() telegraf.Metric {
event := "release" event := "release"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -554,7 +554,7 @@ type repositoryEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s repositoryEvent) NewMetric() telegraf.Metric { func (s repositoryEvent) newMetric() telegraf.Metric {
event := "repository" event := "repository"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -579,7 +579,7 @@ type statusEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s statusEvent) NewMetric() telegraf.Metric { func (s statusEvent) newMetric() telegraf.Metric {
event := "status" event := "status"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -605,7 +605,7 @@ type teamAddEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s teamAddEvent) NewMetric() telegraf.Metric { func (s teamAddEvent) newMetric() telegraf.Metric {
event := "team_add" event := "team_add"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,
@ -629,7 +629,7 @@ type watchEvent struct {
Sender sender `json:"sender"` Sender sender `json:"sender"`
} }
func (s watchEvent) NewMetric() telegraf.Metric { func (s watchEvent) newMetric() telegraf.Metric {
event := "delete" event := "delete"
t := map[string]string{ t := map[string]string{
"event": event, "event": event,

View File

@ -11,9 +11,9 @@ import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
) )
func GithubWebhookRequest(t *testing.T, event, jsonString string) { func githubWebhookRequest(t *testing.T, event, jsonString string) {
var acc testutil.Accumulator var acc testutil.Accumulator
gh := &GithubWebhook{Path: "/github", acc: &acc, log: testutil.Logger{}} gh := &Webhook{Path: "/github", acc: &acc, log: testutil.Logger{}}
req, err := http.NewRequest("POST", "/github", strings.NewReader(jsonString)) req, err := http.NewRequest("POST", "/github", strings.NewReader(jsonString))
require.NoError(t, err) require.NoError(t, err)
req.Header.Add("X-Github-Event", event) req.Header.Add("X-Github-Event", event)
@ -24,9 +24,9 @@ func GithubWebhookRequest(t *testing.T, event, jsonString string) {
} }
} }
func GithubWebhookRequestWithSignature(t *testing.T, event, jsonString, signature string, expectedStatus int) { func githubWebhookRequestWithSignature(t *testing.T, event, jsonString, signature string, expectedStatus int) {
var acc testutil.Accumulator var acc testutil.Accumulator
gh := &GithubWebhook{Path: "/github", Secret: "signature", acc: &acc, log: testutil.Logger{}} gh := &Webhook{Path: "/github", secret: "signature", acc: &acc, log: testutil.Logger{}}
req, err := http.NewRequest("POST", "/github", strings.NewReader(jsonString)) req, err := http.NewRequest("POST", "/github", strings.NewReader(jsonString))
require.NoError(t, err) require.NoError(t, err)
req.Header.Add("X-Github-Event", event) req.Header.Add("X-Github-Event", event)
@ -39,91 +39,91 @@ func GithubWebhookRequestWithSignature(t *testing.T, event, jsonString, signatur
} }
func TestCommitCommentEvent(t *testing.T) { func TestCommitCommentEvent(t *testing.T) {
GithubWebhookRequest(t, "commit_comment", CommitCommentEventJSON()) githubWebhookRequest(t, "commit_comment", commitCommentEventJSON())
} }
func TestPingEvent(t *testing.T) { func TestPingEvent(t *testing.T) {
GithubWebhookRequest(t, "ping", "") githubWebhookRequest(t, "ping", "")
} }
func TestDeleteEvent(t *testing.T) { func TestDeleteEvent(t *testing.T) {
GithubWebhookRequest(t, "delete", DeleteEventJSON()) githubWebhookRequest(t, "delete", deleteEventJSON())
} }
func TestDeploymentEvent(t *testing.T) { func TestDeploymentEvent(t *testing.T) {
GithubWebhookRequest(t, "deployment", DeploymentEventJSON()) githubWebhookRequest(t, "deployment", deploymentEventJSON())
} }
func TestDeploymentStatusEvent(t *testing.T) { func TestDeploymentStatusEvent(t *testing.T) {
GithubWebhookRequest(t, "deployment_status", DeploymentStatusEventJSON()) githubWebhookRequest(t, "deployment_status", deploymentStatusEventJSON())
} }
func TestForkEvent(t *testing.T) { func TestForkEvent(t *testing.T) {
GithubWebhookRequest(t, "fork", ForkEventJSON()) githubWebhookRequest(t, "fork", forkEventJSON())
} }
func TestGollumEvent(t *testing.T) { func TestGollumEvent(t *testing.T) {
GithubWebhookRequest(t, "gollum", GollumEventJSON()) githubWebhookRequest(t, "gollum", gollumEventJSON())
} }
func TestIssueCommentEvent(t *testing.T) { func TestIssueCommentEvent(t *testing.T) {
GithubWebhookRequest(t, "issue_comment", IssueCommentEventJSON()) githubWebhookRequest(t, "issue_comment", issueCommentEventJSON())
} }
func TestIssuesEvent(t *testing.T) { func TestIssuesEvent(t *testing.T) {
GithubWebhookRequest(t, "issues", IssuesEventJSON()) githubWebhookRequest(t, "issues", issuesEventJSON())
} }
func TestMemberEvent(t *testing.T) { func TestMemberEvent(t *testing.T) {
GithubWebhookRequest(t, "member", MemberEventJSON()) githubWebhookRequest(t, "member", memberEventJSON())
} }
func TestMembershipEvent(t *testing.T) { func TestMembershipEvent(t *testing.T) {
GithubWebhookRequest(t, "membership", MembershipEventJSON()) githubWebhookRequest(t, "membership", membershipEventJSON())
} }
func TestPageBuildEvent(t *testing.T) { func TestPageBuildEvent(t *testing.T) {
GithubWebhookRequest(t, "page_build", PageBuildEventJSON()) githubWebhookRequest(t, "page_build", pageBuildEventJSON())
} }
func TestPublicEvent(t *testing.T) { func TestPublicEvent(t *testing.T) {
GithubWebhookRequest(t, "public", PublicEventJSON()) githubWebhookRequest(t, "public", publicEventJSON())
} }
func TestPullRequestReviewCommentEvent(t *testing.T) { func TestPullRequestReviewCommentEvent(t *testing.T) {
GithubWebhookRequest(t, "pull_request_review_comment", PullRequestReviewCommentEventJSON()) githubWebhookRequest(t, "pull_request_review_comment", pullRequestReviewCommentEventJSON())
} }
func TestPushEvent(t *testing.T) { func TestPushEvent(t *testing.T) {
GithubWebhookRequest(t, "push", PushEventJSON()) githubWebhookRequest(t, "push", pushEventJSON())
} }
func TestReleaseEvent(t *testing.T) { func TestReleaseEvent(t *testing.T) {
GithubWebhookRequest(t, "release", ReleaseEventJSON()) githubWebhookRequest(t, "release", releaseEventJSON())
} }
func TestRepositoryEvent(t *testing.T) { func TestRepositoryEvent(t *testing.T) {
GithubWebhookRequest(t, "repository", RepositoryEventJSON()) githubWebhookRequest(t, "repository", repositoryEventJSON())
} }
func TestStatusEvent(t *testing.T) { func TestStatusEvent(t *testing.T) {
GithubWebhookRequest(t, "status", StatusEventJSON()) githubWebhookRequest(t, "status", statusEventJSON())
} }
func TestTeamAddEvent(t *testing.T) { func TestTeamAddEvent(t *testing.T) {
GithubWebhookRequest(t, "team_add", TeamAddEventJSON()) githubWebhookRequest(t, "team_add", teamAddEventJSON())
} }
func TestWatchEvent(t *testing.T) { func TestWatchEvent(t *testing.T) {
GithubWebhookRequest(t, "watch", WatchEventJSON()) githubWebhookRequest(t, "watch", watchEventJSON())
} }
func TestEventWithSignatureFail(t *testing.T) { func TestEventWithSignatureFail(t *testing.T) {
GithubWebhookRequestWithSignature(t, "watch", WatchEventJSON(), "signature", http.StatusBadRequest) githubWebhookRequestWithSignature(t, "watch", watchEventJSON(), "signature", http.StatusBadRequest)
} }
func TestEventWithSignatureSuccess(t *testing.T) { func TestEventWithSignatureSuccess(t *testing.T) {
GithubWebhookRequestWithSignature(t, "watch", WatchEventJSON(), generateSignature("signature", []byte(WatchEventJSON())), http.StatusOK) githubWebhookRequestWithSignature(t, "watch", watchEventJSON(), generateSignature("signature", []byte(watchEventJSON())), http.StatusOK)
} }
func TestCheckSignatureSuccess(t *testing.T) { func TestCheckSignatureSuccess(t *testing.T) {

View File

@ -13,14 +13,15 @@ import (
"github.com/influxdata/telegraf/plugins/common/auth" "github.com/influxdata/telegraf/plugins/common/auth"
) )
type MandrillWebhook struct { type Webhook struct {
Path string Path string
acc telegraf.Accumulator acc telegraf.Accumulator
log telegraf.Logger log telegraf.Logger
auth.BasicAuth auth.BasicAuth
} }
func (md *MandrillWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) { // Register registers the webhook with the provided router
func (md *Webhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
router.HandleFunc(md.Path, returnOK).Methods("HEAD") router.HandleFunc(md.Path, returnOK).Methods("HEAD")
router.HandleFunc(md.Path, md.eventHandler).Methods("POST") router.HandleFunc(md.Path, md.eventHandler).Methods("POST")
@ -33,7 +34,7 @@ func returnOK(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func (md *MandrillWebhook) eventHandler(w http.ResponseWriter, r *http.Request) { func (md *Webhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
if !md.Verify(r) { if !md.Verify(r) {
@ -51,7 +52,7 @@ func (md *MandrillWebhook) eventHandler(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
var events []MandrillEvent var events []mandrillEvent
err = json.Unmarshal([]byte(data.Get("mandrill_events")), &events) err = json.Unmarshal([]byte(data.Get("mandrill_events")), &events)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -59,7 +60,7 @@ func (md *MandrillWebhook) eventHandler(w http.ResponseWriter, r *http.Request)
} }
for _, event := range events { for _, event := range events {
md.acc.AddFields("mandrill_webhooks", event.Fields(), event.Tags(), time.Unix(event.TimeStamp, 0)) md.acc.AddFields("mandrill_webhooks", event.fields(), event.tags(), time.Unix(event.TimeStamp, 0))
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@ -1,23 +1,18 @@
package mandrill package mandrill
type Event interface { type mandrillEvent struct {
Tags() map[string]string
Fields() map[string]interface{}
}
type MandrillEvent struct {
EventName string `json:"event"` EventName string `json:"event"`
TimeStamp int64 `json:"ts"` TimeStamp int64 `json:"ts"`
ID string `json:"_id"` ID string `json:"_id"`
} }
func (me *MandrillEvent) Tags() map[string]string { func (me *mandrillEvent) tags() map[string]string {
return map[string]string{ return map[string]string{
"event": me.EventName, "event": me.EventName,
} }
} }
func (me *MandrillEvent) Fields() map[string]interface{} { func (me *mandrillEvent) fields() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"id": me.ID, "id": me.ID,
} }

View File

@ -13,7 +13,7 @@ import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
) )
func postWebhooks(t *testing.T, md *MandrillWebhook, eventBody string) *httptest.ResponseRecorder { func postWebhooks(t *testing.T, md *Webhook, eventBody string) *httptest.ResponseRecorder {
body := url.Values{} body := url.Values{}
body.Set("mandrill_events", eventBody) body.Set("mandrill_events", eventBody)
req, err := http.NewRequest("POST", "/mandrill", strings.NewReader(body.Encode())) req, err := http.NewRequest("POST", "/mandrill", strings.NewReader(body.Encode()))
@ -44,7 +44,7 @@ func TestHead(t *testing.T) {
func TestSendEvent(t *testing.T) { func TestSendEvent(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
md := &MandrillWebhook{Path: "/mandrill", acc: &acc} md := &Webhook{Path: "/mandrill", acc: &acc}
resp := postWebhooks(t, md, "["+readFile(t, "testdata/send_event.json")+"]") resp := postWebhooks(t, md, "["+readFile(t, "testdata/send_event.json")+"]")
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST send returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST send returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
@ -63,7 +63,7 @@ func TestSendEvent(t *testing.T) {
func TestMultipleEvents(t *testing.T) { func TestMultipleEvents(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
md := &MandrillWebhook{Path: "/mandrill", acc: &acc} md := &Webhook{Path: "/mandrill", acc: &acc}
resp := postWebhooks(t, md, "["+readFile(t, "testdata/send_event.json")+","+readFile(t, "testdata/hard_bounce_event.json")+"]") resp := postWebhooks(t, md, "["+readFile(t, "testdata/send_event.json")+","+readFile(t, "testdata/hard_bounce_event.json")+"]")
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST send returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST send returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)

View File

@ -15,7 +15,7 @@ const (
contentType = "application/x-www-form-urlencoded" contentType = "application/x-www-form-urlencoded"
) )
func post(t *testing.T, pt *PapertrailWebhook, contentType, body string) *httptest.ResponseRecorder { func post(t *testing.T, pt *Webhook, contentType, body string) *httptest.ResponseRecorder {
req, err := http.NewRequest("POST", "/", strings.NewReader(body)) req, err := http.NewRequest("POST", "/", strings.NewReader(body))
require.NoError(t, err) require.NoError(t, err)
req.Header.Set("Content-Type", contentType) req.Header.Set("Content-Type", contentType)
@ -26,7 +26,7 @@ func post(t *testing.T, pt *PapertrailWebhook, contentType, body string) *httpte
func TestWrongContentType(t *testing.T) { func TestWrongContentType(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc} pt := &Webhook{Path: "/papertrail", acc: &acc}
form := url.Values{} form := url.Values{}
form.Set("payload", sampleEventPayload) form.Set("payload", sampleEventPayload)
data := form.Encode() data := form.Encode()
@ -37,7 +37,7 @@ func TestWrongContentType(t *testing.T) {
func TestMissingPayload(t *testing.T) { func TestMissingPayload(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc} pt := &Webhook{Path: "/papertrail", acc: &acc}
resp := post(t, pt, contentType, "") resp := post(t, pt, contentType, "")
require.Equal(t, http.StatusBadRequest, resp.Code) require.Equal(t, http.StatusBadRequest, resp.Code)
@ -45,7 +45,7 @@ func TestMissingPayload(t *testing.T) {
func TestPayloadNotJSON(t *testing.T) { func TestPayloadNotJSON(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc} pt := &Webhook{Path: "/papertrail", acc: &acc}
resp := post(t, pt, contentType, "payload={asdf]") resp := post(t, pt, contentType, "payload={asdf]")
require.Equal(t, http.StatusBadRequest, resp.Code) require.Equal(t, http.StatusBadRequest, resp.Code)
@ -53,7 +53,7 @@ func TestPayloadNotJSON(t *testing.T) {
func TestPayloadInvalidJSON(t *testing.T) { func TestPayloadInvalidJSON(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc} pt := &Webhook{Path: "/papertrail", acc: &acc}
resp := post(t, pt, contentType, `payload={"value": 42}`) resp := post(t, pt, contentType, `payload={"value": 42}`)
require.Equal(t, http.StatusBadRequest, resp.Code) require.Equal(t, http.StatusBadRequest, resp.Code)
@ -61,7 +61,7 @@ func TestPayloadInvalidJSON(t *testing.T) {
func TestEventPayload(t *testing.T) { func TestEventPayload(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc} pt := &Webhook{Path: "/papertrail", acc: &acc}
form := url.Values{} form := url.Values{}
form.Set("payload", sampleEventPayload) form.Set("payload", sampleEventPayload)
@ -111,7 +111,7 @@ func TestEventPayload(t *testing.T) {
func TestCountPayload(t *testing.T) { func TestCountPayload(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
pt := &PapertrailWebhook{Path: "/papertrail", acc: &acc} pt := &Webhook{Path: "/papertrail", acc: &acc}
form := url.Values{} form := url.Values{}
form.Set("payload", sampleCountPayload) form.Set("payload", sampleCountPayload)
resp := post(t, pt, contentType, form.Encode()) resp := post(t, pt, contentType, form.Encode())

View File

@ -12,21 +12,22 @@ import (
"github.com/influxdata/telegraf/plugins/common/auth" "github.com/influxdata/telegraf/plugins/common/auth"
) )
type PapertrailWebhook struct { type Webhook struct {
Path string Path string
acc telegraf.Accumulator acc telegraf.Accumulator
log telegraf.Logger log telegraf.Logger
auth.BasicAuth auth.BasicAuth
} }
func (pt *PapertrailWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) { // Register registers the webhook with the provided router
func (pt *Webhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
router.HandleFunc(pt.Path, pt.eventHandler).Methods("POST") router.HandleFunc(pt.Path, pt.eventHandler).Methods("POST")
pt.log = log pt.log = log
pt.log.Infof("Started the papertrail_webhook on %s", pt.Path) pt.log.Infof("Started the papertrail_webhook on %s", pt.Path)
pt.acc = acc pt.acc = acc
} }
func (pt *PapertrailWebhook) eventHandler(w http.ResponseWriter, r *http.Request) { func (pt *Webhook) eventHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" { if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
http.Error(w, "Unsupported Media Type", http.StatusUnsupportedMediaType) http.Error(w, "Unsupported Media Type", http.StatusUnsupportedMediaType)
return return
@ -43,7 +44,7 @@ func (pt *PapertrailWebhook) eventHandler(w http.ResponseWriter, r *http.Request
return return
} }
var payload Payload var payload payload
err := json.Unmarshal([]byte(data), &payload) err := json.Unmarshal([]byte(data), &payload)
if err != nil { if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest) http.Error(w, "Bad Request", http.StatusBadRequest)

View File

@ -4,7 +4,7 @@ import (
"time" "time"
) )
type Event struct { type event struct {
ID int64 `json:"id"` ID int64 `json:"id"`
ReceivedAt time.Time `json:"received_at"` ReceivedAt time.Time `json:"received_at"`
DisplayReceivedAt string `json:"display_received_at"` DisplayReceivedAt string `json:"display_received_at"`
@ -18,13 +18,13 @@ type Event struct {
Message string `json:"message"` Message string `json:"message"`
} }
type Count struct { type count struct {
SourceName string `json:"source_name"` SourceName string `json:"source_name"`
SourceID int64 `json:"source_id"` SourceID int64 `json:"source_id"`
TimeSeries *map[int64]uint64 `json:"timeseries"` TimeSeries *map[int64]uint64 `json:"timeseries"`
} }
type SavedSearch struct { type savedSearch struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Query string `json:"query"` Query string `json:"query"`
@ -32,10 +32,10 @@ type SavedSearch struct {
SearchURL string `json:"html_search_url"` SearchURL string `json:"html_search_url"`
} }
type Payload struct { type payload struct {
Events []*Event `json:"events"` Events []*event `json:"events"`
Counts []*Count `json:"counts"` Counts []*count `json:"counts"`
SavedSearch *SavedSearch `json:"saved_search"` SavedSearch *savedSearch `json:"saved_search"`
MaxID string `json:"max_id"` MaxID string `json:"max_id"`
MinID string `json:"min_id"` MinID string `json:"min_id"`
} }

View File

@ -33,25 +33,26 @@ func newEvent() *event {
} }
} }
func (e *event) Time() (time.Time, error) { func (e *event) time() (time.Time, error) {
return time.Parse("2006-01-02T15:04:05Z", e.PublishedAt) return time.Parse("2006-01-02T15:04:05Z", e.PublishedAt)
} }
type ParticleWebhook struct { type Webhook struct {
Path string Path string
acc telegraf.Accumulator acc telegraf.Accumulator
log telegraf.Logger log telegraf.Logger
auth.BasicAuth auth.BasicAuth
} }
func (rb *ParticleWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) { // Register registers the webhook with the provided router
func (rb *Webhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST") router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST")
rb.log = log rb.log = log
rb.log.Infof("Started the webhooks_particle on %s", rb.Path) rb.log.Infof("Started the webhooks_particle on %s", rb.Path)
rb.acc = acc rb.acc = acc
} }
func (rb *ParticleWebhook) eventHandler(w http.ResponseWriter, r *http.Request) { func (rb *Webhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
if !rb.Verify(r) { if !rb.Verify(r) {
@ -66,7 +67,7 @@ func (rb *ParticleWebhook) eventHandler(w http.ResponseWriter, r *http.Request)
return return
} }
pTime, err := e.Time() pTime, err := e.time()
if err != nil { if err != nil {
pTime = time.Now() pTime = time.Now()
} }

View File

@ -11,7 +11,7 @@ import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
) )
func postWebhooks(t *testing.T, rb *ParticleWebhook, eventBody string) *httptest.ResponseRecorder { func postWebhooks(t *testing.T, rb *Webhook, eventBody string) *httptest.ResponseRecorder {
req, err := http.NewRequest("POST", "/", strings.NewReader(eventBody)) req, err := http.NewRequest("POST", "/", strings.NewReader(eventBody))
require.NoError(t, err) require.NoError(t, err)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -25,8 +25,8 @@ func postWebhooks(t *testing.T, rb *ParticleWebhook, eventBody string) *httptest
func TestNewItem(t *testing.T) { func TestNewItem(t *testing.T) {
t.Parallel() t.Parallel()
var acc testutil.Accumulator var acc testutil.Accumulator
rb := &ParticleWebhook{Path: "/particle", acc: &acc} rb := &Webhook{Path: "/particle", acc: &acc}
resp := postWebhooks(t, rb, NewItemJSON()) resp := postWebhooks(t, rb, newItemJSON())
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
} }
@ -53,8 +53,8 @@ func TestNewItem(t *testing.T) {
func TestUnknowItem(t *testing.T) { func TestUnknowItem(t *testing.T) {
t.Parallel() t.Parallel()
var acc testutil.Accumulator var acc testutil.Accumulator
rb := &ParticleWebhook{Path: "/particle", acc: &acc} rb := &Webhook{Path: "/particle", acc: &acc}
resp := postWebhooks(t, rb, UnknowJSON()) resp := postWebhooks(t, rb, unknownJSON())
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST unknown returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST unknown returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
} }
@ -63,8 +63,8 @@ func TestUnknowItem(t *testing.T) {
func TestDefaultMeasurementName(t *testing.T) { func TestDefaultMeasurementName(t *testing.T) {
t.Parallel() t.Parallel()
var acc testutil.Accumulator var acc testutil.Accumulator
rb := &ParticleWebhook{Path: "/particle", acc: &acc} rb := &Webhook{Path: "/particle", acc: &acc}
resp := postWebhooks(t, rb, BlankMeasurementJSON()) resp := postWebhooks(t, rb, blankMeasurementJSON())
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
} }
@ -80,7 +80,7 @@ func TestDefaultMeasurementName(t *testing.T) {
acc.AssertContainsTaggedFields(t, "eventName", fields, tags) acc.AssertContainsTaggedFields(t, "eventName", fields, tags)
} }
func BlankMeasurementJSON() string { func blankMeasurementJSON() string {
return ` return `
{ {
"event": "eventName", "event": "eventName",
@ -104,7 +104,7 @@ func BlankMeasurementJSON() string {
}` }`
} }
func NewItemJSON() string { func newItemJSON() string {
return ` return `
{ {
"event": "temperature", "event": "temperature",
@ -136,7 +136,7 @@ func NewItemJSON() string {
}` }`
} }
func UnknowJSON() string { func unknownJSON() string {
return ` return `
{ {
"event": "roger" "event": "roger"

View File

@ -13,21 +13,22 @@ import (
"github.com/influxdata/telegraf/plugins/common/auth" "github.com/influxdata/telegraf/plugins/common/auth"
) )
type RollbarWebhook struct { type Webhook struct {
Path string Path string
acc telegraf.Accumulator acc telegraf.Accumulator
log telegraf.Logger log telegraf.Logger
auth.BasicAuth auth.BasicAuth
} }
func (rb *RollbarWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) { // Register registers the webhook with the provided router
func (rb *Webhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST") router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST")
rb.log = log rb.log = log
rb.log.Infof("Started the webhooks_rollbar on %s", rb.Path) rb.log.Infof("Started the webhooks_rollbar on %s", rb.Path)
rb.acc = acc rb.acc = acc
} }
func (rb *RollbarWebhook) eventHandler(w http.ResponseWriter, r *http.Request) { func (rb *Webhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
if !rb.Verify(r) { if !rb.Verify(r) {
@ -48,13 +49,13 @@ func (rb *RollbarWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
event, err := NewEvent(dummyEvent, data) event, err := newEvent(dummyEvent, data)
if err != nil { if err != nil {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
} }
rb.acc.AddFields("rollbar_webhooks", event.Fields(), event.Tags(), time.Now()) rb.acc.AddFields("rollbar_webhooks", event.fields(), event.tags(), time.Now())
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
@ -67,7 +68,7 @@ func generateEvent(event event, data []byte) (event, error) {
return event, nil return event, nil
} }
func NewEvent(dummyEvent *dummyEvent, data []byte) (event, error) { func newEvent(dummyEvent *dummyEvent, data []byte) (event, error) {
switch dummyEvent.EventName { switch dummyEvent.EventName {
case "new_item": case "new_item":
return generateEvent(&newItem{}, data) return generateEvent(&newItem{}, data)

View File

@ -3,8 +3,8 @@ package rollbar
import "strconv" import "strconv"
type event interface { type event interface {
Tags() map[string]string tags() map[string]string
Fields() map[string]interface{} fields() map[string]interface{}
} }
type dummyEvent struct { type dummyEvent struct {
@ -32,7 +32,7 @@ type newItem struct {
Data newItemData `json:"data"` Data newItemData `json:"data"`
} }
func (ni *newItem) Tags() map[string]string { func (ni *newItem) tags() map[string]string {
return map[string]string{ return map[string]string{
"event": ni.EventName, "event": ni.EventName,
"environment": ni.Data.Item.Environment, "environment": ni.Data.Item.Environment,
@ -42,7 +42,7 @@ func (ni *newItem) Tags() map[string]string {
} }
} }
func (ni *newItem) Fields() map[string]interface{} { func (ni *newItem) fields() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"id": ni.Data.Item.ID, "id": ni.Data.Item.ID,
} }
@ -69,7 +69,7 @@ type occurrence struct {
Data occurrenceData `json:"data"` Data occurrenceData `json:"data"`
} }
func (o *occurrence) Tags() map[string]string { func (o *occurrence) tags() map[string]string {
return map[string]string{ return map[string]string{
"event": o.EventName, "event": o.EventName,
"environment": o.Data.Item.Environment, "environment": o.Data.Item.Environment,
@ -79,7 +79,7 @@ func (o *occurrence) Tags() map[string]string {
} }
} }
func (o *occurrence) Fields() map[string]interface{} { func (o *occurrence) fields() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"id": o.Data.Item.ID, "id": o.Data.Item.ID,
} }
@ -100,7 +100,7 @@ type deploy struct {
Data deployData `json:"data"` Data deployData `json:"data"`
} }
func (ni *deploy) Tags() map[string]string { func (ni *deploy) tags() map[string]string {
return map[string]string{ return map[string]string{
"event": ni.EventName, "event": ni.EventName,
"environment": ni.Data.Deploy.Environment, "environment": ni.Data.Deploy.Environment,
@ -108,7 +108,7 @@ func (ni *deploy) Tags() map[string]string {
} }
} }
func (ni *deploy) Fields() map[string]interface{} { func (ni *deploy) fields() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"id": ni.Data.Deploy.ID, "id": ni.Data.Deploy.ID,
} }

View File

@ -1,6 +1,6 @@
package rollbar package rollbar
func NewItemJSON() string { func newItemJSON() string {
return ` return `
{ {
"event_name": "new_item", "event_name": "new_item",
@ -68,7 +68,7 @@ func NewItemJSON() string {
}` }`
} }
func OccurrenceJSON() string { func occurrenceJSON() string {
return ` return `
{ {
"event_name": "occurrence", "event_name": "occurrence",
@ -132,7 +132,7 @@ func OccurrenceJSON() string {
}` }`
} }
func DeployJSON() string { func deployJSON() string {
return ` return `
{ {
"event_name": "deploy", "event_name": "deploy",
@ -152,7 +152,7 @@ func DeployJSON() string {
}` }`
} }
func UnknowJSON() string { func unknownJSON() string {
return ` return `
{ {
"event_name": "roger" "event_name": "roger"

View File

@ -11,7 +11,7 @@ import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
) )
func postWebhooks(t *testing.T, rb *RollbarWebhook, eventBody string) *httptest.ResponseRecorder { func postWebhooks(t *testing.T, rb *Webhook, eventBody string) *httptest.ResponseRecorder {
req, err := http.NewRequest("POST", "/", strings.NewReader(eventBody)) req, err := http.NewRequest("POST", "/", strings.NewReader(eventBody))
require.NoError(t, err) require.NoError(t, err)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -24,8 +24,8 @@ func postWebhooks(t *testing.T, rb *RollbarWebhook, eventBody string) *httptest.
func TestNewItem(t *testing.T) { func TestNewItem(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
rb := &RollbarWebhook{Path: "/rollbar", acc: &acc} rb := &Webhook{Path: "/rollbar", acc: &acc}
resp := postWebhooks(t, rb, NewItemJSON()) resp := postWebhooks(t, rb, newItemJSON())
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
} }
@ -47,8 +47,8 @@ func TestNewItem(t *testing.T) {
func TestOccurrence(t *testing.T) { func TestOccurrence(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
rb := &RollbarWebhook{Path: "/rollbar", acc: &acc} rb := &Webhook{Path: "/rollbar", acc: &acc}
resp := postWebhooks(t, rb, OccurrenceJSON()) resp := postWebhooks(t, rb, occurrenceJSON())
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST occurrence returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST occurrence returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
} }
@ -70,8 +70,8 @@ func TestOccurrence(t *testing.T) {
func TestDeploy(t *testing.T) { func TestDeploy(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
rb := &RollbarWebhook{Path: "/rollbar", acc: &acc} rb := &Webhook{Path: "/rollbar", acc: &acc}
resp := postWebhooks(t, rb, DeployJSON()) resp := postWebhooks(t, rb, deployJSON())
if resp.Code != http.StatusOK { if resp.Code != http.StatusOK {
t.Errorf("POST deploy returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) t.Errorf("POST deploy returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
} }
@ -90,8 +90,8 @@ func TestDeploy(t *testing.T) {
} }
func TestUnknowItem(t *testing.T) { func TestUnknowItem(t *testing.T) {
rb := &RollbarWebhook{Path: "/rollbar"} rb := &Webhook{Path: "/rollbar"}
resp := postWebhooks(t, rb, UnknowJSON()) resp := postWebhooks(t, rb, unknownJSON())
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)
} }

View File

@ -31,65 +31,34 @@ const (
defaultWriteTimeout = 10 * time.Second defaultWriteTimeout = 10 * time.Second
) )
type Webhook interface {
Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger)
}
func init() {
inputs.Add("webhooks", func() telegraf.Input { return NewWebhooks() })
}
type Webhooks struct { type Webhooks struct {
ServiceAddress string `toml:"service_address"` ServiceAddress string `toml:"service_address"`
ReadTimeout config.Duration `toml:"read_timeout"` ReadTimeout config.Duration `toml:"read_timeout"`
WriteTimeout config.Duration `toml:"write_timeout"` WriteTimeout config.Duration `toml:"write_timeout"`
Github *github.GithubWebhook `toml:"github"` Artifactory *artifactory.Webhook `toml:"artifactory"`
Filestack *filestack.FilestackWebhook `toml:"filestack"` Filestack *filestack.Webhook `toml:"filestack"`
Mandrill *mandrill.MandrillWebhook `toml:"mandrill"` Github *github.Webhook `toml:"github"`
Rollbar *rollbar.RollbarWebhook `toml:"rollbar"` Mandrill *mandrill.Webhook `toml:"mandrill"`
Papertrail *papertrail.PapertrailWebhook `toml:"papertrail"` Papertrail *papertrail.Webhook `toml:"papertrail"`
Particle *particle.ParticleWebhook `toml:"particle"` Particle *particle.Webhook `toml:"particle"`
Artifactory *artifactory.ArtifactoryWebhook `toml:"artifactory"` Rollbar *rollbar.Webhook `toml:"rollbar"`
Log telegraf.Logger `toml:"-"` Log telegraf.Logger `toml:"-"`
srv *http.Server srv *http.Server
} }
func NewWebhooks() *Webhooks { // Webhook is an interface that all webhooks must implement
return &Webhooks{} type Webhook interface {
// Register registers the webhook with the provided router
Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger)
} }
func (*Webhooks) SampleConfig() string { func (*Webhooks) SampleConfig() string {
return sampleConfig return sampleConfig
} }
func (*Webhooks) Gather(telegraf.Accumulator) error {
return nil
}
// AvailableWebhooks Looks for fields which implement Webhook interface
func (wb *Webhooks) AvailableWebhooks() []Webhook {
webhooks := make([]Webhook, 0)
s := reflect.ValueOf(wb).Elem()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
if !f.CanInterface() {
continue
}
if wbPlugin, ok := f.Interface().(Webhook); ok {
if !reflect.ValueOf(wbPlugin).IsNil() {
webhooks = append(webhooks, wbPlugin)
}
}
}
return webhooks
}
func (wb *Webhooks) Start(acc telegraf.Accumulator) error { func (wb *Webhooks) Start(acc telegraf.Accumulator) error {
if wb.ReadTimeout < config.Duration(time.Second) { if wb.ReadTimeout < config.Duration(time.Second) {
wb.ReadTimeout = config.Duration(defaultReadTimeout) wb.ReadTimeout = config.Duration(defaultReadTimeout)
@ -100,7 +69,7 @@ func (wb *Webhooks) Start(acc telegraf.Accumulator) error {
r := mux.NewRouter() r := mux.NewRouter()
for _, webhook := range wb.AvailableWebhooks() { for _, webhook := range wb.availableWebhooks() {
webhook.Register(r, acc, wb.Log) webhook.Register(r, acc, wb.Log)
} }
@ -128,7 +97,40 @@ func (wb *Webhooks) Start(acc telegraf.Accumulator) error {
return nil return nil
} }
func (*Webhooks) Gather(telegraf.Accumulator) error {
return nil
}
func (wb *Webhooks) Stop() { func (wb *Webhooks) Stop() {
wb.srv.Close() wb.srv.Close()
wb.Log.Infof("Stopping the Webhooks service") wb.Log.Infof("Stopping the Webhooks service")
} }
// availableWebhooks Looks for fields which implement Webhook interface
func (wb *Webhooks) availableWebhooks() []Webhook {
webhooks := make([]Webhook, 0)
s := reflect.ValueOf(wb).Elem()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
if !f.CanInterface() {
continue
}
if wbPlugin, ok := f.Interface().(Webhook); ok {
if !reflect.ValueOf(wbPlugin).IsNil() {
webhooks = append(webhooks, wbPlugin)
}
}
}
return webhooks
}
func newWebhooks() *Webhooks {
return &Webhooks{}
}
func init() {
inputs.Add("webhooks", func() telegraf.Input { return newWebhooks() })
}

View File

@ -5,46 +5,60 @@ import (
"testing" "testing"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/artifactory" "github.com/influxdata/telegraf/plugins/inputs/webhooks/artifactory"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/filestack"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/github" "github.com/influxdata/telegraf/plugins/inputs/webhooks/github"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/mandrill"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/papertrail" "github.com/influxdata/telegraf/plugins/inputs/webhooks/papertrail"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/particle" "github.com/influxdata/telegraf/plugins/inputs/webhooks/particle"
"github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar" "github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar"
) )
func TestAvailableWebhooks(t *testing.T) { func TestAvailableWebhooks(t *testing.T) {
wb := NewWebhooks() wb := newWebhooks()
expected := make([]Webhook, 0) expected := make([]Webhook, 0)
if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) { if !reflect.DeepEqual(wb.availableWebhooks(), expected) {
t.Errorf("expected to %v.\nGot %v", expected, wb.AvailableWebhooks()) t.Errorf("expected to %v.\nGot %v", expected, wb.availableWebhooks())
} }
wb.Github = &github.GithubWebhook{Path: "/github"} wb.Artifactory = &artifactory.Webhook{Path: "/artifactory"}
expected = append(expected, wb.Github)
if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.AvailableWebhooks())
}
wb.Rollbar = &rollbar.RollbarWebhook{Path: "/rollbar"}
expected = append(expected, wb.Rollbar)
if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.AvailableWebhooks())
}
wb.Papertrail = &papertrail.PapertrailWebhook{Path: "/papertrail"}
expected = append(expected, wb.Papertrail)
if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.AvailableWebhooks())
}
wb.Particle = &particle.ParticleWebhook{Path: "/particle"}
expected = append(expected, wb.Particle)
if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.AvailableWebhooks())
}
wb.Artifactory = &artifactory.ArtifactoryWebhook{Path: "/artifactory"}
expected = append(expected, wb.Artifactory) expected = append(expected, wb.Artifactory)
if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) { if !reflect.DeepEqual(wb.availableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.AvailableWebhooks()) t.Errorf("expected to be %v.\nGot %v", expected, wb.availableWebhooks())
}
wb.Filestack = &filestack.Webhook{Path: "/filestack"}
expected = append(expected, wb.Filestack)
if !reflect.DeepEqual(wb.availableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.availableWebhooks())
}
wb.Github = &github.Webhook{Path: "/github"}
expected = append(expected, wb.Github)
if !reflect.DeepEqual(wb.availableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.availableWebhooks())
}
wb.Mandrill = &mandrill.Webhook{Path: "/mandrill"}
expected = append(expected, wb.Mandrill)
if !reflect.DeepEqual(wb.availableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.availableWebhooks())
}
wb.Papertrail = &papertrail.Webhook{Path: "/papertrail"}
expected = append(expected, wb.Papertrail)
if !reflect.DeepEqual(wb.availableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.availableWebhooks())
}
wb.Particle = &particle.Webhook{Path: "/particle"}
expected = append(expected, wb.Particle)
if !reflect.DeepEqual(wb.availableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.availableWebhooks())
}
wb.Rollbar = &rollbar.Webhook{Path: "/rollbar"}
expected = append(expected, wb.Rollbar)
if !reflect.DeepEqual(wb.availableWebhooks(), expected) {
t.Errorf("expected to be %v.\nGot %v", expected, wb.availableWebhooks())
} }
} }