Esempio n. 1
0
func (p *Pivotal) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	token := req.URL.Query().Get("token")
	if token == "" {
		w.WriteHeader(http.StatusBadRequest)
		p.log.Error("Token is not found %v", ErrUserTokenIsNotValid)
		return
	}

	pm, err := readBody(req.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	resources := ""
	comma := ""
	for _, pr := range pm.PrimaryResources {
		resources += fmt.Sprintf("%s[%s](%s)", comma, pr.Name, pr.URL)
		comma = ","
	}

	message := fmt.Sprintf("[%s] %s: %s", pm.Project.Name, pm.Message, resources)

	pr := helpers.NewPushRequest(message)

	if err := helpers.Push(token, pr, p.integrationURL); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		p.log.Error("Could not push message: %s", err)
		return
	}

}
Esempio n. 2
0
// TODO(mehmetali) limit outgoing string, should not be more than 2K char?
func (g GithubListener) output(ctx context.Context, str string) {
	gi, ok := FromGithubContext(ctx)
	if !ok {
		g.Log.Error("Could not push message: github info does not exist in the context")
		return
	}

	pr := helpers.NewPushRequest(str)
	pr.SetPayload("eventType", gi.eventType)

	if err := helpers.Push(gi.token, pr, g.IntegrationUrl); err != nil {
		g.Log.Error("Could not push message: %s", err)
	}
}
Esempio n. 3
0
func (p *Pagerduty) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	token := req.URL.Query().Get("token")
	if token == "" {
		w.WriteHeader(http.StatusBadRequest)
		p.log.Error("Token is not found %v", ErrUserTokenIsNotValid)
		return
	}

	pm := &PagerdutyActivity{}
	err := ReadAndParse(req.Body, pm)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
	}

	if err := pm.validate(); err != nil {
		p.log.Error("Types are not valid %v", ErrCouldNotValidate)
		return
	}

	// fetch events from integration's db, if incoming event is not allowed by user, then stop process
	setting, err := helpers.GetSettings(token, p.integrationURL)
	if err != nil {
		p.log.Error("Could not get settings %v", ErrCouldNotGetSettings)
		return
	}

	events, err := helpers.UnmarshalEvents(setting)
	if err != nil {
		p.log.Error("Could not get events %v", ErrCouldNotGetEvents)
		return
	}

	// if incoming event is not allowed by user, we dont send the message to the integration worker
	// don't need to return any error , just stop process
	if !isAllowedEvent(events, pm.getType()) {
		return
	}

	// message will be created according to incoming data with its incident type
	// there are different incident types; trigger,acknowledge,resolve...
	// createMessage creates different meaningful message for each incident types
	message := pm.createMessage()

	pr := helpers.NewPushRequest(message)

	if err := helpers.Push(token, pr, p.integrationURL); err != nil {
		p.log.Error("Could not push message: %s", err)
		return
	}
}