From 8a27f0f0e8b4e3fe2cfc11faa9e1b6e8a579c377 Mon Sep 17 00:00:00 2001 From: Eoin McAfee <83226740+eoinmcafee00@users.noreply.github.com> Date: Fri, 12 Nov 2021 10:23:08 +0000 Subject: [PATCH] refactor create / find / delete end points for cards (#3159) * refactor create / find / delete end points for cards --- cmd/drone-server/wire_gen.go | 4 ++-- core/card.go | 6 ++++++ go.mod | 2 +- go.sum | 5 ----- handler/api/card/create.go | 7 +------ operator/manager/manager.go | 24 ++++++++++++++++++++++-- operator/manager/rpc/client.go | 5 +++++ operator/manager/rpc2/handler.go | 26 ++++++++++++++++++++++++++ operator/manager/rpc2/server.go | 2 +- 9 files changed, 64 insertions(+), 17 deletions(-) diff --git a/cmd/drone-server/wire_gen.go b/cmd/drone-server/wire_gen.go index ad246dbf..99e04df4 100644 --- a/cmd/drone-server/wire_gen.go +++ b/cmd/drone-server/wire_gen.go @@ -78,16 +78,16 @@ func InitializeApplication(config2 config.Config) (application, error) { reaper := provideReaper(repositoryStore, buildStore, stageStore, coreCanceler, config2) coreLicense := provideLicense(client, config2) datadog := provideDatadog(userStore, repositoryStore, buildStore, system, coreLicense, config2) + cardStore := card.New(db) logStore := provideLogStore(db, config2) logStream := livelog.New(redisDB) netrcService := provideNetrcService(client, renewer, config2) secretStore := secret.New(db, encrypter) globalSecretStore := global.New(db, encrypter) - buildManager := manager.New(buildStore, configService, convertService, corePubsub, logStore, logStream, netrcService, repositoryStore, scheduler, secretStore, globalSecretStore, statusService, stageStore, stepStore, system, userStore, webhookSender) + buildManager := manager.New(buildStore, cardStore, configService, convertService, corePubsub, logStore, logStream, netrcService, repositoryStore, scheduler, secretStore, globalSecretStore, statusService, stageStore, stepStore, system, userStore, webhookSender) secretService := provideSecretPlugin(config2) registryService := provideRegistryPlugin(config2) runner := provideRunner(buildManager, secretService, registryService, config2) - cardStore := card.New(db) hookService := provideHookService(client, renewer, config2) licenseService := license.NewService(userStore, repositoryStore, buildStore, coreLicense) organizationService := provideOrgService(client, renewer) diff --git a/core/card.go b/core/card.go index 935ccc00..2da4dcc4 100644 --- a/core/card.go +++ b/core/card.go @@ -16,9 +16,15 @@ package core import ( "context" + "encoding/json" "io" ) +type CardInput struct { + Schema string `json:"schema"` + Data json.RawMessage `json:"data"` +} + // CardStore manages repository cards. type CardStore interface { // Find returns a card data stream from the datastore. diff --git a/go.mod b/go.mod index 8c6fc93d..2ec5ccef 100644 --- a/go.mod +++ b/go.mod @@ -60,4 +60,4 @@ require ( replace github.com/h2non/gock => gopkg.in/h2non/gock.v1 v1.0.14 -go 1.13 +go 1.13 \ No newline at end of file diff --git a/go.sum b/go.sum index 99b90697..9bc76232 100644 --- a/go.sum +++ b/go.sum @@ -78,10 +78,6 @@ github.com/drone/drone-go v1.4.1-0.20201109202657-b9e58bbbcf27/go.mod h1:fxCf9jA github.com/drone/drone-runtime v1.0.7-0.20190729202838-87c84080f4a1/go.mod h1:+osgwGADc/nyl40J0fdsf8Z09bgcBZXvXXnLOY48zYs= github.com/drone/drone-runtime v1.1.1-0.20200623162453-61e33e2cab5d h1:P5HI/Y9hARTZ3F3EKs0kYijhjXZWQRQHYn1neTi0pWM= github.com/drone/drone-runtime v1.1.1-0.20200623162453-61e33e2cab5d/go.mod h1:4/2QToW5+HGD0y1sTw7X35W1f7YINS14UfDY4isggT8= -github.com/drone/drone-ui v2.2.1+incompatible h1:j5Py26SOjyPHCp294qmVCASeCQ+Q8l/fBNw8UqBPyNU= -github.com/drone/drone-ui v2.2.1+incompatible/go.mod h1:NBtVWW7NNJpD9+huMD/5TAE1db2nrEh0i35/9Rf1MPI= -github.com/drone/drone-ui v2.3.0+incompatible h1:iFNrvqF/huhKCmGFH80FapiIF4JscNkCfT9lSnm4+Is= -github.com/drone/drone-ui v2.3.0+incompatible/go.mod h1:NBtVWW7NNJpD9+huMD/5TAE1db2nrEh0i35/9Rf1MPI= github.com/drone/drone-ui v2.3.1+incompatible h1:JYOer5JRttNjXGJhJnH/ELksr3ORO/OWZS19VdlGtro= github.com/drone/drone-ui v2.3.1+incompatible/go.mod h1:NBtVWW7NNJpD9+huMD/5TAE1db2nrEh0i35/9Rf1MPI= github.com/drone/drone-yaml v1.2.4-0.20200326192514-6f4d6dfb39e4 h1:XsstoCeXC2t8lA9OLTdoFwckaptqahxwjCWsenySfX8= @@ -350,7 +346,6 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= diff --git a/handler/api/card/create.go b/handler/api/card/create.go index 5f17b71c..a59d0999 100644 --- a/handler/api/card/create.go +++ b/handler/api/card/create.go @@ -19,11 +19,6 @@ import ( "github.com/go-chi/chi" ) -type cardInput struct { - Schema string `json:"schema"` - Data json.RawMessage `json:"data"` -} - // HandleCreate returns an http.HandlerFunc that processes http // requests to create a new card. func HandleCreate( @@ -57,7 +52,7 @@ func HandleCreate( return } - in := new(cardInput) + in := new(core.CardInput) err = json.NewDecoder(r.Body).Decode(in) if err != nil { render.BadRequest(w, err) diff --git a/operator/manager/manager.go b/operator/manager/manager.go index af725702..f20e51e7 100644 --- a/operator/manager/manager.go +++ b/operator/manager/manager.go @@ -18,6 +18,7 @@ import ( "bytes" "context" "io" + "io/ioutil" "time" "github.com/drone/drone-yaml/yaml/converter" @@ -65,10 +66,10 @@ type ( // After signals the build step is complete. After(ctx context.Context, step *core.Step) error - // Before signals the build stage is about to start. + // BeforeAll signals the build stage is about to start. BeforeAll(ctx context.Context, stage *core.Stage) error - // After signals the build stage is complete. + // AfterAll signals the build stage is complete. AfterAll(ctx context.Context, stage *core.Stage) error // Watch watches for build cancellation requests. @@ -82,6 +83,9 @@ type ( // UploadBytes uploads the full logs UploadBytes(ctx context.Context, step int64, b []byte) error + + // UploadCard creates a new card + UploadCard(ctx context.Context, step int64, input *core.CardInput) error } // Request provides filters when requesting a pending @@ -101,6 +105,7 @@ type ( // New returns a new Manager. func New( builds core.BuildStore, + cards core.CardStore, config core.ConfigService, converter core.ConvertService, events core.Pubsub, @@ -120,6 +125,7 @@ func New( ) BuildManager { return &Manager{ Builds: builds, + Cards: cards, Config: config, Converter: converter, Events: events, @@ -143,6 +149,7 @@ func New( // can more easily interact with the server. type Manager struct { Builds core.BuildStore + Cards core.CardStore Config core.ConfigService Converter core.ConvertService Events core.Pubsub @@ -535,3 +542,16 @@ func (m *Manager) UploadBytes(ctx context.Context, step int64, data []byte) erro } return err } + +// UploadCard creates card for step. +func (m *Manager) UploadCard(ctx context.Context, stepId int64, input *core.CardInput) error { + data := ioutil.NopCloser( + bytes.NewBuffer(input.Data), + ) + err := m.Cards.Create(ctx, stepId, data) + if err != nil { + logger := logrus.WithError(err) + logger.Warnln("manager: cannot create card") + } + return nil +} diff --git a/operator/manager/rpc/client.go b/operator/manager/rpc/client.go index 817a9d20..0cf5f118 100644 --- a/operator/manager/rpc/client.go +++ b/operator/manager/rpc/client.go @@ -9,6 +9,7 @@ package rpc import ( "context" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -208,6 +209,10 @@ func (s *Client) UploadBytes(ctx context.Context, step int64, data []byte) error return s.upload(noContext, endpoint, data) } +func (s *Client) UploadCard(ctx context.Context, step int64, input *core.CardInput) error { + return errors.New("rpc upload card not supported") +} + func (s *Client) send(ctx context.Context, path string, in, out interface{}) error { // Source a buffer from a pool. The agent may generate a // large number of small requests for log entries. This will diff --git a/operator/manager/rpc2/handler.go b/operator/manager/rpc2/handler.go index f901af22..e015615d 100644 --- a/operator/manager/rpc2/handler.go +++ b/operator/manager/rpc2/handler.go @@ -29,6 +29,7 @@ import ( "github.com/go-chi/chi" "github.com/drone/drone/core" + "github.com/drone/drone/handler/api/render" "github.com/drone/drone/operator/manager" "github.com/drone/drone/store/shared/db" ) @@ -270,6 +271,31 @@ func HandleLogUpload(m manager.BuildManager) http.HandlerFunc { } } +// HandleCardUpload returns an http.HandlerFunc that accepts an +// http.Request to upload and persist a card for a pipeline step. +// +// POST /rpc/v2/step/{step}/card +func HandleCardUpload(m manager.BuildManager) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + step, _ := strconv.ParseInt( + chi.URLParam(r, "step"), 10, 64) + + in := new(core.CardInput) + err := json.NewDecoder(r.Body).Decode(in) + if err != nil { + render.BadRequest(w, err) + return + } + + err = m.UploadCard(noContext, step, in) + if err != nil { + writeError(w, err) + } else { + writeOK(w) + } + } +} + // write a 200 Status OK to the response body. func writeJSON(w http.ResponseWriter, v interface{}) { json.NewEncoder(w).Encode(v) diff --git a/operator/manager/rpc2/server.go b/operator/manager/rpc2/server.go index d34fccbb..f5d587f3 100644 --- a/operator/manager/rpc2/server.go +++ b/operator/manager/rpc2/server.go @@ -37,6 +37,7 @@ func NewServer(manager manager.BuildManager, secret string) Server { r.Post("/build/{build}/watch", HandleWatch(manager)) r.Post("/step/{step}/logs/batch", HandleLogBatch(manager)) r.Post("/step/{step}/logs/upload", HandleLogUpload(manager)) + r.Post("/step/{step}/card", HandleCardUpload(manager)) return Server(r) } @@ -55,4 +56,3 @@ func authorization(token string) func(http.Handler) http.Handler { }) } } -