Пример #1
0
// DeliverHooks checks and delivers undelivered hooks.
func DeliverHooks() {
	timeout := time.Duration(setting.WebhookDeliverTimeout) * time.Second
	x.Where("is_deliveried=?", false).Iterate(new(HookTask),
		func(idx int, bean interface{}) error {
			t := bean.(*HookTask)
			req := httplib.Post(t.Url).SetTimeout(timeout, timeout).
				Header("X-Gogs-Delivery", t.Uuid).
				Header("X-Gogs-Event", string(t.EventType))

			switch t.ContentType {
			case JSON:
				req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
			case FORM:
				req.Param("payload", t.PayloadContent)
			}

			t.IsDeliveried = true

			// TODO: record response.
			if _, err := req.Response(); err != nil {
				log.Error(4, "Delivery: %v", err)
			} else {
				t.IsSucceed = true
			}

			if err := UpdateHookTask(t); err != nil {
				log.Error(4, "UpdateHookTask: %v", err)
				return nil
			}

			log.Trace("Hook delivered(%s): %s", t.Uuid, t.PayloadContent)
			return nil
		})
}
Пример #2
0
func deliverHook(t *HookTask) {
	timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
	req := httplib.Post(t.Url).SetTimeout(timeout, timeout).
		Header("X-Gogs-Delivery", t.Uuid).
		Header("X-Gogs-Event", string(t.EventType)).
		SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify})

	switch t.ContentType {
	case JSON:
		req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
	case FORM:
		req.Param("payload", t.PayloadContent)
	}

	t.IsDelivered = true

	// FIXME: record response.
	switch t.Type {
	case GOGS:
		{
			if resp, err := req.Response(); err != nil {
				log.Error(5, "Delivery: %v", err)
			} else {
				resp.Body.Close()
				t.IsSucceed = true
			}
		}
	case SLACK:
		{
			if resp, err := req.Response(); err != nil {
				log.Error(5, "Delivery: %v", err)
			} else {
				defer resp.Body.Close()
				contents, err := ioutil.ReadAll(resp.Body)
				if err != nil {
					log.Error(5, "%s", err)
				} else {
					if string(contents) != "ok" {
						log.Error(5, "slack failed with: %s", string(contents))
					} else {
						t.IsSucceed = true
					}
				}
			}
		}
	}

	t.Delivered = time.Now().UTC().UnixNano()
	if t.IsSucceed {
		log.Trace("Hook delivered(%s): %s", t.Uuid, t.PayloadContent)
	}
}
Пример #3
0
func handleQueue() {
	for {
		select {
		case t := <-taskQueue:
			// Only support JSON now.
			data, err := json.MarshalIndent(t.Payload, "", "\t")
			if err != nil {
				log.Error("hooks.handleQueue(json): %v", err)
				continue
			}

			_, err = httplib.Post(t.Url).SetTimeout(5*time.Second, 5*time.Second).
				Body(data).Response()
			if err != nil {
				log.Error("hooks.handleQueue: Fail to deliver hook: %v", err)
				continue
			}
			log.Info("Hook delivered: %s", string(data))
		}
	}
}
Пример #4
0
// DeliverHooks checks and delivers undelivered hooks.
func DeliverHooks() {
	timeout := time.Duration(setting.WebhookDeliverTimeout) * time.Second
	orm.Where("is_deliveried=?", false).Iterate(new(HookTask),
		func(idx int, bean interface{}) error {
			t := bean.(*HookTask)
			// Only support JSON now.
			if _, err := httplib.Post(t.Url).SetTimeout(timeout, timeout).
				Body([]byte(t.PayloadContent)).Response(); err != nil {
				log.Error("webhook.DeliverHooks(Delivery): %v", err)
				return nil
			}

			t.IsDeliveried = true
			if err := UpdateHookTask(t); err != nil {
				log.Error("webhook.DeliverHooks(UpdateHookTask): %v", err)
				return nil
			}

			log.Trace("Hook delivered: %s", t.PayloadContent)
			return nil
		})
}
Пример #5
0
// DeliverHooks checks and delivers undelivered hooks.
func DeliverHooks() {
	tasks := make([]*HookTask, 0, 10)
	timeout := time.Duration(setting.WebhookDeliverTimeout) * time.Second
	x.Where("is_delivered=?", false).Iterate(new(HookTask),
		func(idx int, bean interface{}) error {
			t := bean.(*HookTask)
			req := httplib.Post(t.Url).SetTimeout(timeout, timeout).
				Header("X-Gogs-Delivery", t.Uuid).
				Header("X-Gogs-Event", string(t.EventType))

			switch t.ContentType {
			case JSON:
				req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
			case FORM:
				req.Param("payload", t.PayloadContent)
			}

			t.IsDelivered = true

			// TODO: record response.
			switch t.Type {
			case GOGS:
				{
					if _, err := req.Response(); err != nil {
						log.Error(4, "Delivery: %v", err)
					} else {
						t.IsSucceed = true
					}
				}
			case SLACK:
				{
					if res, err := req.Response(); err != nil {
						log.Error(4, "Delivery: %v", err)
					} else {
						defer res.Body.Close()
						contents, err := ioutil.ReadAll(res.Body)
						if err != nil {
							log.Error(4, "%s", err)
						} else {
							if string(contents) != "ok" {
								log.Error(4, "slack failed with: %s", string(contents))
							} else {
								t.IsSucceed = true
							}
						}
					}
				}
			}

			tasks = append(tasks, t)

			if t.IsSucceed {
				log.Trace("Hook delivered(%s): %s", t.Uuid, t.PayloadContent)
			}
			return nil
		})

	// Update hook task status.
	for _, t := range tasks {
		if err := UpdateHookTask(t); err != nil {
			log.Error(4, "UpdateHookTask(%d): %v", t.Id, err)
		}
	}
}
Пример #6
0
func (t *HookTask) deliver() {
	t.IsDelivered = true

	timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
	req := httplib.Post(t.URL).SetTimeout(timeout, timeout).
		Header("X-Gogs-Delivery", t.UUID).
		Header("X-Gogs-Event", string(t.EventType)).
		SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify})

	switch t.ContentType {
	case JSON:
		req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
	case FORM:
		req.Param("payload", t.PayloadContent)
	}

	// Record delivery information.
	t.RequestInfo = &HookRequest{
		Headers: map[string]string{},
	}
	for k, vals := range req.Headers() {
		t.RequestInfo.Headers[k] = strings.Join(vals, ",")
	}

	t.ResponseInfo = &HookResponse{
		Headers: map[string]string{},
	}

	defer func() {
		t.Delivered = time.Now().UTC().UnixNano()
		if t.IsSucceed {
			log.Trace("Hook delivered: %s", t.UUID)
		}

		// Update webhook last delivery status.
		w, err := GetWebhookByID(t.HookID)
		if err != nil {
			log.Error(5, "GetWebhookByID: %v", err)
			return
		}
		if t.IsSucceed {
			w.LastStatus = HOOK_STATUS_SUCCEED
		} else {
			w.LastStatus = HOOK_STATUS_FAILED
		}
		if err = UpdateWebhook(w); err != nil {
			log.Error(5, "UpdateWebhook: %v", err)
			return
		}
	}()

	resp, err := req.Response()
	if err != nil {
		t.ResponseInfo.Body = fmt.Sprintf("Delivery: %v", err)
		return
	}
	defer resp.Body.Close()

	// Status code is 20x can be seen as succeed.
	t.IsSucceed = resp.StatusCode/100 == 2
	t.ResponseInfo.Status = resp.StatusCode
	for k, vals := range resp.Header {
		t.ResponseInfo.Headers[k] = strings.Join(vals, ",")
	}

	p, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.ResponseInfo.Body = fmt.Sprintf("read body: %s", err)
		return
	}
	t.ResponseInfo.Body = string(p)

	switch t.Type {
	case SLACK:
		if t.ResponseInfo.Body != "ok" {
			log.Error(5, "slack failed with: %s", t.ResponseInfo.Body)
			t.IsSucceed = false
		}
	}
}