mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-02-13 09:55:14 +00:00
Simplify store interfaces (#3437)
Use `store.Store` interface if possible.
This commit is contained in:
parent
9b0c4e4e3c
commit
cb3efd2cd9
27 changed files with 69 additions and 420 deletions
|
@ -24,11 +24,6 @@ var (
|
||||||
errEnvironValueInvalid = errors.New("invalid Environment Variable Value")
|
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.
|
// Environ represents an environment variable.
|
||||||
type Environ struct {
|
type Environ struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
|
@ -15,14 +15,6 @@
|
||||||
|
|
||||||
package model
|
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.
|
// Perm defines a repository permission for an individual user.
|
||||||
type Perm struct {
|
type Perm struct {
|
||||||
UserID int64 `json:"-" xorm:"UNIQUE(s) INDEX NOT NULL 'perm_user_id'"`
|
UserID int64 `json:"-" xorm:"UNIQUE(s) INDEX NOT NULL 'perm_user_id'"`
|
||||||
|
|
|
@ -63,10 +63,6 @@ func (p Pipeline) IsMultiPipeline() bool {
|
||||||
return len(p.Workflows) > 1
|
return len(p.Workflows) > 1
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdatePipelineStore interface {
|
|
||||||
UpdatePipeline(*Pipeline) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type PipelineOptions struct {
|
type PipelineOptions struct {
|
||||||
Branch string `json:"branch"`
|
Branch string `json:"branch"`
|
||||||
Variables map[string]string `json:"variables"`
|
Variables map[string]string `json:"variables"`
|
||||||
|
|
|
@ -26,15 +26,6 @@ var (
|
||||||
errRegistryPasswordInvalid = errors.New("invalid registry password")
|
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.
|
// Registry represents a docker registry with credentials.
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
ID int64 `json:"id" xorm:"pk autoincr 'registry_id'"`
|
ID int64 `json:"id" xorm:"pk autoincr 'registry_id'"`
|
||||||
|
|
|
@ -14,12 +14,6 @@
|
||||||
|
|
||||||
package model
|
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.
|
// ServerConfig represents a key-value pair for storing server configurations.
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Key string `json:"key" xorm:"pk"`
|
Key string `json:"key" xorm:"pk"`
|
||||||
|
|
|
@ -15,16 +15,6 @@
|
||||||
|
|
||||||
package model
|
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
|
// Different ways to handle failure states
|
||||||
const (
|
const (
|
||||||
FailureIgnore = "ignore"
|
FailureIgnore = "ignore"
|
||||||
|
@ -49,10 +39,6 @@ type Step struct {
|
||||||
Type StepType `json:"type,omitempty" xorm:"step_type"`
|
Type StepType `json:"type,omitempty" xorm:"step_type"`
|
||||||
} // @name Step
|
} // @name Step
|
||||||
|
|
||||||
type UpdateStepStore interface {
|
|
||||||
StepUpdate(*Step) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// TableName return database table name for xorm
|
// TableName return database table name for xorm
|
||||||
func (Step) TableName() string {
|
func (Step) TableName() string {
|
||||||
return "steps"
|
return "steps"
|
||||||
|
|
|
@ -19,13 +19,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TaskStore defines storage for scheduled Tasks.
|
|
||||||
type TaskStore interface {
|
|
||||||
TaskList() ([]*Task, error)
|
|
||||||
TaskInsert(*Task) error
|
|
||||||
TaskDelete(string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Task defines scheduled pipeline Task.
|
// Task defines scheduled pipeline Task.
|
||||||
type Task struct {
|
type Task struct {
|
||||||
ID string `json:"id" xorm:"PK UNIQUE 'task_id'"`
|
ID string `json:"id" xorm:"PK UNIQUE 'task_id'"`
|
||||||
|
|
|
@ -32,10 +32,6 @@ type Workflow struct {
|
||||||
Children []*Step `json:"children,omitempty" xorm:"-"`
|
Children []*Step `json:"children,omitempty" xorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateWorkflowStore interface {
|
|
||||||
WorkflowUpdate(*Workflow) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// TableName return database table name for xorm
|
// TableName return database table name for xorm
|
||||||
func (Workflow) TableName() string {
|
func (Workflow) TableName() string {
|
||||||
return "workflows"
|
return "workflows"
|
||||||
|
|
|
@ -20,15 +20,16 @@ import (
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"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.Status = model.StatusRunning
|
||||||
pipeline.Started = started
|
pipeline.Started = started
|
||||||
return &pipeline, store.UpdatePipeline(&pipeline)
|
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 != "" {
|
if reviewer != "" {
|
||||||
pipeline.Reviewer = reviewer
|
pipeline.Reviewer = reviewer
|
||||||
pipeline.Reviewed = time.Now().Unix()
|
pipeline.Reviewed = time.Now().Unix()
|
||||||
|
@ -37,20 +38,20 @@ func UpdateToStatusPending(store model.UpdatePipelineStore, pipeline model.Pipel
|
||||||
return &pipeline, store.UpdatePipeline(&pipeline)
|
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.Reviewer = reviewer
|
||||||
pipeline.Status = model.StatusDeclined
|
pipeline.Status = model.StatusDeclined
|
||||||
pipeline.Reviewed = time.Now().Unix()
|
pipeline.Reviewed = time.Now().Unix()
|
||||||
return &pipeline, store.UpdatePipeline(&pipeline)
|
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.Status = status
|
||||||
pipeline.Finished = stopped
|
pipeline.Finished = stopped
|
||||||
return &pipeline, store.UpdatePipeline(&pipeline)
|
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.Errors = errors.GetPipelineErrors(err)
|
||||||
pipeline.Status = model.StatusError
|
pipeline.Status = model.StatusError
|
||||||
pipeline.Started = time.Now().Unix()
|
pipeline.Started = time.Now().Unix()
|
||||||
|
@ -58,7 +59,7 @@ func UpdateToStatusError(store model.UpdatePipelineStore, pipeline model.Pipelin
|
||||||
return &pipeline, store.UpdatePipeline(&pipeline)
|
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.Status = model.StatusKilled
|
||||||
pipeline.Finished = time.Now().Unix()
|
pipeline.Finished = time.Now().Unix()
|
||||||
return &pipeline, store.UpdatePipeline(&pipeline)
|
return &pipeline, store.UpdatePipeline(&pipeline)
|
|
@ -21,20 +21,23 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"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/model"
|
||||||
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
||||||
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/mocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockUpdatePipelineStore struct{}
|
func mockStorePipeline(t *testing.T) store.Store {
|
||||||
|
s := mocks.NewStore(t)
|
||||||
func (m *mockUpdatePipelineStore) UpdatePipeline(_ *model.Pipeline) error {
|
s.On("UpdatePipeline", mock.Anything).Return(nil)
|
||||||
return nil
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateToStatusRunning(t *testing.T) {
|
func TestUpdateToStatusRunning(t *testing.T) {
|
||||||
t.Parallel()
|
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.Equal(t, model.StatusRunning, pipeline.Status)
|
||||||
assert.EqualValues(t, 1, pipeline.Started)
|
assert.EqualValues(t, 1, pipeline.Started)
|
||||||
}
|
}
|
||||||
|
@ -44,7 +47,7 @@ func TestUpdateToStatusPending(t *testing.T) {
|
||||||
|
|
||||||
now := time.Now().Unix()
|
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, model.StatusPending, pipeline.Status)
|
||||||
assert.Equal(t, "Reviewer", pipeline.Reviewer)
|
assert.Equal(t, "Reviewer", pipeline.Reviewer)
|
||||||
|
@ -56,7 +59,7 @@ func TestUpdateToStatusDeclined(t *testing.T) {
|
||||||
|
|
||||||
now := time.Now().Unix()
|
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, model.StatusDeclined, pipeline.Status)
|
||||||
assert.Equal(t, "Reviewer", pipeline.Reviewer)
|
assert.Equal(t, "Reviewer", pipeline.Reviewer)
|
||||||
|
@ -66,7 +69,7 @@ func TestUpdateToStatusDeclined(t *testing.T) {
|
||||||
func TestUpdateToStatusToDone(t *testing.T) {
|
func TestUpdateToStatusToDone(t *testing.T) {
|
||||||
t.Parallel()
|
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.Equal(t, model.StatusValue("status"), pipeline.Status)
|
||||||
assert.EqualValues(t, 1, pipeline.Finished)
|
assert.EqualValues(t, 1, pipeline.Finished)
|
||||||
|
@ -77,7 +80,7 @@ func TestUpdateToStatusError(t *testing.T) {
|
||||||
|
|
||||||
now := time.Now().Unix()
|
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.Len(t, pipeline.Errors, 1)
|
||||||
assert.Equal(t, "[generic] this is an error", pipeline.Errors[0].Error())
|
assert.Equal(t, "[generic] this is an error", pipeline.Errors[0].Error())
|
||||||
|
@ -92,7 +95,7 @@ func TestUpdateToStatusKilled(t *testing.T) {
|
||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
pipeline, _ := UpdateToStatusKilled(&mockUpdatePipelineStore{}, model.Pipeline{})
|
pipeline, _ := UpdateToStatusKilled(mockStorePipeline(t), model.Pipeline{})
|
||||||
|
|
||||||
assert.Equal(t, model.StatusKilled, pipeline.Status)
|
assert.Equal(t, model.StatusKilled, pipeline.Status)
|
||||||
assert.LessOrEqual(t, now, pipeline.Finished)
|
assert.LessOrEqual(t, now, pipeline.Finished)
|
|
@ -20,9 +20,10 @@ import (
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"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 {
|
if state.Exited {
|
||||||
step.Stopped = state.Finished
|
step.Stopped = state.Finished
|
||||||
step.ExitCode = state.ExitCode
|
step.ExitCode = state.ExitCode
|
||||||
|
@ -41,13 +42,13 @@ func UpdateStepStatus(store model.UpdateStepStore, step *model.Step, state rpc.S
|
||||||
return store.StepUpdate(step)
|
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.Started = state.Started
|
||||||
step.State = model.StatusRunning
|
step.State = model.StatusRunning
|
||||||
return &step, store.StepUpdate(&step)
|
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
|
step.State = model.StatusSkipped
|
||||||
if step.Started != 0 {
|
if step.Started != 0 {
|
||||||
step.State = model.StatusSuccess // for daemons that are killed
|
step.State = model.StatusSuccess // for daemons that are killed
|
||||||
|
@ -56,7 +57,7 @@ func UpdateStepToStatusSkipped(store model.UpdateStepStore, step model.Step, sto
|
||||||
return &step, store.StepUpdate(&step)
|
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.Stopped = state.Finished
|
||||||
step.Error = state.Error
|
step.Error = state.Error
|
||||||
step.ExitCode = state.ExitCode
|
step.ExitCode = state.ExitCode
|
||||||
|
@ -71,7 +72,7 @@ func UpdateStepStatusToDone(store model.UpdateStepStore, step model.Step, state
|
||||||
return &step, store.StepUpdate(&step)
|
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.State = model.StatusKilled
|
||||||
step.Stopped = time.Now().Unix()
|
step.Stopped = time.Now().Unix()
|
||||||
if step.Started == 0 {
|
if step.Started == 0 {
|
||||||
|
|
|
@ -20,15 +20,18 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"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 mockUpdateStepStore struct{}
|
func mockStoreStep(t *testing.T) store.Store {
|
||||||
|
s := mocks.NewStore(t)
|
||||||
func (m *mockUpdateStepStore) StepUpdate(_ *model.Step) error {
|
s.On("StepUpdate", mock.Anything).Return(nil)
|
||||||
return nil
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateStepStatusNotExited(t *testing.T) {
|
func TestUpdateStepStatusNotExited(t *testing.T) {
|
||||||
|
@ -46,7 +49,7 @@ func TestUpdateStepStatusNotExited(t *testing.T) {
|
||||||
Error: "not an error",
|
Error: "not an error",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, model.StatusRunning, step.State)
|
assert.EqualValues(t, model.StatusRunning, step.State)
|
||||||
assert.EqualValues(t, 42, step.Started)
|
assert.EqualValues(t, 42, step.Started)
|
||||||
|
@ -70,7 +73,7 @@ func TestUpdateStepStatusNotExitedButStopped(t *testing.T) {
|
||||||
Error: "not an error",
|
Error: "not an error",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, model.StatusKilled, step.State)
|
assert.EqualValues(t, model.StatusKilled, step.State)
|
||||||
assert.EqualValues(t, 42, step.Started)
|
assert.EqualValues(t, 42, step.Started)
|
||||||
|
@ -94,7 +97,7 @@ func TestUpdateStepStatusExited(t *testing.T) {
|
||||||
Error: "an error",
|
Error: "an error",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, model.StatusKilled, step.State)
|
assert.EqualValues(t, model.StatusKilled, step.State)
|
||||||
assert.EqualValues(t, 42, step.Started)
|
assert.EqualValues(t, 42, step.Started)
|
||||||
|
@ -117,7 +120,7 @@ func TestUpdateStepStatusExitedButNot137(t *testing.T) {
|
||||||
Error: "an error",
|
Error: "an error",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, model.StatusFailure, step.State)
|
assert.EqualValues(t, model.StatusFailure, step.State)
|
||||||
assert.EqualValues(t, 42, step.Started)
|
assert.EqualValues(t, 42, step.Started)
|
||||||
|
@ -138,7 +141,7 @@ func TestUpdateStepStatusExitedWithCode(t *testing.T) {
|
||||||
Error: "an error",
|
Error: "an error",
|
||||||
}
|
}
|
||||||
step := &model.Step{}
|
step := &model.Step{}
|
||||||
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, model.StatusFailure, step.State)
|
assert.Equal(t, model.StatusFailure, step.State)
|
||||||
|
@ -149,7 +152,7 @@ func TestUpdateStepPToStatusStarted(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
state := rpc.State{Started: int64(42)}
|
state := rpc.State{Started: int64(42)}
|
||||||
step, _ := UpdateStepToStatusStarted(&mockUpdateStepStore{}, model.Step{}, state)
|
step, _ := UpdateStepToStatusStarted(mockStoreStep(t), model.Step{}, state)
|
||||||
|
|
||||||
assert.Equal(t, model.StatusRunning, step.State)
|
assert.Equal(t, model.StatusRunning, step.State)
|
||||||
assert.EqualValues(t, 42, step.Started)
|
assert.EqualValues(t, 42, step.Started)
|
||||||
|
@ -158,7 +161,7 @@ func TestUpdateStepPToStatusStarted(t *testing.T) {
|
||||||
func TestUpdateStepToStatusSkipped(t *testing.T) {
|
func TestUpdateStepToStatusSkipped(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
step, _ := UpdateStepToStatusSkipped(&mockUpdateStepStore{}, model.Step{}, int64(1))
|
step, _ := UpdateStepToStatusSkipped(mockStoreStep(t), model.Step{}, int64(1))
|
||||||
|
|
||||||
assert.Equal(t, model.StatusSkipped, step.State)
|
assert.Equal(t, model.StatusSkipped, step.State)
|
||||||
assert.EqualValues(t, 0, step.Stopped)
|
assert.EqualValues(t, 0, step.Stopped)
|
||||||
|
@ -171,7 +174,7 @@ func TestUpdateStepToStatusSkippedButStarted(t *testing.T) {
|
||||||
Started: int64(42),
|
Started: int64(42),
|
||||||
}
|
}
|
||||||
|
|
||||||
step, _ = UpdateStepToStatusSkipped(&mockUpdateStepStore{}, *step, int64(1))
|
step, _ = UpdateStepToStatusSkipped(mockStoreStep(t), *step, int64(1))
|
||||||
|
|
||||||
assert.Equal(t, model.StatusSuccess, step.State)
|
assert.Equal(t, model.StatusSuccess, step.State)
|
||||||
assert.EqualValues(t, 1, step.Stopped)
|
assert.EqualValues(t, 1, step.Stopped)
|
||||||
|
@ -184,7 +187,7 @@ func TestUpdateStepStatusToDoneSkipped(t *testing.T) {
|
||||||
Finished: int64(34),
|
Finished: int64(34),
|
||||||
}
|
}
|
||||||
|
|
||||||
step, _ := UpdateStepStatusToDone(&mockUpdateStepStore{}, model.Step{}, state)
|
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
|
||||||
|
|
||||||
assert.Equal(t, model.StatusSkipped, step.State)
|
assert.Equal(t, model.StatusSkipped, step.State)
|
||||||
assert.EqualValues(t, 34, step.Stopped)
|
assert.EqualValues(t, 34, step.Stopped)
|
||||||
|
@ -200,7 +203,7 @@ func TestUpdateStepStatusToDoneSuccess(t *testing.T) {
|
||||||
Finished: int64(34),
|
Finished: int64(34),
|
||||||
}
|
}
|
||||||
|
|
||||||
step, _ := UpdateStepStatusToDone(&mockUpdateStepStore{}, model.Step{}, state)
|
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
|
||||||
|
|
||||||
assert.Equal(t, model.StatusSuccess, step.State)
|
assert.Equal(t, model.StatusSuccess, step.State)
|
||||||
assert.EqualValues(t, 34, step.Stopped)
|
assert.EqualValues(t, 34, step.Stopped)
|
||||||
|
@ -213,7 +216,7 @@ func TestUpdateStepStatusToDoneFailureWithError(t *testing.T) {
|
||||||
|
|
||||||
state := rpc.State{Error: "an error"}
|
state := rpc.State{Error: "an error"}
|
||||||
|
|
||||||
step, _ := UpdateStepStatusToDone(&mockUpdateStepStore{}, model.Step{}, state)
|
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
|
||||||
|
|
||||||
assert.Equal(t, model.StatusFailure, step.State)
|
assert.Equal(t, model.StatusFailure, step.State)
|
||||||
}
|
}
|
||||||
|
@ -223,7 +226,7 @@ func TestUpdateStepStatusToDoneFailureWithExitCode(t *testing.T) {
|
||||||
|
|
||||||
state := rpc.State{ExitCode: 43}
|
state := rpc.State{ExitCode: 43}
|
||||||
|
|
||||||
step, _ := UpdateStepStatusToDone(&mockUpdateStepStore{}, model.Step{}, state)
|
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
|
||||||
|
|
||||||
assert.Equal(t, model.StatusFailure, step.State)
|
assert.Equal(t, model.StatusFailure, step.State)
|
||||||
}
|
}
|
||||||
|
@ -233,7 +236,7 @@ func TestUpdateStepToStatusKilledStarted(t *testing.T) {
|
||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
step, _ := UpdateStepToStatusKilled(&mockUpdateStepStore{}, model.Step{})
|
step, _ := UpdateStepToStatusKilled(mockStoreStep(t), model.Step{})
|
||||||
|
|
||||||
assert.Equal(t, model.StatusKilled, step.State)
|
assert.Equal(t, model.StatusKilled, step.State)
|
||||||
assert.LessOrEqual(t, now, step.Stopped)
|
assert.LessOrEqual(t, now, step.Stopped)
|
||||||
|
@ -244,7 +247,7 @@ func TestUpdateStepToStatusKilledStarted(t *testing.T) {
|
||||||
func TestUpdateStepToStatusKilledNotStarted(t *testing.T) {
|
func TestUpdateStepToStatusKilledNotStarted(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
step, _ := UpdateStepToStatusKilled(&mockUpdateStepStore{}, model.Step{Started: int64(1)})
|
step, _ := UpdateStepToStatusKilled(mockStoreStep(t), model.Step{Started: int64(1)})
|
||||||
|
|
||||||
assert.EqualValues(t, 1, step.Started)
|
assert.EqualValues(t, 1, step.Started)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,20 +17,21 @@ package pipeline
|
||||||
import (
|
import (
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UpdateWorkflowToStatusStarted(store model.UpdateWorkflowStore, workflow model.Workflow, state rpc.State) (*model.Workflow, error) {
|
func UpdateWorkflowToStatusStarted(store store.Store, workflow model.Workflow, state rpc.State) (*model.Workflow, error) {
|
||||||
workflow.Started = state.Started
|
workflow.Started = state.Started
|
||||||
workflow.State = model.StatusRunning
|
workflow.State = model.StatusRunning
|
||||||
return &workflow, store.WorkflowUpdate(&workflow)
|
return &workflow, store.WorkflowUpdate(&workflow)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateWorkflowToStatusSkipped(store model.UpdateWorkflowStore, workflow model.Workflow) (*model.Workflow, error) {
|
func UpdateWorkflowToStatusSkipped(store store.Store, workflow model.Workflow) (*model.Workflow, error) {
|
||||||
workflow.State = model.StatusSkipped
|
workflow.State = model.StatusSkipped
|
||||||
return &workflow, store.WorkflowUpdate(&workflow)
|
return &workflow, store.WorkflowUpdate(&workflow)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateWorkflowStatusToDone(store model.UpdateWorkflowStore, workflow model.Workflow, state rpc.State) (*model.Workflow, error) {
|
func UpdateWorkflowStatusToDone(store store.Store, workflow model.Workflow, state rpc.State) (*model.Workflow, error) {
|
||||||
workflow.Stopped = state.Finished
|
workflow.Stopped = state.Finished
|
||||||
workflow.Error = state.Error
|
workflow.Error = state.Error
|
||||||
if state.Started == 0 {
|
if state.Started == 0 {
|
||||||
|
|
|
@ -22,11 +22,12 @@ import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WithTaskStore returns a queue that is backed by the TaskStore. This
|
// WithTaskStore returns a queue that is backed by the TaskStore. This
|
||||||
// ensures the task Queue can be restored when the system starts.
|
// ensures the task Queue can be restored when the system starts.
|
||||||
func WithTaskStore(q Queue, s model.TaskStore) Queue {
|
func WithTaskStore(q Queue, s store.Store) Queue {
|
||||||
tasks, _ := s.TaskList()
|
tasks, _ := s.TaskList()
|
||||||
if err := q.PushAtOnce(context.Background(), tasks); err != nil {
|
if err := q.PushAtOnce(context.Background(), tasks); err != nil {
|
||||||
log.Error().Err(err).Msg("PushAtOnce failed")
|
log.Error().Err(err).Msg("PushAtOnce failed")
|
||||||
|
@ -36,7 +37,7 @@ func WithTaskStore(q Queue, s model.TaskStore) Queue {
|
||||||
|
|
||||||
type persistentQueue struct {
|
type persistentQueue struct {
|
||||||
Queue
|
Queue
|
||||||
store model.TaskStore
|
store store.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push pushes a task to the tail of this queue.
|
// Push pushes a task to the tail of this queue.
|
||||||
|
|
|
@ -16,14 +16,15 @@ package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
type db struct {
|
type db struct {
|
||||||
store model.RegistryStore
|
store store.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new local registry service.
|
// New returns a new local registry service.
|
||||||
func NewDB(store model.RegistryStore) Service {
|
func NewDB(store store.Store) Service {
|
||||||
return &db{store}
|
return &db{store}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,14 +16,15 @@ package secret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
type db struct {
|
type db struct {
|
||||||
store model.SecretStore
|
store store.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDB returns a new local secret service.
|
// NewDB returns a new local secret service.
|
||||||
func NewDB(store model.SecretStore) Service {
|
func NewDB(store store.Store) Service {
|
||||||
return &db{store: store}
|
return &db{store: store}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,18 +69,6 @@ func (s storage) ConfigPersist(conf *model.Config) (*model.Config, error) {
|
||||||
return conf, sess.Commit()
|
return conf, sess.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s storage) ConfigFindApproved(config *model.Config) (bool, error) {
|
|
||||||
return s.engine.Table("pipelines").Select("pipelines.pipeline_id").
|
|
||||||
Join("INNER", "pipeline_config", "pipelines.pipeline_id = pipeline_config.pipeline_id").
|
|
||||||
Where(builder.Eq{"pipelines.pipeline_repo_id": config.RepoID, "pipeline_config.config_id": config.ID}.
|
|
||||||
And(builder.NotIn("pipelines.pipeline_status", model.StatusBlocked, model.StatusPending))).
|
|
||||||
Exist()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s storage) ConfigCreate(config *model.Config) error {
|
|
||||||
return s.configCreate(s.engine.NewSession(), config)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s storage) configCreate(sess *xorm.Session, config *model.Config) error {
|
func (s storage) configCreate(sess *xorm.Session, config *model.Config) error {
|
||||||
// should never happen but just in case
|
// should never happen but just in case
|
||||||
if config.Name == "" {
|
if config.Name == "" {
|
||||||
|
|
|
@ -46,7 +46,9 @@ func TestConfig(t *testing.T) {
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
Name: name,
|
Name: name,
|
||||||
}
|
}
|
||||||
assert.NoError(t, store.ConfigCreate(config))
|
|
||||||
|
_, err := store.ConfigPersist(config)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
pipeline := &model.Pipeline{
|
pipeline := &model.Pipeline{
|
||||||
RepoID: repo.ID,
|
RepoID: repo.ID,
|
||||||
|
@ -71,118 +73,6 @@ func TestConfig(t *testing.T) {
|
||||||
assert.Equal(t, config.ID, loaded[0].ID)
|
assert.Equal(t, config.ID, loaded[0].ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigApproved(t *testing.T) {
|
|
||||||
store, closer := newTestStore(t, new(model.Config), new(model.PipelineConfig), new(model.Pipeline), new(model.Repo))
|
|
||||||
defer closer()
|
|
||||||
|
|
||||||
repo := &model.Repo{
|
|
||||||
UserID: 1,
|
|
||||||
FullName: "bradrydzewski/test",
|
|
||||||
Owner: "bradrydzewski",
|
|
||||||
Name: "test",
|
|
||||||
}
|
|
||||||
assert.NoError(t, store.CreateRepo(repo))
|
|
||||||
|
|
||||||
var (
|
|
||||||
pipelineBlocked = &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusBlocked,
|
|
||||||
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
||||||
}
|
|
||||||
pipelinePending = &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusPending,
|
|
||||||
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
||||||
}
|
|
||||||
pipelineRunning = &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusRunning,
|
|
||||||
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
assert.NoError(t, store.CreatePipeline(pipelineBlocked))
|
|
||||||
assert.NoError(t, store.CreatePipeline(pipelinePending))
|
|
||||||
conf := &model.Config{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Data: data,
|
|
||||||
Hash: hash,
|
|
||||||
Name: name,
|
|
||||||
}
|
|
||||||
assert.NoError(t, store.ConfigCreate(conf))
|
|
||||||
pipelineConfig := &model.PipelineConfig{
|
|
||||||
ConfigID: conf.ID,
|
|
||||||
PipelineID: pipelineBlocked.ID,
|
|
||||||
}
|
|
||||||
assert.NoError(t, store.PipelineConfigCreate(pipelineConfig))
|
|
||||||
|
|
||||||
approved, err := store.ConfigFindApproved(conf)
|
|
||||||
if !assert.NoError(t, err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
assert.False(t, approved, "want config not approved when blocked or pending.")
|
|
||||||
|
|
||||||
assert.NoError(t, store.CreatePipeline(pipelineRunning))
|
|
||||||
conf2 := &model.Config{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Data: data,
|
|
||||||
Hash: "xxx",
|
|
||||||
Name: "xxx",
|
|
||||||
}
|
|
||||||
assert.NoError(t, store.ConfigCreate(conf2))
|
|
||||||
pipelineConfig2 := &model.PipelineConfig{
|
|
||||||
ConfigID: conf2.ID,
|
|
||||||
PipelineID: pipelineRunning.ID,
|
|
||||||
}
|
|
||||||
assert.NoError(t, store.PipelineConfigCreate(pipelineConfig2))
|
|
||||||
|
|
||||||
approved, err = store.ConfigFindApproved(conf2)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, approved)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConfigCreate(t *testing.T) {
|
|
||||||
store, closer := newTestStore(t, new(model.Config), new(model.Step), new(model.Pipeline), new(model.Repo))
|
|
||||||
defer closer()
|
|
||||||
|
|
||||||
// fail due to missing name
|
|
||||||
assert.Error(t, store.ConfigCreate(
|
|
||||||
&model.Config{
|
|
||||||
RepoID: 2,
|
|
||||||
Data: data,
|
|
||||||
Hash: hash,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
|
|
||||||
// fail due to missing hash
|
|
||||||
assert.Error(t, store.ConfigCreate(
|
|
||||||
&model.Config{
|
|
||||||
RepoID: 2,
|
|
||||||
Data: data,
|
|
||||||
Name: name,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
|
|
||||||
assert.NoError(t, store.ConfigCreate(
|
|
||||||
&model.Config{
|
|
||||||
RepoID: 2,
|
|
||||||
Data: data,
|
|
||||||
Hash: hash,
|
|
||||||
Name: name,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
|
|
||||||
// fail due to duplicate sha
|
|
||||||
assert.Error(t, store.ConfigCreate(
|
|
||||||
&model.Config{
|
|
||||||
RepoID: 2,
|
|
||||||
Data: data,
|
|
||||||
Hash: hash,
|
|
||||||
Name: name,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConfigPersist(t *testing.T) {
|
func TestConfigPersist(t *testing.T) {
|
||||||
store, closer := newTestStore(t, new(model.Config))
|
store, closer := newTestStore(t, new(model.Config))
|
||||||
defer closer()
|
defer closer()
|
||||||
|
|
|
@ -15,8 +15,6 @@
|
||||||
package datastore
|
package datastore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,25 +23,6 @@ func (s storage) LogFind(step *model.Step) ([]*model.LogEntry, error) {
|
||||||
return logEntries, s.engine.Asc("id").Where("step_id = ?", step.ID).Find(&logEntries)
|
return logEntries, s.engine.Asc("id").Where("step_id = ?", step.ID).Find(&logEntries)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s storage) LogSave(step *model.Step, logEntries []*model.LogEntry) error {
|
|
||||||
sess := s.engine.NewSession()
|
|
||||||
defer sess.Close()
|
|
||||||
if err := sess.Begin(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, logEntry := range logEntries {
|
|
||||||
if logEntry.StepID != step.ID {
|
|
||||||
return fmt.Errorf("got a log-entry with step id '%d' but expected '%d'", logEntry.StepID, step.ID)
|
|
||||||
}
|
|
||||||
if _, err := sess.Insert(logEntry); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sess.Commit()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s storage) LogAppend(logEntry *model.LogEntry) error {
|
func (s storage) LogAppend(logEntry *model.LogEntry) error {
|
||||||
_, err := s.engine.Insert(logEntry)
|
_, err := s.engine.Insert(logEntry)
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -45,8 +45,9 @@ func TestLogCreateFindDelete(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// first insert should just work
|
for _, logEntry := range logEntries {
|
||||||
assert.NoError(t, store.LogSave(&step, logEntries))
|
assert.NoError(t, store.LogAppend(logEntry))
|
||||||
|
}
|
||||||
|
|
||||||
// we want to find our inserted logs
|
// we want to find our inserted logs
|
||||||
_logEntries, err := store.LogFind(&step)
|
_logEntries, err := store.LogFind(&step)
|
||||||
|
@ -82,7 +83,9 @@ func TestLogAppend(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.NoError(t, store.LogSave(&step, logEntries))
|
for _, logEntry := range logEntries {
|
||||||
|
assert.NoError(t, store.LogAppend(logEntry))
|
||||||
|
}
|
||||||
|
|
||||||
logEntry := &model.LogEntry{
|
logEntry := &model.LogEntry{
|
||||||
StepID: step.ID,
|
StepID: step.ID,
|
||||||
|
|
|
@ -72,17 +72,6 @@ func (s storage) permUpsert(sess *xorm.Session, perm *model.Perm) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s storage) PermDelete(perm *model.Perm) error {
|
|
||||||
return wrapDelete(s.engine.Where(userIDAndRepoIDCond(perm)).Delete(new(model.Perm)))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s storage) PermFlush(user *model.User, before int64) error {
|
|
||||||
_, err := s.engine.
|
|
||||||
Where("perm_user_id = ? AND perm_synced < ?", user.ID, before).
|
|
||||||
Delete(new(model.Perm))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// userPushOrAdminCondition return condition where user must have push or admin rights
|
// userPushOrAdminCondition return condition where user must have push or admin rights
|
||||||
// if used make sure to have permission table ("perms") joined
|
// if used make sure to have permission table ("perms") joined
|
||||||
func userPushOrAdminCondition(userID int64) builder.Cond {
|
func userPushOrAdminCondition(userID int64) builder.Cond {
|
||||||
|
|
|
@ -110,37 +110,3 @@ func TestPermUpsert(t *testing.T) {
|
||||||
assert.True(t, perm.Push)
|
assert.True(t, perm.Push)
|
||||||
assert.True(t, perm.Admin)
|
assert.True(t, perm.Admin)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPermDelete(t *testing.T) {
|
|
||||||
store, closer := newTestStore(t, new(model.Repo), new(model.Perm), new(model.User))
|
|
||||||
defer closer()
|
|
||||||
|
|
||||||
user := &model.User{ID: 1}
|
|
||||||
repo := &model.Repo{
|
|
||||||
UserID: 1,
|
|
||||||
FullName: "bradrydzewski/test",
|
|
||||||
Owner: "bradrydzewski",
|
|
||||||
Name: "test",
|
|
||||||
ForgeRemoteID: "1",
|
|
||||||
}
|
|
||||||
assert.NoError(t, store.CreateRepo(repo))
|
|
||||||
|
|
||||||
err := store.PermUpsert(
|
|
||||||
&model.Perm{
|
|
||||||
UserID: user.ID,
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Repo: repo,
|
|
||||||
Pull: true,
|
|
||||||
Push: false,
|
|
||||||
Admin: false,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
perm, err := store.PermFind(user, repo)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
err = store.PermDelete(perm)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
_, err = store.PermFind(user, repo)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
|
@ -35,20 +35,6 @@ func (s storage) GetPipelineNumber(repo *model.Repo, num int64) (*model.Pipeline
|
||||||
).Get(pipeline))
|
).Get(pipeline))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s storage) GetPipelineRef(repo *model.Repo, ref string) (*model.Pipeline, error) {
|
|
||||||
pipeline := new(model.Pipeline)
|
|
||||||
return pipeline, wrapGet(s.engine.Where(
|
|
||||||
builder.Eq{"pipeline_repo_id": repo.ID, "pipeline_ref": ref},
|
|
||||||
).Get(pipeline))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s storage) GetPipelineCommit(repo *model.Repo, sha, branch string) (*model.Pipeline, error) {
|
|
||||||
pipeline := new(model.Pipeline)
|
|
||||||
return pipeline, wrapGet(s.engine.Where(
|
|
||||||
builder.Eq{"pipeline_repo_id": repo.ID, "pipeline_branch": branch, "pipeline_commit": sha},
|
|
||||||
).Get(pipeline))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s storage) GetPipelineLast(repo *model.Repo, branch string) (*model.Pipeline, error) {
|
func (s storage) GetPipelineLast(repo *model.Repo, branch string) (*model.Pipeline, error) {
|
||||||
pipeline := new(model.Pipeline)
|
pipeline := new(model.Pipeline)
|
||||||
return pipeline, wrapGet(s.engine.
|
return pipeline, wrapGet(s.engine.
|
||||||
|
|
|
@ -144,78 +144,6 @@ func TestPipelines(t *testing.T) {
|
||||||
g.Assert(pipeline2.Number).Equal(GetPipeline.Number)
|
g.Assert(pipeline2.Number).Equal(GetPipeline.Number)
|
||||||
})
|
})
|
||||||
|
|
||||||
g.It("Should Get a Pipeline by Ref", func() {
|
|
||||||
pipeline1 := &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusPending,
|
|
||||||
Ref: "refs/pull/5",
|
|
||||||
}
|
|
||||||
pipeline2 := &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusPending,
|
|
||||||
Ref: "refs/pull/6",
|
|
||||||
}
|
|
||||||
err1 := store.CreatePipeline(pipeline1, []*model.Step{}...)
|
|
||||||
g.Assert(err1).IsNil()
|
|
||||||
err2 := store.CreatePipeline(pipeline2, []*model.Step{}...)
|
|
||||||
g.Assert(err2).IsNil()
|
|
||||||
GetPipeline, err3 := store.GetPipelineRef(&model.Repo{ID: 1}, "refs/pull/6")
|
|
||||||
g.Assert(err3).IsNil()
|
|
||||||
g.Assert(pipeline2.ID).Equal(GetPipeline.ID)
|
|
||||||
g.Assert(pipeline2.RepoID).Equal(GetPipeline.RepoID)
|
|
||||||
g.Assert(pipeline2.Number).Equal(GetPipeline.Number)
|
|
||||||
g.Assert(pipeline2.Ref).Equal(GetPipeline.Ref)
|
|
||||||
})
|
|
||||||
|
|
||||||
g.It("Should Get a Pipeline by Ref", func() {
|
|
||||||
pipeline1 := &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusPending,
|
|
||||||
Ref: "refs/pull/5",
|
|
||||||
}
|
|
||||||
pipeline2 := &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusPending,
|
|
||||||
Ref: "refs/pull/6",
|
|
||||||
}
|
|
||||||
err1 := store.CreatePipeline(pipeline1, []*model.Step{}...)
|
|
||||||
g.Assert(err1).IsNil()
|
|
||||||
err2 := store.CreatePipeline(pipeline2, []*model.Step{}...)
|
|
||||||
g.Assert(err2).IsNil()
|
|
||||||
GetPipeline, err3 := store.GetPipelineRef(&model.Repo{ID: 1}, "refs/pull/6")
|
|
||||||
g.Assert(err3).IsNil()
|
|
||||||
g.Assert(pipeline2.ID).Equal(GetPipeline.ID)
|
|
||||||
g.Assert(pipeline2.RepoID).Equal(GetPipeline.RepoID)
|
|
||||||
g.Assert(pipeline2.Number).Equal(GetPipeline.Number)
|
|
||||||
g.Assert(pipeline2.Ref).Equal(GetPipeline.Ref)
|
|
||||||
})
|
|
||||||
|
|
||||||
g.It("Should Get a Pipeline by Commit", func() {
|
|
||||||
pipeline1 := &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusPending,
|
|
||||||
Branch: "main",
|
|
||||||
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
||||||
}
|
|
||||||
pipeline2 := &model.Pipeline{
|
|
||||||
RepoID: repo.ID,
|
|
||||||
Status: model.StatusPending,
|
|
||||||
Branch: "dev",
|
|
||||||
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
|
|
||||||
}
|
|
||||||
err1 := store.CreatePipeline(pipeline1, []*model.Step{}...)
|
|
||||||
g.Assert(err1).IsNil()
|
|
||||||
err2 := store.CreatePipeline(pipeline2, []*model.Step{}...)
|
|
||||||
g.Assert(err2).IsNil()
|
|
||||||
GetPipeline, err3 := store.GetPipelineCommit(&model.Repo{ID: 1}, pipeline2.Commit, pipeline2.Branch)
|
|
||||||
g.Assert(err3).IsNil()
|
|
||||||
g.Assert(pipeline2.ID).Equal(GetPipeline.ID)
|
|
||||||
g.Assert(pipeline2.RepoID).Equal(GetPipeline.RepoID)
|
|
||||||
g.Assert(pipeline2.Number).Equal(GetPipeline.Number)
|
|
||||||
g.Assert(pipeline2.Commit).Equal(GetPipeline.Commit)
|
|
||||||
g.Assert(pipeline2.Branch).Equal(GetPipeline.Branch)
|
|
||||||
})
|
|
||||||
|
|
||||||
g.It("Should Get the last Pipeline", func() {
|
g.It("Should Get the last Pipeline", func() {
|
||||||
pipeline1 := &model.Pipeline{
|
pipeline1 := &model.Pipeline{
|
||||||
RepoID: repo.ID,
|
RepoID: repo.ID,
|
||||||
|
|
|
@ -21,12 +21,6 @@ import (
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s storage) GetRedirection(fullName string) (*model.Redirection, error) {
|
|
||||||
sess := s.engine.NewSession()
|
|
||||||
defer sess.Close()
|
|
||||||
return s.getRedirection(sess, fullName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s storage) getRedirection(e *xorm.Session, fullName string) (*model.Redirection, error) {
|
func (s storage) getRedirection(e *xorm.Session, fullName string) (*model.Redirection, error) {
|
||||||
repo := new(model.Redirection)
|
repo := new(model.Redirection)
|
||||||
return repo, wrapGet(e.Where("repo_full_name = ?", fullName).Get(repo))
|
return repo, wrapGet(e.Where("repo_full_name = ?", fullName).Get(repo))
|
||||||
|
|
|
@ -20,26 +20,8 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/server/store/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetRedirection(t *testing.T) {
|
|
||||||
store, closer := newTestStore(t, new(model.Redirection))
|
|
||||||
defer closer()
|
|
||||||
|
|
||||||
redirection := &model.Redirection{
|
|
||||||
RepoID: 1,
|
|
||||||
FullName: "foo/bar",
|
|
||||||
}
|
|
||||||
assert.NoError(t, store.CreateRedirection(redirection))
|
|
||||||
redirectionFromStore, err := store.GetRedirection("foo/bar")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, redirectionFromStore)
|
|
||||||
assert.Equal(t, redirection.RepoID, redirectionFromStore.RepoID)
|
|
||||||
_, err = store.GetRedirection("foo/baz")
|
|
||||||
assert.ErrorIs(t, err, types.RecordNotExist)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateRedirection(t *testing.T) {
|
func TestCreateRedirection(t *testing.T) {
|
||||||
store, closer := newTestStore(t, new(model.Redirection))
|
store, closer := newTestStore(t, new(model.Redirection))
|
||||||
defer closer()
|
defer closer()
|
||||||
|
|
|
@ -61,8 +61,6 @@ type Store interface {
|
||||||
DeleteRepo(*model.Repo) error
|
DeleteRepo(*model.Repo) error
|
||||||
|
|
||||||
// Redirections
|
// Redirections
|
||||||
// GetRedirection returns the redirection for the given full repo name
|
|
||||||
GetRedirection(string) (*model.Redirection, error)
|
|
||||||
// CreateRedirection creates a redirection
|
// CreateRedirection creates a redirection
|
||||||
CreateRedirection(redirection *model.Redirection) error
|
CreateRedirection(redirection *model.Redirection) error
|
||||||
// HasRedirectionForRepo checks if there's a redirection for the given repo and full name
|
// HasRedirectionForRepo checks if there's a redirection for the given repo and full name
|
||||||
|
@ -73,10 +71,6 @@ type Store interface {
|
||||||
GetPipeline(int64) (*model.Pipeline, error)
|
GetPipeline(int64) (*model.Pipeline, error)
|
||||||
// GetPipelineNumber gets a pipeline by number.
|
// GetPipelineNumber gets a pipeline by number.
|
||||||
GetPipelineNumber(*model.Repo, int64) (*model.Pipeline, error)
|
GetPipelineNumber(*model.Repo, int64) (*model.Pipeline, error)
|
||||||
// GetPipelineRef gets a pipeline by its ref.
|
|
||||||
GetPipelineRef(*model.Repo, string) (*model.Pipeline, error)
|
|
||||||
// GetPipelineCommit gets a pipeline by its commit sha.
|
|
||||||
GetPipelineCommit(*model.Repo, string, string) (*model.Pipeline, error)
|
|
||||||
// GetPipelineLast gets the last pipeline for the branch.
|
// GetPipelineLast gets the last pipeline for the branch.
|
||||||
GetPipelineLast(*model.Repo, string) (*model.Pipeline, error)
|
GetPipelineLast(*model.Repo, string) (*model.Pipeline, error)
|
||||||
// GetPipelineLastBefore gets the last pipeline before pipeline number N.
|
// GetPipelineLastBefore gets the last pipeline before pipeline number N.
|
||||||
|
@ -107,14 +101,10 @@ type Store interface {
|
||||||
// Permissions
|
// Permissions
|
||||||
PermFind(user *model.User, repo *model.Repo) (*model.Perm, error)
|
PermFind(user *model.User, repo *model.Repo) (*model.Perm, error)
|
||||||
PermUpsert(perm *model.Perm) error
|
PermUpsert(perm *model.Perm) error
|
||||||
PermDelete(perm *model.Perm) error
|
|
||||||
PermFlush(user *model.User, before int64) error
|
|
||||||
|
|
||||||
// Configs
|
// Configs
|
||||||
ConfigsForPipeline(pipelineID int64) ([]*model.Config, error)
|
ConfigsForPipeline(pipelineID int64) ([]*model.Config, error)
|
||||||
ConfigPersist(*model.Config) (*model.Config, error)
|
ConfigPersist(*model.Config) (*model.Config, error)
|
||||||
ConfigFindApproved(*model.Config) (bool, error)
|
|
||||||
ConfigCreate(*model.Config) error
|
|
||||||
PipelineConfigCreate(*model.PipelineConfig) error
|
PipelineConfigCreate(*model.PipelineConfig) error
|
||||||
|
|
||||||
// Secrets
|
// Secrets
|
||||||
|
@ -147,7 +137,6 @@ type Store interface {
|
||||||
|
|
||||||
// Logs
|
// Logs
|
||||||
LogFind(*model.Step) ([]*model.LogEntry, error)
|
LogFind(*model.Step) ([]*model.LogEntry, error)
|
||||||
LogSave(*model.Step, []*model.LogEntry) error
|
|
||||||
LogAppend(logEntry *model.LogEntry) error
|
LogAppend(logEntry *model.LogEntry) error
|
||||||
LogDelete(*model.Step) error
|
LogDelete(*model.Step) error
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue