~sircmpwn/builds.sr.ht

builds.sr.ht/api/graph/schema.resolvers.go -rw-r--r-- 19.5 KiB
0467110eDrew DeVault qemu: upgrade to 7.0.0 8 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
package graph

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
	"bytes"
	"context"
	"database/sql"
	"encoding/json"
	"fmt"
	"net/http"
	"strings"

	"git.sr.ht/~sircmpwn/builds.sr.ht/api/graph/api"
	"git.sr.ht/~sircmpwn/builds.sr.ht/api/graph/model"
	"git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders"
	"git.sr.ht/~sircmpwn/core-go/auth"
	"git.sr.ht/~sircmpwn/core-go/config"
	"git.sr.ht/~sircmpwn/core-go/database"
	coremodel "git.sr.ht/~sircmpwn/core-go/model"
	sq "github.com/Masterminds/squirrel"
	"github.com/lib/pq"
	yaml "gopkg.in/yaml.v2"
)

func (r *jobResolver) Owner(ctx context.Context, obj *model.Job) (model.Entity, error) {
	return loaders.ForContext(ctx).UsersByID.Load(obj.OwnerID)
}

func (r *jobResolver) Group(ctx context.Context, obj *model.Job) (*model.JobGroup, error) {
	if obj.JobGroupID == nil {
		return nil, nil
	}
	return loaders.ForContext(ctx).JobGroupsByID.Load(*obj.JobGroupID)
}

func (r *jobResolver) Tasks(ctx context.Context, obj *model.Job) ([]*model.Task, error) {
	var tasks []*model.Task

	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		task := (&model.Task{}).As(`t`)
		rows, err := database.
			Select(ctx, task).
			From(`task t`).
			Join(`job j ON j.id = t.job_id`).
			Columns(`j.runner`).
			Where(`t.job_id = ?`, obj.ID).
			RunWith(tx).
			QueryContext(ctx)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		for rows.Next() {
			var task model.Task
			if err := rows.Scan(append(database.Scan(ctx, &task), &task.Runner)...); err != nil {
				panic(err)
			}
			tasks = append(tasks, &task)
		}
		return nil
	}); err != nil {
		return nil, err
	}

	return tasks, nil
}

func (r *jobResolver) Artifacts(ctx context.Context, obj *model.Job) ([]*model.Artifact, error) {
	var artifacts []*model.Artifact
	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		artifact := (&model.Artifact{}).As(`a`)
		rows, err := database.
			Select(ctx, artifact).
			From(`artifact a`).
			Where(`a.job_id = ?`, obj.ID).
			RunWith(tx).
			QueryContext(ctx)
		if err != nil {
			panic(err)
		}
		defer rows.Close()
		for rows.Next() {
			var artifact model.Artifact
			if err := rows.Scan(database.Scan(ctx, &artifact)...); err != nil {
				panic(err)
			}
			artifacts = append(artifacts, &artifact)
		}
		return nil
	}); err != nil {
		return nil, err
	}
	return artifacts, nil
}

func (r *jobResolver) Log(ctx context.Context, obj *model.Job) (*model.Log, error) {
	if obj.Runner == nil {
		return nil, nil
	}
	url := fmt.Sprintf("http://%s/logs/%d/log", *obj.Runner, obj.ID)
	return FetchLogs(ctx, url)
}

func (r *jobResolver) Secrets(ctx context.Context, obj *model.Job) ([]model.Secret, error) {
	var secrets []model.Secret

	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		row := tx.QueryRowContext(ctx,
			`SELECT manifest FROM job WHERE id = $1`, obj.ID)

		var rawManifest string
		if err := row.Scan(&rawManifest); err != nil {
			return err
		}

		type Manifest struct {
			Secrets []string `yaml:"secrets"`
		}

		var manifest Manifest
		if err := yaml.Unmarshal([]byte(rawManifest), &manifest); err != nil {
			return err
		}

		secret := (&model.RawSecret{}).As(`sec`)
		rows, err := database.
			Select(ctx, secret).
			From(`secret sec`).
			Where(sq.Expr(`sec.uuid = ANY(?)`, pq.Array(manifest.Secrets))).
			Where(`sec.user_id = ? AND sec.user_id = ?`,
				obj.OwnerID, auth.ForContext(ctx).UserID).
			RunWith(tx).
			QueryContext(ctx)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		for rows.Next() {
			var sec model.RawSecret
			if err := rows.Scan(database.Scan(ctx, &sec)...); err != nil {
				panic(err)
			}
			secrets = append(secrets, sec.ToSecret())
		}

		return nil
	}); err != nil {
		return nil, err
	}

	return secrets, nil
}

func (r *jobGroupResolver) Owner(ctx context.Context, obj *model.JobGroup) (model.Entity, error) {
	return loaders.ForContext(ctx).UsersByID.Load(obj.OwnerID)
}

func (r *jobGroupResolver) Jobs(ctx context.Context, obj *model.JobGroup) ([]*model.Job, error) {
	var jobs []*model.Job
	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		job := (&model.Job{}).As(`j`)
		rows, err := database.
			Select(ctx, job).
			From(`job j`).
			Where(`j.job_group_id = ?`, obj.ID).
			RunWith(tx).
			QueryContext(ctx)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		for rows.Next() {
			var job model.Job
			if err := rows.Scan(database.Scan(ctx, &job)...); err != nil {
				panic(err)
			}
			jobs = append(jobs, &job)
		}
		return nil
	}); err != nil {
		return nil, err
	}

	return jobs, nil
}

func (r *jobGroupResolver) Triggers(ctx context.Context, obj *model.JobGroup) ([]model.Trigger, error) {
	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) {
	man, err := LoadManifest(manifest)
	if err != nil {
		return nil, err
	}
	conf := config.ForContext(ctx)
	user := auth.ForContext(ctx)

	allowFree, _ := conf.Get("builds.sr.ht", "allow-free")
	if allowFree != "yes" {
		if user.UserType != "admin" &&
			user.UserType != "active_free" &&
			user.UserType != "active_paying" {
			return nil, fmt.Errorf("A paid account is required to submit builds")
		}
	}

	var job model.Job
	if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		sec := true
		if secrets != nil {
			sec = *secrets
		}
		status := "pending"
		if execute == nil || *execute {
			status = "pending"
		}
		tags := strings.Join(tags, "/")

		// TODO: Refactor tags into a pg array
		row := tx.QueryRowContext(ctx, `INSERT INTO job (
			created, updated,
			manifest, owner_id, secrets, note, tags, image, status
		) VALUES (
			NOW() at time zone 'utc',
			NOW() at time zone 'utc',
			$1, $2, $3, $4, $5, $6, $7
		) RETURNING
			id, created, updated, manifest, note, image, runner, owner_id,
			tags, status
		`, manifest, user.UserID, sec, note, tags, man.Image, status)

		if err := row.Scan(&job.ID, &job.Created, &job.Updated, &job.Manifest,
			&job.Note, &job.Image, &job.Runner, &job.OwnerID, &job.RawTags,
			&job.RawStatus); err != nil {
			return err
		}

		for _, task := range man.Tasks {
			var name string
			for key, _ := range task {
				name = key
				break
			}

			_, err := tx.ExecContext(ctx, `INSERT INTO task (
				created, updated, name, status, job_id
			) VALUES (
				NOW() at time zone 'utc',
				NOW() at time zone 'utc',
				$1, 'pending', $2
			)`, name, job.ID)

			if err != nil {
				return err
			}
		}

		return nil
	}); err != nil {
		return nil, err
	}

	if execute == nil || *execute {
		if err := SubmitJob(ctx, job.ID, man); err != nil {
			panic(err)
		}
	}

	return &job, nil
}

func (r *mutationResolver) Start(ctx context.Context, jobID int) (*model.Job, error) {
	var job model.Job

	if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		row := tx.QueryRowContext(ctx, `
			UPDATE job
			SET status = 'queued'
			WHERE
				id = $1 AND
				status = 'pending' AND
				owner_id = $2
			RETURNING
				id, created, updated, manifest, note, image, runner, owner_id,
				tags, status;
		`, jobID, auth.ForContext(ctx).UserID)
		if err := row.Scan(&job.ID, &job.Created, &job.Updated, &job.Manifest,
			&job.Note, &job.Image, &job.Runner, &job.OwnerID, &job.RawTags,
			&job.RawStatus); err != nil {
			if err == sql.ErrNoRows {
				return fmt.Errorf("Found no pending jobs with this ID on your account")
			}
			return err
		}

		man, err := LoadManifest(job.Manifest)
		if err != nil {
			// Invalid manifests should not have made it to the database
			panic(err)
		}
		if err := SubmitJob(ctx, job.ID, man); err != nil {
			panic(err)
		}

		return nil
	}); err != nil {
		return nil, err
	}

	return &job, nil
}

func (r *mutationResolver) Cancel(ctx context.Context, jobID int) (*model.Job, error) {
	job := (&model.Job{}).As(`j`)

	if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		row := database.
			Select(ctx, job).
			From(`job j`).
			Columns(`j.runner`).
			Where(`j.id = ?`, jobID).
			Where(`j.owner_id = ?`, auth.ForContext(ctx).UserID).
			Where(`j.status = 'running'`).
			RunWith(tx).
			QueryRowContext(ctx)

		var runner string
		if err := row.Scan(append(
			database.Scan(ctx, job), &runner)...); err != nil {
			if err == sql.ErrNoRows {
				return fmt.Errorf("Found no running jobs for your account with this ID")
			}
			return err
		}

		resp, err := http.Post(fmt.Sprintf("http://%s/job/%d/cancel",
			runner, job.ID), "application/json", bytes.NewReader([]byte{}))
		if err != nil {
			return err
		}
		if resp.StatusCode != 200 {
			return fmt.Errorf("Failed to cancel job")
		}

		job.RawStatus = "cancelled"
		return nil
	}); err != nil {
		return nil, err
	}

	return job, nil
}

func (r *mutationResolver) CreateGroup(ctx context.Context, jobIds []int, triggers []*model.TriggerInput, execute *bool, note *string) (*model.JobGroup, error) {
	var group model.JobGroup

	if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		row := tx.QueryRowContext(ctx, `
			INSERT INTO job_group (
				created, updated, owner_id, note
			) VALUES (
				NOW() at time zone 'utc',
				NOW() at time zone 'utc',
				$1, $2
			) RETURNING
				id, created, note, owner_id
			`, auth.ForContext(ctx).UserID, note)
		if err := row.Scan(&group.ID, &group.Created,
			&group.Note, &group.OwnerID); err != nil {
			return err
		}

		result, err := tx.ExecContext(ctx, `
			UPDATE job
			SET job_group_id = $1
			WHERE
				job_group_id IS NULL AND
				status = 'pending' AND
				id = ANY($2) AND owner_id = $3;
			`, group.ID, pq.Array(jobIds), auth.ForContext(ctx).UserID)
		if err != nil {
			panic(err)
		}

		affected, err := result.RowsAffected()
		if err != nil {
			panic(err)
		}

		if affected != int64(len(jobIds)) {
			return fmt.Errorf("Invalid list of job IDs. All jobs must be owned by you, not assigned to another job group, and in the pending state.")
		}

		for _, trigger := range triggers {
			if trigger == nil {
				panic("GQL schema invariant broken: nil trigger")
			}

			type EmailDetails struct {
				Action    string  `json:"action"`
				Condition string  `json:"condition"`
				To        string  `json:"to"`
				Cc        *string `json:"cc"`
				InReplyTo *string `json:"in_reply_to"`
			}
			type WebhookDetails struct {
				Action    string `json:"action"`
				Condition string `json:"condition"`
				URL       string `json:"url"`
			}

			var (
				details     string
				triggerType string
			)

			// TODO: Drop job_id column from triggers (unused)
			switch trigger.Type {
			case model.TriggerTypeEmail:
				triggerType = "email"
				email := EmailDetails{
					Action:    "email",
					Condition: strings.ToLower(trigger.Condition.String()),
					To:        trigger.Email.To,
					Cc:        trigger.Email.Cc,
					InReplyTo: trigger.Email.InReplyTo,
				}
				buf, err := json.Marshal(&email)
				if err != nil {
					panic(err)
				}
				details = string(buf)
			case model.TriggerTypeWebhook:
				triggerType = "webhook"
				webhook := WebhookDetails{
					Action:    "webhook",
					Condition: strings.ToLower(trigger.Condition.String()),
					URL:       trigger.Webhook.URL,
				}
				buf, err := json.Marshal(&webhook)
				if err != nil {
					panic(err)
				}
				details = string(buf)
			default:
				panic("GQL schema invariant broken: invalid trigger type")
			}

			_, err = tx.ExecContext(ctx, `
				INSERT INTO trigger (
					created, updated, details, condition, trigger_type,
					job_group_id
				) VALUES (
					NOW() at time zone 'utc',
					NOW() at time zone 'utc',
					$1, $2, $3, $4
				)`,
				details, strings.ToLower(trigger.Condition.String()),
				triggerType, group.ID)

			if err != nil {
				return err
			}
		}

		if execute != nil && !*execute {
			return nil
		}

		return StartJobGroupUnsafe(ctx, tx, group.ID)
	}); err != nil {
		return nil, err
	}

	return &group, nil
}

func (r *mutationResolver) StartGroup(ctx context.Context, groupID int) (*model.JobGroup, error) {
	var group model.JobGroup

	if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		row := tx.QueryRowContext(ctx, `
			UPDATE job_group SET updated = NOW() at time zone 'utc'
			WHERE id = $1 AND owner_id = $2
			RETURNING id, created, note, owner_id
			`, groupID, auth.ForContext(ctx).UserID)
		if err := row.Scan(&group.ID, &group.Created,
			&group.Note, &group.OwnerID); err != nil {
			if err == sql.ErrNoRows {
				return fmt.Errorf("Found no job group by this ID for your account")
			}
			return err
		}

		return StartJobGroupUnsafe(ctx, tx, groupID)
	}); err != nil {
		return nil, err
	}

	return &group, nil
}

func (r *mutationResolver) Claim(ctx context.Context, jobID int) (*model.Job, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) UpdateJob(ctx context.Context, jobID int, status model.JobStatus) (*model.Job, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) UpdateTask(ctx context.Context, taskID int, status model.TaskStatus) (*model.Job, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) CreateArtifact(ctx context.Context, jobID int, path string, contents string) (*model.Artifact, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *pGPKeyResolver) PrivateKey(ctx context.Context, obj *model.PGPKey) (string, error) {
	// TODO: This is simple to implement, but I'm not going to rig it up until
	// we need it
	panic(fmt.Errorf("not implemented"))
}

func (r *queryResolver) Version(ctx context.Context) (*model.Version, error) {
	conf := config.ForContext(ctx)
	buildTimeout, _ := conf.Get("builds.sr.ht::worker", "timeout")
	sshUser, _ := conf.Get("git.sr.ht::dispatch", "/usr/bin/buildsrht-keys")
	sshUser = strings.Split(sshUser, ":")[0]

	return &model.Version{
		Major:           0,
		Minor:           0,
		Patch:           0,
		DeprecationDate: nil,

		Settings: &model.Settings{
			SSHUser:      sshUser,
			BuildTimeout: buildTimeout,
		},
	}, nil
}

func (r *queryResolver) Me(ctx context.Context) (*model.User, error) {
	user := auth.ForContext(ctx)
	return &model.User{
		ID:       user.UserID,
		Created:  user.Created,
		Updated:  user.Updated,
		Username: user.Username,
		Email:    user.Email,
		URL:      user.URL,
		Location: user.Location,
		Bio:      user.Bio,
	}, nil
}

func (r *queryResolver) UserByID(ctx context.Context, id int) (*model.User, error) {
	return loaders.ForContext(ctx).UsersByID.Load(id)
}

func (r *queryResolver) UserByName(ctx context.Context, username string) (*model.User, error) {
	return loaders.ForContext(ctx).UsersByName.Load(username)
}

func (r *queryResolver) Jobs(ctx context.Context, cursor *coremodel.Cursor) (*model.JobCursor, error) {
	if cursor == nil {
		cursor = coremodel.NewCursor(nil)
	}

	var jobs []*model.Job
	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		job := (&model.Job{}).As(`j`)
		query := database.
			Select(ctx, job).
			From(`job j`).
			Where(`j.owner_id = ?`, auth.ForContext(ctx).UserID)
		jobs, cursor = job.QueryWithCursor(ctx, tx, query, cursor)
		return nil
	}); err != nil {
		return nil, err
	}

	return &model.JobCursor{jobs, cursor}, nil
}

func (r *queryResolver) Job(ctx context.Context, id int) (*model.Job, error) {
	return loaders.ForContext(ctx).JobsByID.Load(id)
}

func (r *queryResolver) Secrets(ctx context.Context, cursor *coremodel.Cursor) (*model.SecretCursor, error) {
	if cursor == nil {
		cursor = coremodel.NewCursor(nil)
	}

	var secrets []model.Secret
	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		secret := (&model.RawSecret{}).As(`sec`)
		query := database.
			Select(ctx, secret).
			From(`secret sec`).
			Where(`sec.user_id = ?`, auth.ForContext(ctx).UserID)
		secrets, cursor = secret.QueryWithCursor(ctx, tx, query, cursor)
		return nil
	}); err != nil {
		return nil, err
	}

	return &model.SecretCursor{secrets, cursor}, nil
}

func (r *sSHKeyResolver) PrivateKey(ctx context.Context, obj *model.SSHKey) (string, error) {
	// TODO: This is simple to implement, but I'm not going to rig it up until
	// we need it
	panic(fmt.Errorf("not implemented"))
}

func (r *secretFileResolver) Data(ctx context.Context, obj *model.SecretFile) (string, error) {
	// TODO: This is simple to implement, but I'm not going to rig it up until
	// we need it
	panic(fmt.Errorf("not implemented"))
}

func (r *taskResolver) Log(ctx context.Context, obj *model.Task) (*model.Log, error) {
	if obj.Runner == nil {
		return nil, nil
	}
	url := fmt.Sprintf("http://%s/logs/%d/%s/log", *obj.Runner, obj.JobID, obj.Name)
	return FetchLogs(ctx, url)
}

func (r *taskResolver) Job(ctx context.Context, obj *model.Task) (*model.Job, error) {
	return loaders.ForContext(ctx).JobsByID.Load(obj.JobID)
}

func (r *userResolver) Jobs(ctx context.Context, obj *model.User, cursor *coremodel.Cursor) (*model.JobCursor, error) {
	if cursor == nil {
		cursor = coremodel.NewCursor(nil)
	}

	var jobs []*model.Job
	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		job := (&model.Job{}).As(`j`)
		query := database.
			Select(ctx, job).
			From(`job j`).
			Where(`j.owner_id = ?`, obj.ID)
		jobs, cursor = job.QueryWithCursor(ctx, tx, query, cursor)
		return nil
	}); err != nil {
		return nil, err
	}

	return &model.JobCursor{jobs, cursor}, nil
}

// Job returns api.JobResolver implementation.
func (r *Resolver) Job() api.JobResolver { return &jobResolver{r} }

// JobGroup returns api.JobGroupResolver implementation.
func (r *Resolver) JobGroup() api.JobGroupResolver { return &jobGroupResolver{r} }

// Mutation returns api.MutationResolver implementation.
func (r *Resolver) Mutation() api.MutationResolver { return &mutationResolver{r} }

// PGPKey returns api.PGPKeyResolver implementation.
func (r *Resolver) PGPKey() api.PGPKeyResolver { return &pGPKeyResolver{r} }

// Query returns api.QueryResolver implementation.
func (r *Resolver) Query() api.QueryResolver { return &queryResolver{r} }

// SSHKey returns api.SSHKeyResolver implementation.
func (r *Resolver) SSHKey() api.SSHKeyResolver { return &sSHKeyResolver{r} }

// SecretFile returns api.SecretFileResolver implementation.
func (r *Resolver) SecretFile() api.SecretFileResolver { return &secretFileResolver{r} }

// Task returns api.TaskResolver implementation.
func (r *Resolver) Task() api.TaskResolver { return &taskResolver{r} }

// User returns api.UserResolver implementation.
func (r *Resolver) User() api.UserResolver { return &userResolver{r} }

type jobResolver struct{ *Resolver }
type jobGroupResolver struct{ *Resolver }
type mutationResolver struct{ *Resolver }
type pGPKeyResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type sSHKeyResolver struct{ *Resolver }
type secretFileResolver struct{ *Resolver }
type taskResolver struct{ *Resolver }
type userResolver struct{ *Resolver }