From 41b5fa6b557d5cb937690635306aea6749ee6c91 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Thu, 20 Oct 2022 15:28:21 +0800 Subject: [PATCH] fix: update step result --- go.mod | 1 + go.sum | 3 +- runtime/reporter.go | 82 +++++++++++++++++++++++++++++++-------------- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 4fb7716..381b762 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/avast/retry-go/v4 v4.1.0 github.com/bufbuild/connect-go v0.5.0 github.com/docker/docker v20.10.17+incompatible + github.com/google/go-cmp v0.5.8 github.com/joho/godotenv v1.4.0 github.com/kelseyhightower/envconfig v1.4.0 github.com/mattn/go-isatty v0.0.16 diff --git a/go.sum b/go.sum index bb611a3..4dcd689 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,6 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= gitea.com/gitea/act v0.0.0-20220922135643-52a5bba9e7fa h1:HHqlvfIvqFlny3sgJgAM1BYeLNr1uM4yXtvF7aAoYK8= gitea.com/gitea/act v0.0.0-20220922135643-52a5bba9e7fa/go.mod h1:9W/Nz16tjfnWp7O5DUo3EjZBnZFBI/5rlWstX4o7+hU= -gitea.com/gitea/proto-go v0.0.0-20221013073523-69d53451957a h1:WHNPcbDR2vw2a17Ml06+n4MC0UwsyD/F3WeVteaXWMI= -gitea.com/gitea/proto-go v0.0.0-20221013073523-69d53451957a/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= gitea.com/gitea/proto-go v0.0.0-20221014123629-9116865c883b h1:TSz7VRHfnM/5JwGPgIAjSlDIvcr4pTGfuRMtgMxttmg= gitea.com/gitea/proto-go v0.0.0-20221014123629-9116865c883b/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= @@ -406,6 +404,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-containerregistry v0.5.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= diff --git a/runtime/reporter.go b/runtime/reporter.go index be5c3e5..68def59 100644 --- a/runtime/reporter.go +++ b/runtime/reporter.go @@ -12,6 +12,8 @@ import ( "github.com/avast/retry-go/v4" "github.com/bufbuild/connect-go" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" log "github.com/sirupsen/logrus" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/timestamppb" @@ -59,7 +61,9 @@ func (r *Reporter) Fire(entry *log.Entry) error { timestamp := entry.Time if r.state.StartedAt == nil { - r.state.StartedAt = timestamppb.New(timestamp) + r.updateState(func() { + r.state.StartedAt = timestamppb.New(timestamp) + }) } var step *runnerv1.StepState @@ -71,8 +75,8 @@ func (r *Reporter) Fire(entry *log.Entry) error { if step == nil { if v, ok := entry.Data["jobResult"]; ok { - if v, ok := v.(string); ok { - if jobResult := r.parseResult(v); jobResult != runnerv1.Result_RESULT_UNSPECIFIED { + if jobResult, ok := r.parseResult(v); ok { + r.updateState(func() { r.state.Result = jobResult r.state.StoppedAt = timestamppb.New(timestamp) for _, s := range r.state.Steps { @@ -80,7 +84,7 @@ func (r *Reporter) Fire(entry *log.Entry) error { s.Result = runnerv1.Result_RESULT_CANCELLED } } - } + }) } } if !r.duringSteps() { @@ -90,28 +94,33 @@ func (r *Reporter) Fire(entry *log.Entry) error { } if step.StartedAt == nil { - step.StartedAt = timestamppb.New(timestamp) + r.updateState(func() { + step.StartedAt = timestamppb.New(timestamp) + }) } if v, ok := entry.Data["raw_output"]; ok { if rawOutput, ok := v.(bool); ok && rawOutput { - if step.LogLength == 0 { - step.LogIndex = int64(r.logOffset + len(r.logRows)) - } - step.LogLength++ + r.updateState(func() { + if step.LogLength == 0 { + step.LogIndex = int64(r.logOffset + len(r.logRows)) + } + step.LogLength++ + }) r.logRows = append(r.logRows, r.parseLogRow(entry)) return nil } } - log.Info(entry.Data) - if v, ok := entry.Data["stepResult"]; ok { - if v, ok := v.(string); ok { - if stepResult := r.parseResult(v); stepResult != runnerv1.Result_RESULT_UNSPECIFIED { + if stepResult, ok := r.parseResult(v); ok { + r.updateState(func() { + if step.LogLength == 0 { + step.LogIndex = int64(r.logOffset + len(r.logRows)) + } step.Result = stepResult step.StoppedAt = timestamppb.New(timestamp) - } + }) } } @@ -238,18 +247,25 @@ func (r *Reporter) duringSteps() bool { return true } -func (r *Reporter) parseResult(s string) runnerv1.Result { - switch s { - case "success": - return runnerv1.Result_RESULT_SUCCESS - case "failure": - return runnerv1.Result_RESULT_FAILURE - case "skipped": - return runnerv1.Result_RESULT_SKIPPED - case "cancelled": - return runnerv1.Result_RESULT_CANCELLED +var ( + stringToResult = map[string]runnerv1.Result{ + "success": runnerv1.Result_RESULT_SUCCESS, + "failure": runnerv1.Result_RESULT_FAILURE, + "skipped": runnerv1.Result_RESULT_SKIPPED, + "cancelled": runnerv1.Result_RESULT_CANCELLED, } - return runnerv1.Result_RESULT_UNSPECIFIED +) + +func (r *Reporter) parseResult(result interface{}) (runnerv1.Result, bool) { + str := "" + if v, ok := result.(string); ok { // for jobResult + str = v + } else if v, ok := result.(fmt.Stringer); ok { // for stepResult + str = v.String() + } + + ret, ok := stringToResult[str] + return ret, ok } func (r *Reporter) parseLogRow(entry *log.Entry) *runnerv1.LogRow { @@ -258,3 +274,19 @@ func (r *Reporter) parseLogRow(entry *log.Entry) *runnerv1.LogRow { Content: strings.TrimSuffix(entry.Message, "\r\n"), } } + +func (r *Reporter) updateState(update func()) { + if log.GetLevel() < log.TraceLevel { + update() + return + } + + before := proto.Clone(r.state) + update() + diff := cmp.Diff(before, r.state, cmpopts.IgnoreUnexported( + runnerv1.TaskState{}, + runnerv1.StepState{}, + timestamppb.Timestamp{}, + )) + log.Tracef("update state %d: %s", r.state.Id, diff) +}