Add StepFinished to log service (#5530)

This commit is contained in:
qwerty287
2025-09-24 13:41:03 +02:00
committed by GitHub
parent 2f3c4a28c1
commit e9c545e25b
12 changed files with 2908 additions and 118 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.9
// protoc v6.31.1
// protoc v6.32.0
// source: woodpecker.proto
package proto
+1 -1
View File
@@ -16,7 +16,7 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.5.1
// - protoc v6.31.1
// - protoc v6.32.0
// source: woodpecker.proto
package proto
+4
View File
@@ -188,6 +188,10 @@ func (s *RPC) Update(c context.Context, strWorkflowID string, state rpc.StepStat
log.Error().Err(err).Msg("rpc.update: cannot update step")
}
if state.Exited {
server.Config.Services.LogStore.StepFinished(step)
}
if currentPipeline.Workflows, err = s.store.WorkflowGetTree(currentPipeline); err != nil {
log.Error().Err(err).Msg("cannot build tree from step list")
return err
-33
View File
@@ -16,8 +16,6 @@
package pipeline
import (
"time"
"go.woodpecker-ci.org/woodpecker/v3/pipeline"
"go.woodpecker-ci.org/woodpecker/v3/pipeline/rpc"
"go.woodpecker-ci.org/woodpecker/v3/server/model"
@@ -43,12 +41,6 @@ func UpdateStepStatus(store store.Store, step *model.Step, state rpc.StepState)
return store.StepUpdate(step)
}
func UpdateStepToStatusStarted(store store.Store, step model.Step, state rpc.StepState) (*model.Step, error) {
step.Started = state.Started
step.State = model.StatusRunning
return &step, store.StepUpdate(&step)
}
func UpdateStepToStatusSkipped(store store.Store, step model.Step, finished int64) (*model.Step, error) {
step.State = model.StatusSkipped
if step.Started != 0 {
@@ -57,28 +49,3 @@ func UpdateStepToStatusSkipped(store store.Store, step model.Step, finished int6
}
return &step, store.StepUpdate(&step)
}
func UpdateStepStatusToDone(store store.Store, step model.Step, state rpc.StepState) (*model.Step, error) {
step.Finished = state.Finished
step.Error = state.Error
step.ExitCode = state.ExitCode
if state.Started == 0 {
step.State = model.StatusSkipped
} else {
step.State = model.StatusSuccess
}
if step.ExitCode != 0 || step.Error != "" {
step.State = model.StatusFailure
}
return &step, store.StepUpdate(&step)
}
func UpdateStepToStatusKilled(store store.Store, step model.Step) (*model.Step, error) {
step.State = model.StatusKilled
step.Finished = time.Now().Unix()
if step.Started == 0 {
step.Started = step.Finished
}
step.ExitCode = pipeline.ExitCodeKilled
return &step, store.StepUpdate(&step)
}
-83
View File
@@ -17,7 +17,6 @@ package pipeline
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -149,16 +148,6 @@ func TestUpdateStepStatusExitedWithCode(t *testing.T) {
assert.Equal(t, 1, step.ExitCode)
}
func TestUpdateStepToStatusStarted(t *testing.T) {
t.Parallel()
state := rpc.StepState{Started: int64(42)}
step, _ := UpdateStepToStatusStarted(mockStoreStep(t), model.Step{}, state)
assert.Equal(t, model.StatusRunning, step.State)
assert.EqualValues(t, 42, step.Started)
}
func TestUpdateStepToStatusSkipped(t *testing.T) {
t.Parallel()
@@ -180,75 +169,3 @@ func TestUpdateStepToStatusSkippedButStarted(t *testing.T) {
assert.Equal(t, model.StatusSuccess, step.State)
assert.EqualValues(t, 1, step.Finished)
}
func TestUpdateStepStatusToDoneSkipped(t *testing.T) {
t.Parallel()
state := rpc.StepState{
Finished: int64(34),
}
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
assert.Equal(t, model.StatusSkipped, step.State)
assert.EqualValues(t, 34, step.Finished)
assert.Empty(t, step.Error)
assert.Equal(t, 0, step.ExitCode)
}
func TestUpdateStepStatusToDoneSuccess(t *testing.T) {
t.Parallel()
state := rpc.StepState{
Started: int64(42),
Finished: int64(34),
}
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
assert.Equal(t, model.StatusSuccess, step.State)
assert.EqualValues(t, 34, step.Finished)
assert.Empty(t, step.Error)
assert.Equal(t, 0, step.ExitCode)
}
func TestUpdateStepStatusToDoneFailureWithError(t *testing.T) {
t.Parallel()
state := rpc.StepState{Error: "an error"}
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
assert.Equal(t, model.StatusFailure, step.State)
}
func TestUpdateStepStatusToDoneFailureWithExitCode(t *testing.T) {
t.Parallel()
state := rpc.StepState{ExitCode: 43}
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
assert.Equal(t, model.StatusFailure, step.State)
}
func TestUpdateStepToStatusKilledStarted(t *testing.T) {
t.Parallel()
now := time.Now().Unix()
step, _ := UpdateStepToStatusKilled(mockStoreStep(t), model.Step{})
assert.Equal(t, model.StatusKilled, step.State)
assert.LessOrEqual(t, now, step.Finished)
assert.Equal(t, step.Finished, step.Started)
assert.Equal(t, 137, step.ExitCode)
}
func TestUpdateStepToStatusKilledNotStarted(t *testing.T) {
t.Parallel()
step, _ := UpdateStepToStatusKilled(mockStoreStep(t), model.Step{Started: int64(1)})
assert.EqualValues(t, 1, step.Started)
}
+2
View File
@@ -102,3 +102,5 @@ func (l logStore) LogAppend(step *model.Step, logEntries []*model.LogEntry) erro
func (l logStore) LogDelete(step *model.Step) error {
return os.Remove(l.filePath(step.ID))
}
func (l logStore) StepFinished(_ *model.Step) {}
+40
View File
@@ -205,3 +205,43 @@ func (_c *MockService_LogFind_Call) RunAndReturn(run func(step *model.Step) ([]*
_c.Call.Return(run)
return _c
}
// StepFinished provides a mock function for the type MockService
func (_mock *MockService) StepFinished(step *model.Step) {
_mock.Called(step)
return
}
// MockService_StepFinished_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'StepFinished'
type MockService_StepFinished_Call struct {
*mock.Call
}
// StepFinished is a helper method to define mock.On call
// - step *model.Step
func (_e *MockService_Expecter) StepFinished(step interface{}) *MockService_StepFinished_Call {
return &MockService_StepFinished_Call{Call: _e.mock.On("StepFinished", step)}
}
func (_c *MockService_StepFinished_Call) Run(run func(step *model.Step)) *MockService_StepFinished_Call {
_c.Call.Run(func(args mock.Arguments) {
var arg0 *model.Step
if args[0] != nil {
arg0 = args[0].(*model.Step)
}
run(
arg0,
)
})
return _c
}
func (_c *MockService_StepFinished_Call) Return() *MockService_StepFinished_Call {
_c.Call.Return()
return _c
}
func (_c *MockService_StepFinished_Call) RunAndReturn(run func(step *model.Step)) *MockService_StepFinished_Call {
_c.Run(run)
return _c
}
+1
View File
@@ -6,4 +6,5 @@ type Service interface {
LogFind(step *model.Step) ([]*model.LogEntry, error)
LogAppend(step *model.Step, logEntries []*model.LogEntry) error
LogDelete(step *model.Step) error
StepFinished(step *model.Step)
}
+2
View File
@@ -56,3 +56,5 @@ func logDelete(sess *xorm.Session, stepID int64) error {
_, err := sess.Where("step_id = ?", stepID).Delete(new(model.LogEntry))
return err
}
func (s storage) StepFinished(_ *model.Step) {}
+40
View File
@@ -5673,6 +5673,46 @@ func (_c *MockStore_StepFind_Call) RunAndReturn(run func(pipeline *model.Pipelin
return _c
}
// StepFinished provides a mock function for the type MockStore
func (_mock *MockStore) StepFinished(step *model.Step) {
_mock.Called(step)
return
}
// MockStore_StepFinished_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'StepFinished'
type MockStore_StepFinished_Call struct {
*mock.Call
}
// StepFinished is a helper method to define mock.On call
// - step *model.Step
func (_e *MockStore_Expecter) StepFinished(step interface{}) *MockStore_StepFinished_Call {
return &MockStore_StepFinished_Call{Call: _e.mock.On("StepFinished", step)}
}
func (_c *MockStore_StepFinished_Call) Run(run func(step *model.Step)) *MockStore_StepFinished_Call {
_c.Call.Run(func(args mock.Arguments) {
var arg0 *model.Step
if args[0] != nil {
arg0 = args[0].(*model.Step)
}
run(
arg0,
)
})
return _c
}
func (_c *MockStore_StepFinished_Call) Return() *MockStore_StepFinished_Call {
_c.Call.Return()
return _c
}
func (_c *MockStore_StepFinished_Call) RunAndReturn(run func(step *model.Step)) *MockStore_StepFinished_Call {
_c.Run(run)
return _c
}
// StepList provides a mock function for the type MockStore
func (_mock *MockStore) StepList(pipeline *model.Pipeline) ([]*model.Step, error) {
ret := _mock.Called(pipeline)
File diff suppressed because it is too large Load Diff
+1
View File
@@ -147,6 +147,7 @@ type Store interface {
LogFind(*model.Step) ([]*model.LogEntry, error)
LogAppend(*model.Step, []*model.LogEntry) error
LogDelete(*model.Step) error
StepFinished(*model.Step)
// Tasks
// TaskList TODO: paginate & opt filter