Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten committed Jun 28, 2024
1 parent add130a commit c79dca1
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 195 deletions.
9 changes: 5 additions & 4 deletions agent/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func (r *Runner) createTracer(ctxMeta context.Context, logger zerolog.Logger, wo

stepState := rpc.StepState{
StepUUID: state.Pipeline.Step.UUID,
Exited: state.Process.Exited,
ExitCode: state.Process.ExitCode,
Started: time.Now().Unix(), // TODO: do not do this
Finished: time.Now().Unix(),
}
if state.Process.Exited {
stepState.Finished = time.Now().Unix()
}
if state.Process.Error != nil {
stepState.Error = state.Process.Error.Error()
Expand Down Expand Up @@ -69,11 +70,11 @@ func (r *Runner) createTracer(ctxMeta context.Context, logger zerolog.Logger, wo
state.Pipeline.Step.Environment["CI_MACHINE"] = r.hostname

state.Pipeline.Step.Environment["CI_PIPELINE_STATUS"] = "success"
state.Pipeline.Step.Environment["CI_PIPELINE_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
state.Pipeline.Step.Environment["CI_PIPELINE_STARTED"] = strconv.FormatInt(state.Pipeline.Started, 10)
state.Pipeline.Step.Environment["CI_PIPELINE_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)

state.Pipeline.Step.Environment["CI_STEP_STATUS"] = "success"
state.Pipeline.Step.Environment["CI_STEP_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
state.Pipeline.Step.Environment["CI_STEP_STARTED"] = strconv.FormatInt(state.Pipeline.Started, 10)
state.Pipeline.Step.Environment["CI_STEP_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)

state.Pipeline.Step.Environment["CI_SYSTEM_PLATFORM"] = runtime.GOOS + "/" + runtime.GOARCH
Expand Down
4 changes: 2 additions & 2 deletions pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type (
// Global state of the pipeline.
Pipeline struct {
// Pipeline time started
Time int64 `json:"time"`
Started int64 `json:"time"`
// Current pipeline step
Step *backend.Step `json:"step"`
// Current pipeline error state
Expand Down Expand Up @@ -147,7 +147,7 @@ func (r *Runtime) traceStep(processState *backend.State, err error, step *backen
}

state := new(State)
state.Pipeline.Time = r.started
state.Pipeline.Started = r.started
state.Pipeline.Step = step
state.Process = processState // empty
state.Pipeline.Error = r.err
Expand Down
1 change: 0 additions & 1 deletion pipeline/rpc/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type (
StepUUID string `json:"step_uuid"`
Started int64 `json:"started"`
Finished int64 `json:"finished"`
Exited bool `json:"exited"`
ExitCode int `json:"exit_code"`
Error string `json:"error"`
}
Expand Down
282 changes: 136 additions & 146 deletions pipeline/rpc/proto/woodpecker.pb.go

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions pipeline/rpc/proto/woodpecker.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ message StepState {
string step_uuid = 1;
int64 started = 2;
int64 finished = 3;
bool exited = 4;
int32 exit_code = 5;
string error = 6;
int32 exit_code = 4;
string error = 5;
}

message WorkflowState {
Expand Down
4 changes: 2 additions & 2 deletions pipeline/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ var DefaultTracer = TraceFunc(func(state *State) error {
return nil
}
state.Pipeline.Step.Environment["CI_PIPELINE_STATUS"] = "success"
state.Pipeline.Step.Environment["CI_PIPELINE_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
state.Pipeline.Step.Environment["CI_PIPELINE_STARTED"] = strconv.FormatInt(state.Pipeline.Started, 10)
state.Pipeline.Step.Environment["CI_PIPELINE_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)

state.Pipeline.Step.Environment["CI_STEP_STATUS"] = "success"
state.Pipeline.Step.Environment["CI_STEP_STARTED"] = strconv.FormatInt(state.Pipeline.Time, 10)
state.Pipeline.Step.Environment["CI_STEP_STARTED"] = strconv.FormatInt(state.Pipeline.Started, 10)
state.Pipeline.Step.Environment["CI_STEP_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10)

if state.Pipeline.Error != nil {
Expand Down
1 change: 0 additions & 1 deletion server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (s *WoodpeckerServer) Update(c context.Context, req *proto.UpdateRequest) (
StepUUID: req.GetState().GetStepUuid(),
Started: req.GetState().GetStarted(),
Finished: req.GetState().GetFinished(),
Exited: req.GetState().GetExited(),
Error: req.GetState().GetError(),
ExitCode: int(req.GetState().GetExitCode()),
}
Expand Down
2 changes: 1 addition & 1 deletion server/pipeline/step_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func UpdateStepStatus(store store.Store, step *model.Step, state rpc.StepState) error {
if state.Exited {
if state.Finished != 0 {
step.Finished = state.Finished
step.ExitCode = state.ExitCode
step.Error = state.Error
Expand Down
42 changes: 7 additions & 35 deletions server/pipeline/step_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@ func mockStoreStep(t *testing.T) store.Store {
return s
}

func TestUpdateStepStatusNotExited(t *testing.T) {
func TestUpdateStepStatusNotFinished(t *testing.T) {
t.Parallel()
// step in db before update
step := &model.Step{}

// advertised step status
state := rpc.StepState{
Started: int64(42),
Exited: false,
Started: int64(42),
Finished: int64(0),
// Dummy data
Finished: int64(1),
ExitCode: pipeline.ExitCodeKilled,
Error: "not an error",
}
Expand All @@ -59,31 +58,7 @@ func TestUpdateStepStatusNotExited(t *testing.T) {
assert.EqualValues(t, "", step.Error)
}

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

// step in db before update
step := &model.Step{Started: 42, Finished: 64, State: model.StatusKilled}

// advertised step status
state := rpc.StepState{
Exited: false,
// Dummy data
Finished: int64(1),
ExitCode: pipeline.ExitCodeKilled,
Error: "not an error",
}

err := UpdateStepStatus(mockStoreStep(t), step, state)
assert.NoError(t, err)
assert.EqualValues(t, model.StatusKilled, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 64, step.Finished)
assert.EqualValues(t, 0, step.ExitCode)
assert.EqualValues(t, "", step.Error)
}

func TestUpdateStepStatusExited(t *testing.T) {
func TestUpdateStepStatusFinished(t *testing.T) {
t.Parallel()

// step in db before update
Expand All @@ -92,7 +67,6 @@ func TestUpdateStepStatusExited(t *testing.T) {
// advertised step status
state := rpc.StepState{
Started: int64(42),
Exited: true,
Finished: int64(34),
ExitCode: pipeline.ExitCodeKilled,
Error: "an error",
Expand All @@ -107,7 +81,7 @@ func TestUpdateStepStatusExited(t *testing.T) {
assert.EqualValues(t, "an error", step.Error)
}

func TestUpdateStepStatusExitedButNot137(t *testing.T) {
func TestUpdateStepStatusFinishedButNot137(t *testing.T) {
t.Parallel()

// step in db before update
Expand All @@ -116,7 +90,6 @@ func TestUpdateStepStatusExitedButNot137(t *testing.T) {
// advertised step status
state := rpc.StepState{
Started: int64(42),
Exited: true,
Finished: int64(34),
Error: "an error",
}
Expand All @@ -130,13 +103,12 @@ func TestUpdateStepStatusExitedButNot137(t *testing.T) {
assert.EqualValues(t, "an error", step.Error)
}

func TestUpdateStepStatusExitedWithCode(t *testing.T) {
func TestUpdateStepStatusFinishedWithCode(t *testing.T) {
t.Parallel()

// advertised step status
state := rpc.StepState{
Started: int64(42),
Exited: true,
Finished: int64(34),
ExitCode: 1,
Error: "an error",
Expand All @@ -149,7 +121,7 @@ func TestUpdateStepStatusExitedWithCode(t *testing.T) {
assert.Equal(t, 1, step.ExitCode)
}

func TestUpdateStepPToStatusStarted(t *testing.T) {
func TestUpdateStepToStatusStarted(t *testing.T) {
t.Parallel()

state := rpc.StepState{Started: int64(42)}
Expand Down

0 comments on commit c79dca1

Please sign in to comment.