forked from
tangled.org/core
Forked monorepo for Tangled
1package xrpc
2
3import (
4 "context"
5 _ "embed"
6 "encoding/json"
7 "errors"
8 "log/slog"
9 "net/http"
10
11 "github.com/bluesky-social/indigo/atproto/syntax"
12 "github.com/go-chi/chi/v5"
13
14 "tangled.org/core/api/tangled"
15 "tangled.org/core/idresolver"
16 "tangled.org/core/notifier"
17 "tangled.org/core/rbac"
18 "tangled.org/core/spindle/config"
19 "tangled.org/core/spindle/db"
20 "tangled.org/core/spindle/models"
21 "tangled.org/core/spindle/secrets"
22 xrpcerr "tangled.org/core/xrpc/errors"
23 "tangled.org/core/xrpc/serviceauth"
24)
25
26const ActorDid = serviceauth.ActorDid
27
28var ErrNoMatchingWorkflows = errors.New("no workflows to run")
29
30// this is to break an import cycle. spindle imports this package for Xrpc,
31// so this package can't import *spindle.Spindle back.
32type PipelineTrigger interface {
33 TriggerManual(ctx context.Context, repoDid syntax.DID, sha, ref string, workflows []string, sourceRepo syntax.DID, pull PullContext, inputs []*tangled.Pipeline_Pair) (syntax.ATURI, error)
34}
35
36type PullContext struct {
37 IsPullRequest bool
38 Pull syntax.ATURI
39 SourceBranch string
40 TargetBranch string
41}
42
43type Xrpc struct {
44 Logger *slog.Logger
45 Db *db.DB
46 Enforcer *rbac.Enforcer
47 Engines map[string]models.Engine
48 Config *config.Config
49 Resolver *idresolver.Resolver
50 Vault secrets.Manager
51 Notifier *notifier.Notifier
52 ServiceAuth *serviceauth.ServiceAuth
53 Trigger PipelineTrigger
54}
55
56func (x *Xrpc) Router() http.Handler {
57 r := chi.NewRouter()
58
59 r.Group(func(r chi.Router) {
60 r.Use(x.ServiceAuth.VerifyServiceAuth)
61
62 r.Post("/"+tangled.RepoAddSecretNSID, x.AddSecret)
63 r.Post("/"+tangled.RepoRemoveSecretNSID, x.RemoveSecret)
64 r.Get("/"+tangled.RepoListSecretsNSID, x.ListSecrets)
65 r.Post("/"+tangled.CiCancelPipelineNSID, x.CancelPipeline)
66 r.Post("/"+tangled.CiTriggerPipelineNSID, x.TriggerPipeline)
67 })
68
69 // service query endpoints (no auth required)
70 r.Get("/"+tangled.OwnerNSID, x.Owner)
71 r.Get("/"+tangled.CiSubscribePipelineLogsNSID, x.HandleCiSubscribePipelineLogs)
72 r.Get("/"+tangled.CiQueryPipelinesNSID, x.HandleCiQueryPipelines)
73 r.Get("/"+tangled.CiGetPipelineNSID, x.HandleCiGetPipeline)
74
75 return r
76}
77
78// this is slightly different from http_util::write_error to follow the spec:
79//
80// the json object returned must include an "error" and a "message"
81func writeError(w http.ResponseWriter, e xrpcerr.XrpcError, status int) {
82 w.Header().Set("Content-Type", "application/json")
83 w.WriteHeader(status)
84 json.NewEncoder(w).Encode(e)
85}
86
87func writeJson(w http.ResponseWriter, status int, response any) error {
88 w.Header().Set("Content-Type", "application/json")
89 w.WriteHeader(status)
90 return json.NewEncoder(w).Encode(response)
91}