Skip to content

Commit

Permalink
Simplify store interfaces (#3437)
Browse files Browse the repository at this point in the history
Use `store.Store` interface if possible.
  • Loading branch information
qwerty287 authored Feb 25, 2024
1 parent 9b0c4e4 commit cb3efd2
Show file tree
Hide file tree
Showing 27 changed files with 69 additions and 420 deletions.
5 changes: 0 additions & 5 deletions server/model/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ var (
errEnvironValueInvalid = errors.New("invalid Environment Variable Value")
)

// EnvironStore persists environment information to storage.
type EnvironStore interface {
EnvironList(*Repo) ([]*Environ, error)
}

// Environ represents an environment variable.
type Environ struct {
Name string `json:"name"`
Expand Down
8 changes: 0 additions & 8 deletions server/model/perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@

package model

// PermStore persists repository permissions information to storage.
type PermStore interface {
PermFind(user *User, repo *Repo) (*Perm, error)
PermUpsert(perm *Perm) error
PermDelete(perm *Perm) error
PermFlush(user *User, before int64) error
}

// Perm defines a repository permission for an individual user.
type Perm struct {
UserID int64 `json:"-" xorm:"UNIQUE(s) INDEX NOT NULL 'perm_user_id'"`
Expand Down
4 changes: 0 additions & 4 deletions server/model/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func (p Pipeline) IsMultiPipeline() bool {
return len(p.Workflows) > 1
}

type UpdatePipelineStore interface {
UpdatePipeline(*Pipeline) error
}

type PipelineOptions struct {
Branch string `json:"branch"`
Variables map[string]string `json:"variables"`
Expand Down
9 changes: 0 additions & 9 deletions server/model/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ var (
errRegistryPasswordInvalid = errors.New("invalid registry password")
)

// RegistryStore persists registry information to storage.
type RegistryStore interface {
RegistryFind(*Repo, string) (*Registry, error)
RegistryList(*Repo, *ListOptions) ([]*Registry, error)
RegistryCreate(*Registry) error
RegistryUpdate(*Registry) error
RegistryDelete(repo *Repo, addr string) error
}

// Registry represents a docker registry with credentials.
type Registry struct {
ID int64 `json:"id" xorm:"pk autoincr 'registry_id'"`
Expand Down
6 changes: 0 additions & 6 deletions server/model/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

package model

// ServerConfigStore persists key-value pairs for storing server configurations.
type ServerConfigStore interface {
ServerConfigGet(key string) (string, error)
ServerConfigSet(key int64, value string) error
}

// ServerConfig represents a key-value pair for storing server configurations.
type ServerConfig struct {
Key string `json:"key" xorm:"pk"`
Expand Down
14 changes: 0 additions & 14 deletions server/model/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@

package model

// StepStore persists process information to storage.
type StepStore interface {
StepLoad(int64) (*Step, error)
StepFind(*Pipeline, int) (*Step, error)
StepChild(*Pipeline, int, string) (*Step, error)
StepList(*Pipeline) ([]*Step, error)
StepCreate([]*Step) error
StepUpdate(*Step) error
}

// Different ways to handle failure states
const (
FailureIgnore = "ignore"
Expand All @@ -49,10 +39,6 @@ type Step struct {
Type StepType `json:"type,omitempty" xorm:"step_type"`
} // @name Step

type UpdateStepStore interface {
StepUpdate(*Step) error
}

// TableName return database table name for xorm
func (Step) TableName() string {
return "steps"
Expand Down
7 changes: 0 additions & 7 deletions server/model/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ import (
"strings"
)

// TaskStore defines storage for scheduled Tasks.
type TaskStore interface {
TaskList() ([]*Task, error)
TaskInsert(*Task) error
TaskDelete(string) error
}

// Task defines scheduled pipeline Task.
type Task struct {
ID string `json:"id" xorm:"PK UNIQUE 'task_id'"`
Expand Down
4 changes: 0 additions & 4 deletions server/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ type Workflow struct {
Children []*Step `json:"children,omitempty" xorm:"-"`
}

type UpdateWorkflowStore interface {
WorkflowUpdate(*Workflow) error
}

// TableName return database table name for xorm
func (Workflow) TableName() string {
return "workflows"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import (

"go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
)

func UpdateToStatusRunning(store model.UpdatePipelineStore, pipeline model.Pipeline, started int64) (*model.Pipeline, error) {
func UpdateToStatusRunning(store store.Store, pipeline model.Pipeline, started int64) (*model.Pipeline, error) {
pipeline.Status = model.StatusRunning
pipeline.Started = started
return &pipeline, store.UpdatePipeline(&pipeline)
}

func UpdateToStatusPending(store model.UpdatePipelineStore, pipeline model.Pipeline, reviewer string) (*model.Pipeline, error) {
func UpdateToStatusPending(store store.Store, pipeline model.Pipeline, reviewer string) (*model.Pipeline, error) {
if reviewer != "" {
pipeline.Reviewer = reviewer
pipeline.Reviewed = time.Now().Unix()
Expand All @@ -37,28 +38,28 @@ func UpdateToStatusPending(store model.UpdatePipelineStore, pipeline model.Pipel
return &pipeline, store.UpdatePipeline(&pipeline)
}

func UpdateToStatusDeclined(store model.UpdatePipelineStore, pipeline model.Pipeline, reviewer string) (*model.Pipeline, error) {
func UpdateToStatusDeclined(store store.Store, pipeline model.Pipeline, reviewer string) (*model.Pipeline, error) {
pipeline.Reviewer = reviewer
pipeline.Status = model.StatusDeclined
pipeline.Reviewed = time.Now().Unix()
return &pipeline, store.UpdatePipeline(&pipeline)
}

func UpdateStatusToDone(store model.UpdatePipelineStore, pipeline model.Pipeline, status model.StatusValue, stopped int64) (*model.Pipeline, error) {
func UpdateStatusToDone(store store.Store, pipeline model.Pipeline, status model.StatusValue, stopped int64) (*model.Pipeline, error) {
pipeline.Status = status
pipeline.Finished = stopped
return &pipeline, store.UpdatePipeline(&pipeline)
}

func UpdateToStatusError(store model.UpdatePipelineStore, pipeline model.Pipeline, err error) (*model.Pipeline, error) {
func UpdateToStatusError(store store.Store, pipeline model.Pipeline, err error) (*model.Pipeline, error) {
pipeline.Errors = errors.GetPipelineErrors(err)
pipeline.Status = model.StatusError
pipeline.Started = time.Now().Unix()
pipeline.Finished = pipeline.Started
return &pipeline, store.UpdatePipeline(&pipeline)
}

func UpdateToStatusKilled(store model.UpdatePipelineStore, pipeline model.Pipeline) (*model.Pipeline, error) {
func UpdateToStatusKilled(store store.Store, pipeline model.Pipeline) (*model.Pipeline, error) {
pipeline.Status = model.StatusKilled
pipeline.Finished = time.Now().Unix()
return &pipeline, store.UpdatePipeline(&pipeline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
"go.woodpecker-ci.org/woodpecker/v2/server/store/mocks"
)

type mockUpdatePipelineStore struct{}

func (m *mockUpdatePipelineStore) UpdatePipeline(_ *model.Pipeline) error {
return nil
func mockStorePipeline(t *testing.T) store.Store {
s := mocks.NewStore(t)
s.On("UpdatePipeline", mock.Anything).Return(nil)
return s
}

func TestUpdateToStatusRunning(t *testing.T) {
t.Parallel()

pipeline, _ := UpdateToStatusRunning(&mockUpdatePipelineStore{}, model.Pipeline{}, int64(1))
pipeline, _ := UpdateToStatusRunning(mockStorePipeline(t), model.Pipeline{}, int64(1))
assert.Equal(t, model.StatusRunning, pipeline.Status)
assert.EqualValues(t, 1, pipeline.Started)
}
Expand All @@ -44,7 +47,7 @@ func TestUpdateToStatusPending(t *testing.T) {

now := time.Now().Unix()

pipeline, _ := UpdateToStatusPending(&mockUpdatePipelineStore{}, model.Pipeline{}, "Reviewer")
pipeline, _ := UpdateToStatusPending(mockStorePipeline(t), model.Pipeline{}, "Reviewer")

assert.Equal(t, model.StatusPending, pipeline.Status)
assert.Equal(t, "Reviewer", pipeline.Reviewer)
Expand All @@ -56,7 +59,7 @@ func TestUpdateToStatusDeclined(t *testing.T) {

now := time.Now().Unix()

pipeline, _ := UpdateToStatusDeclined(&mockUpdatePipelineStore{}, model.Pipeline{}, "Reviewer")
pipeline, _ := UpdateToStatusDeclined(mockStorePipeline(t), model.Pipeline{}, "Reviewer")

assert.Equal(t, model.StatusDeclined, pipeline.Status)
assert.Equal(t, "Reviewer", pipeline.Reviewer)
Expand All @@ -66,7 +69,7 @@ func TestUpdateToStatusDeclined(t *testing.T) {
func TestUpdateToStatusToDone(t *testing.T) {
t.Parallel()

pipeline, _ := UpdateStatusToDone(&mockUpdatePipelineStore{}, model.Pipeline{}, "status", int64(1))
pipeline, _ := UpdateStatusToDone(mockStorePipeline(t), model.Pipeline{}, "status", int64(1))

assert.Equal(t, model.StatusValue("status"), pipeline.Status)
assert.EqualValues(t, 1, pipeline.Finished)
Expand All @@ -77,7 +80,7 @@ func TestUpdateToStatusError(t *testing.T) {

now := time.Now().Unix()

pipeline, _ := UpdateToStatusError(&mockUpdatePipelineStore{}, model.Pipeline{}, errors.New("this is an error"))
pipeline, _ := UpdateToStatusError(mockStorePipeline(t), model.Pipeline{}, errors.New("this is an error"))

assert.Len(t, pipeline.Errors, 1)
assert.Equal(t, "[generic] this is an error", pipeline.Errors[0].Error())
Expand All @@ -92,7 +95,7 @@ func TestUpdateToStatusKilled(t *testing.T) {

now := time.Now().Unix()

pipeline, _ := UpdateToStatusKilled(&mockUpdatePipelineStore{}, model.Pipeline{})
pipeline, _ := UpdateToStatusKilled(mockStorePipeline(t), model.Pipeline{})

assert.Equal(t, model.StatusKilled, pipeline.Status)
assert.LessOrEqual(t, now, pipeline.Finished)
Expand Down
11 changes: 6 additions & 5 deletions server/pipeline/step_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (

"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
)

func UpdateStepStatus(store model.UpdateStepStore, step *model.Step, state rpc.State) error {
func UpdateStepStatus(store store.Store, step *model.Step, state rpc.State) error {
if state.Exited {
step.Stopped = state.Finished
step.ExitCode = state.ExitCode
Expand All @@ -41,13 +42,13 @@ func UpdateStepStatus(store model.UpdateStepStore, step *model.Step, state rpc.S
return store.StepUpdate(step)
}

func UpdateStepToStatusStarted(store model.UpdateStepStore, step model.Step, state rpc.State) (*model.Step, error) {
func UpdateStepToStatusStarted(store store.Store, step model.Step, state rpc.State) (*model.Step, error) {
step.Started = state.Started
step.State = model.StatusRunning
return &step, store.StepUpdate(&step)
}

func UpdateStepToStatusSkipped(store model.UpdateStepStore, step model.Step, stopped int64) (*model.Step, error) {
func UpdateStepToStatusSkipped(store store.Store, step model.Step, stopped int64) (*model.Step, error) {
step.State = model.StatusSkipped
if step.Started != 0 {
step.State = model.StatusSuccess // for daemons that are killed
Expand All @@ -56,7 +57,7 @@ func UpdateStepToStatusSkipped(store model.UpdateStepStore, step model.Step, sto
return &step, store.StepUpdate(&step)
}

func UpdateStepStatusToDone(store model.UpdateStepStore, step model.Step, state rpc.State) (*model.Step, error) {
func UpdateStepStatusToDone(store store.Store, step model.Step, state rpc.State) (*model.Step, error) {
step.Stopped = state.Finished
step.Error = state.Error
step.ExitCode = state.ExitCode
Expand All @@ -71,7 +72,7 @@ func UpdateStepStatusToDone(store model.UpdateStepStore, step model.Step, state
return &step, store.StepUpdate(&step)
}

func UpdateStepToStatusKilled(store model.UpdateStepStore, step model.Step) (*model.Step, error) {
func UpdateStepToStatusKilled(store store.Store, step model.Step) (*model.Step, error) {
step.State = model.StatusKilled
step.Stopped = time.Now().Unix()
if step.Started == 0 {
Expand Down
Loading

0 comments on commit cb3efd2

Please sign in to comment.