Ejemplo n.º 1
0
func Hook(ctx *context.Context) {
	collector, err := models.GetCollectorBySecret(ctx.Query("secret"))
	if err != nil {
		if errors.IsCollectorNotFound(err) {
			ctx.Error(403)
		} else {
			ctx.Error(500, err.Error())
		}
		return
	}

	payload, err := ctx.Req.Body().Bytes()
	if err != nil {
		ctx.Error(500, err.Error())
		return
	}

	// NOTE: Currently only support GitHub
	event, err := webhook.ParseGitHubEvent(payload)
	if err != nil {
		ctx.Error(500, err.Error())
		return
	}

	if err = models.NewWebhook(&models.Webhook{
		CollectorID: collector.ID,
		Owner:       tool.FirstNonEmptyString(event.Repository.Owner.Login, event.Repository.Owner.Name),
		RepoName:    event.Repository.Name,
		EventType:   ctx.Req.Header.Get("X-GitHub-Event"),
		Sender:      event.Sender.Login,
		Payload:     string(payload),
	}); err != nil {
		ctx.Error(500, err.Error())
		return
	}

	ctx.Status(202)
}