~sircmpwn/sr.ht-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH builds.sr.ht] API: rig up JobGroup { triggers }

Details
Message ID
<20210501194757.12709-1-ecs@d2evs.net>
DKIM signature
pass
Download raw message
Patch: +142 -25
---
 api/graph/api/generated.go    |   8 +--
 api/graph/model/models_gen.go |  20 -------
 api/graph/model/trigger.go    | 108 ++++++++++++++++++++++++++++++++++
 api/graph/schema.resolvers.go |  31 +++++++++-
 4 files changed, 142 insertions(+), 25 deletions(-)
 create mode 100644 api/graph/model/trigger.go

diff --git a/api/graph/api/generated.go b/api/graph/api/generated.go
index 2a1a3b4..9ebd6ee 100644
--- a/api/graph/api/generated.go
+++ b/api/graph/api/generated.go
@@ -2074,9 +2074,9 @@ func (ec *executionContext) _EmailTrigger_cc(ctx context.Context, field graphql.
	if resTmp == nil {
		return graphql.Null
	}
	res := resTmp.(*string)
	res := resTmp.(string)
	fc.Result = res
	return ec.marshalOString2ᚖstring(ctx, field.Selections, res)
	return ec.marshalOString2string(ctx, field.Selections, res)
}

func (ec *executionContext) _EmailTrigger_inReplyTo(ctx context.Context, field graphql.CollectedField, obj *model.EmailTrigger) (ret graphql.Marshaler) {
@@ -2106,9 +2106,9 @@ func (ec *executionContext) _EmailTrigger_inReplyTo(ctx context.Context, field g
	if resTmp == nil {
		return graphql.Null
	}
	res := resTmp.(*string)
	res := resTmp.(string)
	fc.Result = res
	return ec.marshalOString2ᚖstring(ctx, field.Selections, res)
	return ec.marshalOString2string(ctx, field.Selections, res)
}

func (ec *executionContext) _Job_id(ctx context.Context, field graphql.CollectedField, obj *model.Job) (ret graphql.Marshaler) {
diff --git a/api/graph/model/models_gen.go b/api/graph/model/models_gen.go
index 95a4a61..c9311da 100644
--- a/api/graph/model/models_gen.go
+++ b/api/graph/model/models_gen.go
@@ -15,19 +15,6 @@ type Entity interface {
	IsEntity()
}

type Trigger interface {
	IsTrigger()
}

type EmailTrigger struct {
	Condition TriggerCondition `json:"condition"`
	To        string           `json:"to"`
	Cc        *string          `json:"cc"`
	InReplyTo *string          `json:"inReplyTo"`
}

func (EmailTrigger) IsTrigger() {}

type EmailTriggerInput struct {
	To        string  `json:"to"`
	Cc        *string `json:"cc"`
@@ -63,13 +50,6 @@ type Version struct {
	DeprecationDate *time.Time `json:"deprecationDate"`
}

type WebhookTrigger struct {
	Condition TriggerCondition `json:"condition"`
	URL       string           `json:"url"`
}

func (WebhookTrigger) IsTrigger() {}

type WebhookTriggerInput struct {
	URL string `json:"url"`
}
diff --git a/api/graph/model/trigger.go b/api/graph/model/trigger.go
new file mode 100644
index 0000000..ac6515f
--- /dev/null
+++ b/api/graph/model/trigger.go
@@ -0,0 +1,108 @@
package model

import (
	"encoding/json"
	"fmt"

	"git.sr.ht/~sircmpwn/core-go/database"
)

const (
	TRIGGER_EMAIL   = "email"
	TRIGGER_WEBHOOK = "webhook"
)

type Trigger interface {
	IsTrigger()
}

type RawTrigger struct {
	ID          int
	Details     string
	Condition   string
	TriggerType string

	alias  string
	fields *database.ModelFields
}

func (t *RawTrigger) As(alias string) *RawTrigger {
	t.alias = alias
	return t
}

func (t *RawTrigger) Alias() string {
	return t.alias
}

func (t *RawTrigger) Table() string {
	return `"trigger"`
}

type EmailTrigger struct {
	ID        int              `json:"id"`
	To        string           `json:"to"`
	Cc        string           `json:"cc"`
	InReplyTo string           `json:"in_reply_to"`
	Condition TriggerCondition `json:"condition"`
}

func (EmailTrigger) IsTrigger() {}

type WebhookTrigger struct {
	ID        int              `json:"id"`
	URL       string           `json:"url"`
	Condition TriggerCondition `json:"condition"`
}

func (WebhookTrigger) IsTrigger() {}

func (t *RawTrigger) ToTrigger() Trigger {
	var cond TriggerCondition
	switch t.Condition {
	case "always":
		cond = TriggerConditionAlways
	case "failure":
		cond = TriggerConditionFailure
	case "success":
		cond = TriggerConditionSuccess
	default:
		panic("Database invariant broken: unknown trigger condition")
	}
	switch t.TriggerType {
	case TRIGGER_EMAIL:
		p := &EmailTrigger{
			ID:        t.ID,
			Condition: cond,
		}
		fmt.Println(t.Details)
		json.Unmarshal([]byte(t.Details), p)
		return p
	case TRIGGER_WEBHOOK:
		p := &WebhookTrigger{
			ID:        t.ID,
			Condition: cond,
		}
		json.Unmarshal([]byte(t.Details), p)
		return p
	default:
		panic("Database invariant broken: unknown trigger type")
	}
}

func (t *RawTrigger) Fields() *database.ModelFields {
	if t.fields != nil {
		return t.fields
	}
	t.fields = &database.ModelFields{
		Fields: []*database.FieldMap{
			{"condition", "condition", &t.Condition},

			// Always fetch:
			{"id", "", &t.ID},
			{"details", "", &t.Details},
			{"trigger_type", "", &t.TriggerType},
		},
	}
	return t.fields
}
diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go
index 809b2e1..a17a233 100644
--- a/api/graph/schema.resolvers.go
+++ b/api/graph/schema.resolvers.go
@@ -197,7 +197,36 @@ func (r *jobGroupResolver) Jobs(ctx context.Context, obj *model.JobGroup) ([]*mo
}

func (r *jobGroupResolver) Triggers(ctx context.Context, obj *model.JobGroup) ([]model.Trigger, error) {
	panic(fmt.Errorf("not implemented"))
	var triggers []model.Trigger
	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		trigger := (&model.RawTrigger{}).As(`t`)
		rows, err := database.
			Select(ctx, trigger).
			From(`trigger t`).
			Where(`t.job_group_id = ?`, obj.ID).
			RunWith(tx).
			QueryContext(ctx)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		for rows.Next() {
			var trigger model.RawTrigger
			if err := rows.Scan(database.Scan(ctx, &trigger)...); err != nil {
				panic(err)
			}
			triggers = append(triggers, trigger.ToTrigger())
		}
		return nil
	}); err != nil {
		return nil, err
	}

	return triggers, nil
}

func (r *mutationResolver) Submit(ctx context.Context, manifest string, tags []*string, note *string, secrets *bool, execute *bool) (*model.Job, error) {
-- 
2.31.1

[builds.sr.ht/patches] build failed

builds.sr.ht
Details
Message ID
<CB263HM1HMX3.16HSZ41HZ1F1E@cirno>
In-Reply-To
<20210501194757.12709-1-ecs@d2evs.net> (view parent)
DKIM signature
missing
Download raw message
builds.sr.ht/patches: FAILED in 27s

[API: rig up JobGroup { triggers }][0] from [Eyal Sawady][1]

[0]: https://lists.sr.ht/~sircmpwn/sr.ht-dev/patches/22453
[1]: mailto:ecs@d2evs.net

✗ #499290 FAILED builds.sr.ht/patches/debian.yml    https://builds.sr.ht/~sircmpwn/job/499290
✗ #499288 FAILED builds.sr.ht/patches/alpine.yml    https://builds.sr.ht/~sircmpwn/job/499288
✗ #499289 FAILED builds.sr.ht/patches/archlinux.yml https://builds.sr.ht/~sircmpwn/job/499289
Details
Message ID
<CB3M9829YM2N.2T1LQ0OKJ3CKN@taiga>
In-Reply-To
<20210501194757.12709-1-ecs@d2evs.net> (view parent)
DKIM signature
fail
Download raw message
DKIM signature: fail
Thanks!

To git@git.sr.ht:~sircmpwn/builds.sr.ht
   98333db..c3ffc2e  master -> master
Reply to thread Export thread (mbox)