Skip to content

Commit

Permalink
Merge pull request #3149 from drone/feature/dron-101-cards
Browse files Browse the repository at this point in the history
Feature/dron 101 cards
  • Loading branch information
bradrydzewski committed Oct 1, 2021
2 parents 42c5f4d + a69573b commit 3c39e54
Show file tree
Hide file tree
Showing 28 changed files with 1,870 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Taskfile.yml
Expand Up @@ -79,6 +79,7 @@ tasks:
- cmd: go test -count=1 github.com/drone/drone/store/batch
- cmd: go test -count=1 github.com/drone/drone/store/batch2
- cmd: go test -count=1 github.com/drone/drone/store/build
- cmd: go test -count=1 github.com/drone/drone/store/card
- cmd: go test -count=1 github.com/drone/drone/store/cron
- cmd: go test -count=1 github.com/drone/drone/store/logs
- cmd: go test -count=1 github.com/drone/drone/store/perm
Expand Down Expand Up @@ -113,6 +114,7 @@ tasks:
- cmd: go test -count=1 github.com/drone/drone/store/batch
- cmd: go test -count=1 github.com/drone/drone/store/batch2
- cmd: go test -count=1 github.com/drone/drone/store/build
- cmd: go test -count=1 github.com/drone/drone/store/card
- cmd: go test -count=1 github.com/drone/drone/store/cron
- cmd: go test -count=1 github.com/drone/drone/store/logs
- cmd: go test -count=1 github.com/drone/drone/store/perm
Expand Down
2 changes: 2 additions & 0 deletions cmd/drone-server/inject_store.go
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/drone/drone/store/batch"
"github.com/drone/drone/store/batch2"
"github.com/drone/drone/store/build"
"github.com/drone/drone/store/card"
"github.com/drone/drone/store/cron"
"github.com/drone/drone/store/logs"
"github.com/drone/drone/store/perm"
Expand Down Expand Up @@ -50,6 +51,7 @@ var storeSet = wire.NewSet(
provideBatchStore,
// batch.New,
cron.New,
card.New,
perm.New,
secret.New,
global.New,
Expand Down
6 changes: 4 additions & 2 deletions cmd/drone-server/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions core/card.go
@@ -0,0 +1,64 @@
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package core

import (
"context"
"io"

"github.com/drone/drone/handler/api/errors"
)

var (
errCardStepInvalid = errors.New("No Step ID Provided")
errCardBuildInvalid = errors.New("No Build ID Provided")
errCardSchemaInvalid = errors.New("No Card Schema Has Been Provided")
)

type Card struct {
Id int64 `json:"id,omitempty"`
Build int64 `json:"build,omitempty"`
Stage int64 `json:"stage,omitempty"`
Step int64 `json:"step,omitempty"`
Schema string `json:"schema,omitempty"`
}

type CardData struct {
Id int64 `json:"id,omitempty"`
Data []byte `json:"card_data"`
}

// CardStore manages repository cards.
type CardStore interface {
FindByBuild(ctx context.Context, build int64) ([]*Card, error)
Find(ctx context.Context, step int64) (*Card, error)
FindData(ctx context.Context, id int64) (io.ReadCloser, error)
Create(ctx context.Context, card *Card, data io.ReadCloser) error
Delete(ctx context.Context, id int64) error
}

// Validate validates the required fields and formats.
func (c *Card) Validate() error {
switch {
case c.Step == 0:
return errCardStepInvalid
case c.Build == 0:
return errCardBuildInvalid
case len(c.Schema) == 0:
return errCardSchemaInvalid
default:
return nil
}
}
16 changes: 16 additions & 0 deletions handler/api/api.go
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/drone/drone/handler/api/auth"
"github.com/drone/drone/handler/api/badge"
globalbuilds "github.com/drone/drone/handler/api/builds"
"github.com/drone/drone/handler/api/card"
"github.com/drone/drone/handler/api/ccmenu"
"github.com/drone/drone/handler/api/events"
"github.com/drone/drone/handler/api/queue"
Expand Down Expand Up @@ -63,6 +64,7 @@ var corsOpts = cors.Options{
func New(
builds core.BuildStore,
commits core.CommitService,
card core.CardStore,
cron core.CronStore,
events core.Pubsub,
globals core.GlobalSecretStore,
Expand Down Expand Up @@ -92,6 +94,7 @@ func New(
) Server {
return Server{
Builds: builds,
Card: card,
Cron: cron,
Commits: commits,
Events: events,
Expand Down Expand Up @@ -125,6 +128,7 @@ func New(
// Server is a http.Handler which exposes drone functionality over HTTP.
type Server struct {
Builds core.BuildStore
Card core.CardStore
Cron core.CronStore
Commits core.CommitService
Events core.Pubsub
Expand Down Expand Up @@ -285,6 +289,18 @@ func (s Server) Handler() http.Handler {
acl.CheckAdminAccess(),
).Delete("/{member}", collabs.HandleDelete(s.Users, s.Repos, s.Perms))
})

r.Route("/cards", func(r chi.Router) {
r.Get("/{build}", card.HandleFindAll(s.Builds, s.Card, s.Repos))
r.Get("/{build}/{stage}/{step}", card.HandleFind(s.Builds, s.Card, s.Stages, s.Steps, s.Repos))
r.Get("/{build}/{stage}/{step}/json", card.HandleFindData(s.Builds, s.Card, s.Stages, s.Steps, s.Repos))
r.With(
acl.CheckAdminAccess(),
).Post("/{build}/{stage}/{step}", card.HandleCreate(s.Builds, s.Card, s.Stages, s.Steps, s.Repos))
r.With(
acl.CheckAdminAccess(),
).Delete("/{build}/{stage}/{step}", card.HandleDelete(s.Builds, s.Card, s.Stages, s.Steps, s.Repos))
})
})
})

Expand Down
113 changes: 113 additions & 0 deletions handler/api/card/create.go
@@ -0,0 +1,113 @@
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.

// +build !oss

package card

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"

"github.com/drone/drone/core"
"github.com/drone/drone/handler/api/render"

"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(
buildStore core.BuildStore,
cardStore core.CardStore,
stageStore core.StageStore,
stepStore core.StepStore,
repoStore core.RepositoryStore,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
namespace = chi.URLParam(r, "owner")
name = chi.URLParam(r, "name")
)

buildNumber, err := strconv.ParseInt(chi.URLParam(r, "build"), 10, 64)
if err != nil {
render.BadRequest(w, err)
return
}

stageNumber, err := strconv.Atoi(chi.URLParam(r, "stage"))
if err != nil {
render.BadRequest(w, err)
return
}

stepNumber, err := strconv.Atoi(chi.URLParam(r, "step"))
if err != nil {
render.BadRequest(w, err)
return
}

in := new(cardInput)
err = json.NewDecoder(r.Body).Decode(in)
if err != nil {
render.BadRequest(w, err)
return
}

repo, err := repoStore.FindName(r.Context(), namespace, name)
if err != nil {
render.NotFound(w, err)
return
}
build, err := buildStore.FindNumber(r.Context(), repo.ID, buildNumber)
if err != nil {
render.NotFound(w, err)
return
}
stage, err := stageStore.FindNumber(r.Context(), build.ID, stageNumber)
if err != nil {
render.NotFound(w, err)
return
}
step, err := stepStore.FindNumber(r.Context(), stage.ID, stepNumber)
if err != nil {
render.NotFound(w, err)
return
}

c := &core.Card{
Build: build.ID,
Stage: stage.ID,
Step: step.ID,
Schema: in.Schema,
}

err = c.Validate()
if err != nil {
render.BadRequest(w, err)
return
}

data := ioutil.NopCloser(
bytes.NewBuffer([]byte(in.Data)),
)

err = cardStore.Create(r.Context(), c, data)
if err != nil {
render.InternalError(w, err)
return
}

render.JSON(w, c, 200)
}
}

0 comments on commit 3c39e54

Please sign in to comment.