This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / spindle / xrpc / pipeline_cancel_pipeline.go
2.8 kB 100 lines
1package xrpc 2 3import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "strings" 8 9 "github.com/bluesky-social/indigo/api/atproto" 10 "github.com/bluesky-social/indigo/atproto/syntax" 11 "github.com/bluesky-social/indigo/xrpc" 12 securejoin "github.com/cyphar/filepath-securejoin" 13 "tangled.org/core/api/tangled" 14 "tangled.org/core/rbac" 15 "tangled.org/core/spindle/models" 16 xrpcerr "tangled.org/core/xrpc/errors" 17) 18 19func (x *Xrpc) CancelPipeline(w http.ResponseWriter, r *http.Request) { 20 l := x.Logger 21 fail := func(e xrpcerr.XrpcError) { 22 l.Error("failed", "kind", e.Tag, "error", e.Message) 23 writeError(w, e, http.StatusBadRequest) 24 } 25 l.Debug("cancel pipeline") 26 27 actorDid, ok := r.Context().Value(ActorDid).(syntax.DID) 28 if !ok { 29 fail(xrpcerr.MissingActorDidError) 30 return 31 } 32 33 var input tangled.PipelineCancelPipeline_Input 34 if err := json.NewDecoder(r.Body).Decode(&input); err != nil { 35 fail(xrpcerr.GenericError(err)) 36 return 37 } 38 39 aturi := syntax.ATURI(input.Pipeline) 40 wid := models.WorkflowId{ 41 PipelineId: models.PipelineId{ 42 Knot: strings.TrimPrefix(aturi.Authority().String(), "did:web:"), 43 Rkey: aturi.RecordKey().String(), 44 }, 45 Name: input.Workflow, 46 } 47 l.Debug("cancel pipeline", "wid", wid) 48 49 // unfortunately we have to resolve repo-at here 50 repoAt, err := syntax.ParseATURI(input.Repo) 51 if err != nil { 52 fail(xrpcerr.InvalidRepoError(input.Repo)) 53 return 54 } 55 56 ident, err := x.Resolver.ResolveIdent(r.Context(), repoAt.Authority().String()) 57 if err != nil || ident.Handle.IsInvalidHandle() { 58 fail(xrpcerr.GenericError(fmt.Errorf("failed to resolve handle: %w", err))) 59 return 60 } 61 62 xrpcc := xrpc.Client{Host: ident.PDSEndpoint()} 63 resp, err := atproto.RepoGetRecord(r.Context(), &xrpcc, "", tangled.RepoNSID, repoAt.Authority().String(), repoAt.RecordKey().String()) 64 if err != nil { 65 fail(xrpcerr.GenericError(err)) 66 return 67 } 68 69 if _, ok := resp.Value.Val.(*tangled.Repo); !ok { 70 fail(xrpcerr.RepoNotFoundError) 71 return 72 } 73 didSlashRepo, err := securejoin.SecureJoin(ident.DID.String(), repoAt.RecordKey().String()) 74 if err != nil { 75 fail(xrpcerr.GenericError(err)) 76 return 77 } 78 79 // TODO: fine-grained role based control 80 isRepoOwner, err := x.Enforcer.IsRepoOwner(actorDid.String(), rbac.ThisServer, didSlashRepo) 81 if err != nil || !isRepoOwner { 82 fail(xrpcerr.AccessControlError(actorDid.String())) 83 return 84 } 85 for _, engine := range x.Engines { 86 l.Debug("destroying workflow", "wid", wid) 87 err = engine.DestroyWorkflow(r.Context(), wid) 88 if err != nil { 89 fail(xrpcerr.GenericError(fmt.Errorf("failed to destroy workflow: %w", err))) 90 return 91 } 92 err = x.Db.StatusCancelled(wid, "User canceled the workflow", -1, x.Notifier) 93 if err != nil { 94 fail(xrpcerr.GenericError(fmt.Errorf("failed to emit status failed: %w", err))) 95 return 96 } 97 } 98 99 w.WriteHeader(http.StatusOK) 100}