Skip to content

Commit

Permalink
Merge branch 'main' into feat-iota-event-matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Sep 15, 2023
2 parents e60a7b0 + 3829fd1 commit ce74b93
Show file tree
Hide file tree
Showing 19 changed files with 1,156 additions and 80 deletions.
3 changes: 3 additions & 0 deletions constants/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const (
// TableBuild defines the table type for the database builds table.
TableBuild = "builds"

// TableBuildExecutable defines the table type for the database build_executables table.
TableBuildExecutable = "build_executables"

// TableHook defines the table type for the database hooks table.
TableHook = "hooks"

Expand Down
167 changes: 167 additions & 0 deletions database/build_executable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package database

import (
"database/sql"
"encoding/base64"
"errors"

"github.com/go-vela/types/library"
)

var (
// ErrEmptyBuildExecutableBuildID defines the error type when a
// BuildExecutable type has an empty BuildID field provided.
ErrEmptyBuildExecutableBuildID = errors.New("empty build_executable build_id provided")
)

// BuildExecutable is the database representation of a BuildExecutable.
type BuildExecutable struct {
ID sql.NullInt64 `sql:"id"`
BuildID sql.NullInt64 `sql:"build_id"`
Data []byte `sql:"data"`
}

// Compress will manipulate the existing data for the
// BuildExecutable by compressing that data. This produces
// a significantly smaller amount of data that is
// stored in the system.
func (b *BuildExecutable) Compress(level int) error {
// compress the database BuildExecutable data
data, err := compress(level, b.Data)
if err != nil {
return err
}

// overwrite database BuildExecutable data with compressed BuildExecutable data
b.Data = data

return nil
}

// Decompress will manipulate the existing data for the
// BuildExecutable by decompressing that data. This allows us
// to have a significantly smaller amount of data that
// is stored in the system.
func (b *BuildExecutable) Decompress() error {
// decompress the database BuildExecutable data
data, err := decompress(b.Data)
if err != nil {
return err
}

// overwrite compressed BuildExecutable data with decompressed BuildExecutable data
b.Data = data

return nil
}

// Decrypt will manipulate the existing executable data by
// base64 decoding that value. Then, a AES-256 cipher
// block is created from the encryption key in order to
// decrypt the base64 decoded secret value.
func (b *BuildExecutable) Decrypt(key string) error {
dst := make([]byte, base64.StdEncoding.DecodedLen(len(b.Data)))

// base64 decode the encrypted repo hash
n, err := base64.StdEncoding.Decode(dst, b.Data)
if err != nil {
return err
}

dst = dst[:n]

// decrypt the base64 decoded executable data
decrypted, err := decrypt(key, dst)
if err != nil {
return err
}

// set the decrypted executable
b.Data = decrypted

return nil
}

// Encrypt will manipulate the existing build executable by
// creating a AES-256 cipher block from the encryption
// key in order to encrypt the build executable. Then, the
// build executable is base64 encoded for transport across
// network boundaries.
func (b *BuildExecutable) Encrypt(key string) error {
// encrypt the executable data
encrypted, err := encrypt(key, b.Data)
if err != nil {
return err
}

// base64 encode the encrypted executable to make it network safe
dst := make([]byte, base64.StdEncoding.EncodedLen(len(encrypted)))
base64.StdEncoding.Encode(dst, encrypted)

b.Data = dst

return nil
}

// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the BuildExecutable type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (b *BuildExecutable) Nullify() *BuildExecutable {
if b == nil {
return nil
}

// check if the ID field should be false
if b.ID.Int64 == 0 {
b.ID.Valid = false
}

// check if the BuildID field should be false
if b.BuildID.Int64 == 0 {
b.BuildID.Valid = false
}

return b
}

// ToLibrary converts the BuildExecutable type
// to a library BuildExecutable type.
func (b *BuildExecutable) ToLibrary() *library.BuildExecutable {
buildExecutable := new(library.BuildExecutable)

buildExecutable.SetID(b.ID.Int64)
buildExecutable.SetBuildID(b.BuildID.Int64)
buildExecutable.SetData(b.Data)

return buildExecutable
}

// Validate verifies the necessary fields for
// the BuildExecutable type are populated correctly.
func (b *BuildExecutable) Validate() error {
// verify the BuildID field is populated
if b.BuildID.Int64 <= 0 {
return ErrEmptyBuildExecutableBuildID
}

return nil
}

// BuildExecutableFromLibrary converts the library BuildExecutable type
// to a database BuildExecutable type.
func BuildExecutableFromLibrary(c *library.BuildExecutable) *BuildExecutable {
buildExecutable := &BuildExecutable{
ID: sql.NullInt64{Int64: c.GetID(), Valid: true},
BuildID: sql.NullInt64{Int64: c.GetBuildID(), Valid: true},
Data: c.GetData(),
}

return buildExecutable.Nullify()
}
457 changes: 457 additions & 0 deletions database/build_executable_test.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions database/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Schedule struct {
UpdatedAt sql.NullInt64 `sql:"updated_at"`
UpdatedBy sql.NullString `sql:"updated_by"`
ScheduledAt sql.NullInt64 `sql:"scheduled_at"`
Branch sql.NullString `sql:"branch"`
}

// ScheduleFromLibrary converts the library.Schedule type to a database Schedule type.
Expand All @@ -53,6 +54,7 @@ func ScheduleFromLibrary(s *library.Schedule) *Schedule {
UpdatedAt: sql.NullInt64{Int64: s.GetUpdatedAt(), Valid: true},
UpdatedBy: sql.NullString{String: s.GetUpdatedBy(), Valid: true},
ScheduledAt: sql.NullInt64{Int64: s.GetScheduledAt(), Valid: true},
Branch: sql.NullString{String: s.GetBranch(), Valid: true},
}

return schedule.Nullify()
Expand Down Expand Up @@ -87,6 +89,8 @@ func (s *Schedule) Nullify() *Schedule {
s.UpdatedBy.Valid = len(s.UpdatedBy.String) != 0
// check if the ScheduledAt field should be valid
s.ScheduledAt.Valid = s.ScheduledAt.Int64 != 0
// check if the Branch field should be valid
s.Branch.Valid = len(s.Branch.String) != 0

return s
}
Expand All @@ -104,6 +108,7 @@ func (s *Schedule) ToLibrary() *library.Schedule {
UpdatedAt: &s.UpdatedAt.Int64,
UpdatedBy: &s.UpdatedBy.String,
ScheduledAt: &s.ScheduledAt.Int64,
Branch: &s.Branch.String,
}
}

Expand Down
4 changes: 4 additions & 0 deletions database/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestDatabase_ScheduleFromLibrary(t *testing.T) {
s.SetUpdatedAt(time.Now().Add(time.Hour * 1).UTC().Unix())
s.SetUpdatedBy("user2")
s.SetScheduledAt(time.Now().Add(time.Hour * 2).UTC().Unix())
s.SetBranch("main")

want := testSchedule()

Expand Down Expand Up @@ -59,6 +60,7 @@ func TestDatabase_Schedule_Nullify(t *testing.T) {
UpdatedAt: sql.NullInt64{Int64: 0, Valid: false},
UpdatedBy: sql.NullString{String: "", Valid: false},
ScheduledAt: sql.NullInt64{Int64: 0, Valid: false},
Branch: sql.NullString{String: "", Valid: false},
},
},
{
Expand Down Expand Up @@ -90,6 +92,7 @@ func TestDatabase_Schedule_ToLibrary(t *testing.T) {
want.SetUpdatedAt(time.Now().Add(time.Hour * 1).UTC().Unix())
want.SetUpdatedBy("user2")
want.SetScheduledAt(time.Now().Add(time.Hour * 2).UTC().Unix())
want.SetBranch("main")

got := testSchedule().ToLibrary()
if !reflect.DeepEqual(got, want) {
Expand Down Expand Up @@ -176,5 +179,6 @@ func testSchedule() *Schedule {
UpdatedAt: sql.NullInt64{Int64: time.Now().Add(time.Hour * 1).UTC().Unix(), Valid: true},
UpdatedBy: sql.NullString{String: "user2", Valid: true},
ScheduledAt: sql.NullInt64{Int64: time.Now().Add(time.Hour * 2).UTC().Unix(), Valid: true},
Branch: sql.NullString{String: "main", Valid: true},
}
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module github.com/go-vela/types
go 1.19

require (
github.com/adhocore/gronx v1.6.3
github.com/adhocore/gronx v1.6.5
github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3
github.com/drone/envsubst v1.0.3
github.com/ghodss/yaml v1.0.0
github.com/lib/pq v1.10.9
github.com/microcosm-cc/bluemonday v1.0.24
github.com/microcosm-cc/bluemonday v1.0.25
)

require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/kr/pretty v0.2.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/net v0.12.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/adhocore/gronx v1.6.3 h1:bnm5vieTrY3QQPpsfB0hrAaeaHDpuZTUC2LLCVMLe9c=
github.com/adhocore/gronx v1.6.3/go.mod h1:7oUY1WAU8rEJWmAxXR2DN0JaO4gi9khSgKjiRypqteg=
github.com/adhocore/gronx v1.6.5 h1:/pryEagBKz3WqUgpgvtL51eBN2rJLXowuW7rpS+jrew=
github.com/adhocore/gronx v1.6.5/go.mod h1:7oUY1WAU8rEJWmAxXR2DN0JaO4gi9khSgKjiRypqteg=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3 h1:q+sMKdA6L8LyGVudTkpGoC73h6ak2iWSPFiFo/pFOU8=
Expand All @@ -19,10 +19,10 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg=
github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
15 changes: 6 additions & 9 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,26 @@ package types

import (
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
)

// ItemVersion allows the worker to detect items that were queued before an Vela server
// upgrade or downgrade, so it can handle such stale data gracefully.
// For example, the worker could fail a stale build or ask the server to recompile it.
// This is not a public API and is unrelated to the version key in pipeline yaml.
const ItemVersion uint64 = 1
const ItemVersion uint64 = 2

// Item is the queue representation of an item to publish to the queue.
type Item struct {
Build *library.Build `json:"build"`
Pipeline *pipeline.Build `json:"pipeline"`
Repo *library.Repo `json:"repo"`
User *library.User `json:"user"`
Build *library.Build `json:"build"`
Repo *library.Repo `json:"repo"`
User *library.User `json:"user"`
// The 0-value is the implicit ItemVersion for queued Items that pre-date adding the field.
ItemVersion uint64 `json:"item_version"`
}

// ToItem creates a queue item from a pipeline, build, repo and user.
func ToItem(p *pipeline.Build, b *library.Build, r *library.Repo, u *library.User) *Item {
// ToItem creates a queue item from a build, repo and user.
func ToItem(b *library.Build, r *library.Repo, u *library.User) *Item {
return &Item{
Pipeline: p,
Build: b,
Repo: r,
User: u,
Expand Down
43 changes: 1 addition & 42 deletions item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"

"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
)

func TestTypes_ToItem(t *testing.T) {
Expand Down Expand Up @@ -44,26 +43,6 @@ func TestTypes_ToItem(t *testing.T) {
Ref: &str,
BaseRef: &str,
}
p := &pipeline.Build{
Version: "v1",
Stages: pipeline.StageSlice{
&pipeline.Stage{
Name: str,
Steps: pipeline.ContainerSlice{
&pipeline.Container{
Image: "alpine",
Name: str,
},
},
},
},
Steps: pipeline.ContainerSlice{
&pipeline.Container{
Image: "alpine",
Name: str,
},
},
}
r := &library.Repo{
ID: &num64,
UserID: &num64,
Expand All @@ -88,26 +67,6 @@ func TestTypes_ToItem(t *testing.T) {
Admin: &booL,
}
want := &Item{
Pipeline: &pipeline.Build{
Version: "v1",
Stages: pipeline.StageSlice{
&pipeline.Stage{
Name: str,
Steps: pipeline.ContainerSlice{
&pipeline.Container{
Image: "alpine",
Name: str,
},
},
},
},
Steps: pipeline.ContainerSlice{
&pipeline.Container{
Image: "alpine",
Name: str,
},
},
},
Build: &library.Build{
ID: &num64,
RepoID: &num64,
Expand Down Expand Up @@ -159,7 +118,7 @@ func TestTypes_ToItem(t *testing.T) {
}

// run test
got := ToItem(p, b, r, u)
got := ToItem(b, r, u)

if !reflect.DeepEqual(got, want) {
t.Errorf("ToItem is %v, want %v", got, want)
Expand Down
Loading

0 comments on commit ce74b93

Please sign in to comment.