Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3149 from drone/feature/dron-101-cards
Feature/dron 101 cards
- Loading branch information
Showing
28 changed files
with
1,870 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
Oops, something went wrong.