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