Beispiel #1
0
// SaveWebhook writes the webhook to the postgres database, updating an existing
// row if it exists.
func (db *Driver) SaveWebhook(hook *entity.Webhook) error {
	if hook.ID == nil || hook.IsNew() {
		return db.insertWebhook(hook)
	}

	panic("TODO: update existing webhook")
}
Beispiel #2
0
// Exec runs the use case.
func (kase *CreateWebhook) Exec(hook entity.Webhook) (uid RepoID, err error) {
	hook.ID = &RepoID{}

	err = hook.Valid()
	if err != nil {
		err = &CreateWebhookError{Step: "validate", Child: err}
		return
	}

	err = kase.DB.SaveWebhook(&hook)
	if err != nil {
		err = &CreateWebhookError{Step: "repo", Child: err}
		return
	}

	if hook.IsNew() {
		// TODO: add some further explanation that the repo failed to assign
		// and ID.
		err = &CreateWebhookError{Step: "repo"}
		return
	}

	uid = *hook.ID.(*RepoID)
	return
}
Beispiel #3
0
func (db *Driver) insertWebhook(hook *entity.Webhook) error {
	var (
		id  int64
		mf  sql.NullString
		mtf sql.NullString
	)

	if hook.MemoFilter != nil {
		mtf = sql.NullString{String: hook.MemoFilter.Type, Valid: true}
		mf = sql.NullString{String: hook.MemoFilter.Value, Valid: true}
	}

	err := db.DB.Get(&id, Queries.Webhook.Insert,
		hook.URL.String(),
		string(hook.DestinationFilter),
		mtf,
		mf,
		time.Now().UTC(),
	)
	if err != nil {
		return err
	}

	hook.ID = &usecase.RepoID{T: "webhook", V: id}
	return nil
}