Beispiel #1
0
func (g Github) configure(cr *helpers.ConfigureRequest, method, url string) (helpers.ConfigureResponse, error) {
	events, err := helpers.UnmarshalEvents(cr.Settings)
	if err != nil {
		g.Log.Error("Could not unmarshal events", err)
	}

	cpr := &ConfigurePostRequest{
		Name:   hookType,
		Events: events,
		Active: true,
		Config: &Config{
			URL:         prepareEndpoint(g.PublicURL, GITHUB, cr.ServiceToken),
			ContentType: "json",
			Secret:      g.Secret,
		},
	}

	by, err := json.Marshal(cpr)
	if err != nil {
		return nil, err
	}
	reader := bytes.NewReader(by)

	req, err := http.NewRequest(method, url, reader)
	if err != nil {
		return nil, err
	}

	// Set user's oauth token
	headerAuth := fmt.Sprintf("token %s", cr.UserToken)
	req.Header.Add("Authorization", headerAuth)
	c := new(http.Client)
	resp, err := c.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode >= 300 {
		return nil, errors.New(resp.Status)
	}

	result := make(map[string]interface{})
	// it is delete request with no content
	if resp.ContentLength == 0 {
		return nil, nil
	}

	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return nil, err
	}

	response := helpers.ConfigureResponse{}
	response["serviceId"] = strconv.FormatInt(int64(result["id"].(float64)), 10)
	response["events"] = result["events"]

	return response, nil
}
Beispiel #2
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
	}
}