From 6f2222c6d086d1614c29ce2ff1eb71c48005cb06 Mon Sep 17 00:00:00 2001 From: wuhuizuo Date: Thu, 2 Nov 2023 19:14:10 +0800 Subject: [PATCH] refactor(cloudevents-server): add `reason` filed Signed-off-by: wuhuizuo --- cloudevents-server/client/main.go | 5 +- cloudevents-server/ent/client.go | 21 ++++- cloudevents-server/ent/migrate/schema.go | 1 + cloudevents-server/ent/mutation.go | 56 ++++++++++- cloudevents-server/ent/problemcaserun.go | 15 ++- .../ent/problemcaserun/problemcaserun.go | 10 ++ .../ent/problemcaserun/where.go | 93 +++++++++++++++---- .../ent/problemcaserun_create.go | 22 +++++ .../ent/problemcaserun_update.go | 28 ++++++ cloudevents-server/ent/runtime.go | 4 + cloudevents-server/ent/runtime/runtime.go | 4 +- .../ent/schema/problemcaserun.go | 1 + .../events/custom/testcaserun/testcaserun.go | 30 +++--- .../pkg/events/custom/testcaserun/types.go | 12 ++- 14 files changed, 259 insertions(+), 43 deletions(-) diff --git a/cloudevents-server/client/main.go b/cloudevents-server/client/main.go index eb3d1ff..1dfb2b5 100644 --- a/cloudevents-server/client/main.go +++ b/cloudevents-server/client/main.go @@ -3,16 +3,15 @@ package main import ( "context" "fmt" - "net/http" - "log" + "net/http" cloudevents "github.com/cloudevents/sdk-go/v2" cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" ) func main() { - ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:80/events") + ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost/events") p, err := cloudevents.NewHTTP() if err != nil { log.Fatalf("failed to create protocol: %s", err.Error()) diff --git a/cloudevents-server/ent/client.go b/cloudevents-server/ent/client.go index 7e6d0ac..f585d37 100644 --- a/cloudevents-server/ent/client.go +++ b/cloudevents-server/ent/client.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "log" + "reflect" "github.com/PingCAP-QE/ee-apps/cloudevents-server/ent/migrate" @@ -104,11 +105,14 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } +// ErrTxStarted is returned when trying to start a new transaction from a transactional client. +var ErrTxStarted = errors.New("ent: cannot start a transaction within a transaction") + // Tx returns a new transactional client. The provided context // is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { - return nil, errors.New("ent: cannot start a transaction within a transaction") + return nil, ErrTxStarted } tx, err := newTx(ctx, c.driver) if err != nil { @@ -220,6 +224,21 @@ func (c *ProblemCaseRunClient) CreateBulk(builders ...*ProblemCaseRunCreate) *Pr return &ProblemCaseRunCreateBulk{config: c.config, builders: builders} } +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *ProblemCaseRunClient) MapCreateBulk(slice any, setFunc func(*ProblemCaseRunCreate, int)) *ProblemCaseRunCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &ProblemCaseRunCreateBulk{err: fmt.Errorf("calling to ProblemCaseRunClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*ProblemCaseRunCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &ProblemCaseRunCreateBulk{config: c.config, builders: builders} +} + // Update returns an update builder for ProblemCaseRun. func (c *ProblemCaseRunClient) Update() *ProblemCaseRunUpdate { mutation := newProblemCaseRunMutation(c.config, OpUpdate) diff --git a/cloudevents-server/ent/migrate/schema.go b/cloudevents-server/ent/migrate/schema.go index 55d7683..9ff802a 100644 --- a/cloudevents-server/ent/migrate/schema.go +++ b/cloudevents-server/ent/migrate/schema.go @@ -19,6 +19,7 @@ var ( {Name: "timecost_ms", Type: field.TypeInt}, {Name: "report_time", Type: field.TypeTime}, {Name: "build_url", Type: field.TypeString, Size: 1024}, + {Name: "reason", Type: field.TypeString, Size: 32}, } // ProblemCaseRunsTable holds the schema information for the "problem_case_runs" table. ProblemCaseRunsTable = &schema.Table{ diff --git a/cloudevents-server/ent/mutation.go b/cloudevents-server/ent/mutation.go index ece2138..4c7c8f4 100644 --- a/cloudevents-server/ent/mutation.go +++ b/cloudevents-server/ent/mutation.go @@ -42,6 +42,7 @@ type ProblemCaseRunMutation struct { addtimecost_ms *int report_time *time.Time build_url *string + reason *string clearedFields map[string]struct{} done bool oldValue func(context.Context) (*ProblemCaseRun, error) @@ -454,6 +455,42 @@ func (m *ProblemCaseRunMutation) ResetBuildURL() { m.build_url = nil } +// SetReason sets the "reason" field. +func (m *ProblemCaseRunMutation) SetReason(s string) { + m.reason = &s +} + +// Reason returns the value of the "reason" field in the mutation. +func (m *ProblemCaseRunMutation) Reason() (r string, exists bool) { + v := m.reason + if v == nil { + return + } + return *v, true +} + +// OldReason returns the old "reason" field's value of the ProblemCaseRun entity. +// If the ProblemCaseRun object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *ProblemCaseRunMutation) OldReason(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldReason is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldReason requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldReason: %w", err) + } + return oldValue.Reason, nil +} + +// ResetReason resets all changes to the "reason" field. +func (m *ProblemCaseRunMutation) ResetReason() { + m.reason = nil +} + // Where appends a list predicates to the ProblemCaseRunMutation builder. func (m *ProblemCaseRunMutation) Where(ps ...predicate.ProblemCaseRun) { m.predicates = append(m.predicates, ps...) @@ -488,7 +525,7 @@ func (m *ProblemCaseRunMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *ProblemCaseRunMutation) Fields() []string { - fields := make([]string, 0, 8) + fields := make([]string, 0, 9) if m.repo != nil { fields = append(fields, problemcaserun.FieldRepo) } @@ -513,6 +550,9 @@ func (m *ProblemCaseRunMutation) Fields() []string { if m.build_url != nil { fields = append(fields, problemcaserun.FieldBuildURL) } + if m.reason != nil { + fields = append(fields, problemcaserun.FieldReason) + } return fields } @@ -537,6 +577,8 @@ func (m *ProblemCaseRunMutation) Field(name string) (ent.Value, bool) { return m.ReportTime() case problemcaserun.FieldBuildURL: return m.BuildURL() + case problemcaserun.FieldReason: + return m.Reason() } return nil, false } @@ -562,6 +604,8 @@ func (m *ProblemCaseRunMutation) OldField(ctx context.Context, name string) (ent return m.OldReportTime(ctx) case problemcaserun.FieldBuildURL: return m.OldBuildURL(ctx) + case problemcaserun.FieldReason: + return m.OldReason(ctx) } return nil, fmt.Errorf("unknown ProblemCaseRun field %s", name) } @@ -627,6 +671,13 @@ func (m *ProblemCaseRunMutation) SetField(name string, value ent.Value) error { } m.SetBuildURL(v) return nil + case problemcaserun.FieldReason: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetReason(v) + return nil } return fmt.Errorf("unknown ProblemCaseRun field %s", name) } @@ -715,6 +766,9 @@ func (m *ProblemCaseRunMutation) ResetField(name string) error { case problemcaserun.FieldBuildURL: m.ResetBuildURL() return nil + case problemcaserun.FieldReason: + m.ResetReason() + return nil } return fmt.Errorf("unknown ProblemCaseRun field %s", name) } diff --git a/cloudevents-server/ent/problemcaserun.go b/cloudevents-server/ent/problemcaserun.go index fd41f27..609c8d0 100644 --- a/cloudevents-server/ent/problemcaserun.go +++ b/cloudevents-server/ent/problemcaserun.go @@ -32,7 +32,9 @@ type ProblemCaseRun struct { // report unit timestamp ReportTime time.Time `json:"report_time,omitempty"` // CI build url - BuildURL string `json:"build_url,omitempty"` + BuildURL string `json:"build_url,omitempty"` + // failure reason + Reason string `json:"reason,omitempty"` selectValues sql.SelectValues } @@ -45,7 +47,7 @@ func (*ProblemCaseRun) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullBool) case problemcaserun.FieldID, problemcaserun.FieldTimecostMs: values[i] = new(sql.NullInt64) - case problemcaserun.FieldRepo, problemcaserun.FieldBranch, problemcaserun.FieldSuiteName, problemcaserun.FieldCaseName, problemcaserun.FieldBuildURL: + case problemcaserun.FieldRepo, problemcaserun.FieldBranch, problemcaserun.FieldSuiteName, problemcaserun.FieldCaseName, problemcaserun.FieldBuildURL, problemcaserun.FieldReason: values[i] = new(sql.NullString) case problemcaserun.FieldReportTime: values[i] = new(sql.NullTime) @@ -118,6 +120,12 @@ func (pcr *ProblemCaseRun) assignValues(columns []string, values []any) error { } else if value.Valid { pcr.BuildURL = value.String } + case problemcaserun.FieldReason: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field reason", values[i]) + } else if value.Valid { + pcr.Reason = value.String + } default: pcr.selectValues.Set(columns[i], values[i]) } @@ -177,6 +185,9 @@ func (pcr *ProblemCaseRun) String() string { builder.WriteString(", ") builder.WriteString("build_url=") builder.WriteString(pcr.BuildURL) + builder.WriteString(", ") + builder.WriteString("reason=") + builder.WriteString(pcr.Reason) builder.WriteByte(')') return builder.String() } diff --git a/cloudevents-server/ent/problemcaserun/problemcaserun.go b/cloudevents-server/ent/problemcaserun/problemcaserun.go index 6a8ad68..068c53d 100644 --- a/cloudevents-server/ent/problemcaserun/problemcaserun.go +++ b/cloudevents-server/ent/problemcaserun/problemcaserun.go @@ -27,6 +27,8 @@ const ( FieldReportTime = "report_time" // FieldBuildURL holds the string denoting the build_url field in the database. FieldBuildURL = "build_url" + // FieldReason holds the string denoting the reason field in the database. + FieldReason = "reason" // Table holds the table name of the problemcaserun in the database. Table = "problem_case_runs" ) @@ -42,6 +44,7 @@ var Columns = []string{ FieldTimecostMs, FieldReportTime, FieldBuildURL, + FieldReason, } // ValidColumn reports if the column name is valid (part of the table columns). @@ -59,6 +62,8 @@ var ( DefaultFlaky bool // BuildURLValidator is a validator for the "build_url" field. It is called by the builders before save. BuildURLValidator func(string) error + // ReasonValidator is a validator for the "reason" field. It is called by the builders before save. + ReasonValidator func(string) error ) // OrderOption defines the ordering options for the ProblemCaseRun queries. @@ -108,3 +113,8 @@ func ByReportTime(opts ...sql.OrderTermOption) OrderOption { func ByBuildURL(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldBuildURL, opts...).ToFunc() } + +// ByReason orders the results by the reason field. +func ByReason(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldReason, opts...).ToFunc() +} diff --git a/cloudevents-server/ent/problemcaserun/where.go b/cloudevents-server/ent/problemcaserun/where.go index d9e3d3b..c6758f1 100644 --- a/cloudevents-server/ent/problemcaserun/where.go +++ b/cloudevents-server/ent/problemcaserun/where.go @@ -94,6 +94,11 @@ func BuildURL(v string) predicate.ProblemCaseRun { return predicate.ProblemCaseRun(sql.FieldEQ(FieldBuildURL, v)) } +// Reason applies equality check predicate on the "reason" field. It's identical to ReasonEQ. +func Reason(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldEQ(FieldReason, v)) +} + // RepoEQ applies the EQ predicate on the "repo" field. func RepoEQ(v string) predicate.ProblemCaseRun { return predicate.ProblemCaseRun(sql.FieldEQ(FieldRepo, v)) @@ -509,34 +514,82 @@ func BuildURLContainsFold(v string) predicate.ProblemCaseRun { return predicate.ProblemCaseRun(sql.FieldContainsFold(FieldBuildURL, v)) } +// ReasonEQ applies the EQ predicate on the "reason" field. +func ReasonEQ(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldEQ(FieldReason, v)) +} + +// ReasonNEQ applies the NEQ predicate on the "reason" field. +func ReasonNEQ(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldNEQ(FieldReason, v)) +} + +// ReasonIn applies the In predicate on the "reason" field. +func ReasonIn(vs ...string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldIn(FieldReason, vs...)) +} + +// ReasonNotIn applies the NotIn predicate on the "reason" field. +func ReasonNotIn(vs ...string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldNotIn(FieldReason, vs...)) +} + +// ReasonGT applies the GT predicate on the "reason" field. +func ReasonGT(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldGT(FieldReason, v)) +} + +// ReasonGTE applies the GTE predicate on the "reason" field. +func ReasonGTE(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldGTE(FieldReason, v)) +} + +// ReasonLT applies the LT predicate on the "reason" field. +func ReasonLT(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldLT(FieldReason, v)) +} + +// ReasonLTE applies the LTE predicate on the "reason" field. +func ReasonLTE(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldLTE(FieldReason, v)) +} + +// ReasonContains applies the Contains predicate on the "reason" field. +func ReasonContains(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldContains(FieldReason, v)) +} + +// ReasonHasPrefix applies the HasPrefix predicate on the "reason" field. +func ReasonHasPrefix(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldHasPrefix(FieldReason, v)) +} + +// ReasonHasSuffix applies the HasSuffix predicate on the "reason" field. +func ReasonHasSuffix(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldHasSuffix(FieldReason, v)) +} + +// ReasonEqualFold applies the EqualFold predicate on the "reason" field. +func ReasonEqualFold(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldEqualFold(FieldReason, v)) +} + +// ReasonContainsFold applies the ContainsFold predicate on the "reason" field. +func ReasonContainsFold(v string) predicate.ProblemCaseRun { + return predicate.ProblemCaseRun(sql.FieldContainsFold(FieldReason, v)) +} + // And groups predicates with the AND operator between them. func And(predicates ...predicate.ProblemCaseRun) predicate.ProblemCaseRun { - return predicate.ProblemCaseRun(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for _, p := range predicates { - p(s1) - } - s.Where(s1.P()) - }) + return predicate.ProblemCaseRun(sql.AndPredicates(predicates...)) } // Or groups predicates with the OR operator between them. func Or(predicates ...predicate.ProblemCaseRun) predicate.ProblemCaseRun { - return predicate.ProblemCaseRun(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for i, p := range predicates { - if i > 0 { - s1.Or() - } - p(s1) - } - s.Where(s1.P()) - }) + return predicate.ProblemCaseRun(sql.OrPredicates(predicates...)) } // Not applies the not operator on the given predicate. func Not(p predicate.ProblemCaseRun) predicate.ProblemCaseRun { - return predicate.ProblemCaseRun(func(s *sql.Selector) { - p(s.Not()) - }) + return predicate.ProblemCaseRun(sql.NotPredicates(p)) } diff --git a/cloudevents-server/ent/problemcaserun_create.go b/cloudevents-server/ent/problemcaserun_create.go index 4fef016..c2fbad9 100644 --- a/cloudevents-server/ent/problemcaserun_create.go +++ b/cloudevents-server/ent/problemcaserun_create.go @@ -76,6 +76,12 @@ func (pcrc *ProblemCaseRunCreate) SetBuildURL(s string) *ProblemCaseRunCreate { return pcrc } +// SetReason sets the "reason" field. +func (pcrc *ProblemCaseRunCreate) SetReason(s string) *ProblemCaseRunCreate { + pcrc.mutation.SetReason(s) + return pcrc +} + // Mutation returns the ProblemCaseRunMutation object of the builder. func (pcrc *ProblemCaseRunCreate) Mutation() *ProblemCaseRunMutation { return pcrc.mutation @@ -148,6 +154,14 @@ func (pcrc *ProblemCaseRunCreate) check() error { return &ValidationError{Name: "build_url", err: fmt.Errorf(`ent: validator failed for field "ProblemCaseRun.build_url": %w`, err)} } } + if _, ok := pcrc.mutation.Reason(); !ok { + return &ValidationError{Name: "reason", err: errors.New(`ent: missing required field "ProblemCaseRun.reason"`)} + } + if v, ok := pcrc.mutation.Reason(); ok { + if err := problemcaserun.ReasonValidator(v); err != nil { + return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "ProblemCaseRun.reason": %w`, err)} + } + } return nil } @@ -206,17 +220,25 @@ func (pcrc *ProblemCaseRunCreate) createSpec() (*ProblemCaseRun, *sqlgraph.Creat _spec.SetField(problemcaserun.FieldBuildURL, field.TypeString, value) _node.BuildURL = value } + if value, ok := pcrc.mutation.Reason(); ok { + _spec.SetField(problemcaserun.FieldReason, field.TypeString, value) + _node.Reason = value + } return _node, _spec } // ProblemCaseRunCreateBulk is the builder for creating many ProblemCaseRun entities in bulk. type ProblemCaseRunCreateBulk struct { config + err error builders []*ProblemCaseRunCreate } // Save creates the ProblemCaseRun entities in the database. func (pcrcb *ProblemCaseRunCreateBulk) Save(ctx context.Context) ([]*ProblemCaseRun, error) { + if pcrcb.err != nil { + return nil, pcrcb.err + } specs := make([]*sqlgraph.CreateSpec, len(pcrcb.builders)) nodes := make([]*ProblemCaseRun, len(pcrcb.builders)) mutators := make([]Mutator, len(pcrcb.builders)) diff --git a/cloudevents-server/ent/problemcaserun_update.go b/cloudevents-server/ent/problemcaserun_update.go index 40ed989..412876e 100644 --- a/cloudevents-server/ent/problemcaserun_update.go +++ b/cloudevents-server/ent/problemcaserun_update.go @@ -91,6 +91,12 @@ func (pcru *ProblemCaseRunUpdate) SetBuildURL(s string) *ProblemCaseRunUpdate { return pcru } +// SetReason sets the "reason" field. +func (pcru *ProblemCaseRunUpdate) SetReason(s string) *ProblemCaseRunUpdate { + pcru.mutation.SetReason(s) + return pcru +} + // Mutation returns the ProblemCaseRunMutation object of the builder. func (pcru *ProblemCaseRunUpdate) Mutation() *ProblemCaseRunMutation { return pcru.mutation @@ -130,6 +136,11 @@ func (pcru *ProblemCaseRunUpdate) check() error { return &ValidationError{Name: "build_url", err: fmt.Errorf(`ent: validator failed for field "ProblemCaseRun.build_url": %w`, err)} } } + if v, ok := pcru.mutation.Reason(); ok { + if err := problemcaserun.ReasonValidator(v); err != nil { + return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "ProblemCaseRun.reason": %w`, err)} + } + } return nil } @@ -172,6 +183,9 @@ func (pcru *ProblemCaseRunUpdate) sqlSave(ctx context.Context) (n int, err error if value, ok := pcru.mutation.BuildURL(); ok { _spec.SetField(problemcaserun.FieldBuildURL, field.TypeString, value) } + if value, ok := pcru.mutation.Reason(); ok { + _spec.SetField(problemcaserun.FieldReason, field.TypeString, value) + } if n, err = sqlgraph.UpdateNodes(ctx, pcru.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{problemcaserun.Label} @@ -255,6 +269,12 @@ func (pcruo *ProblemCaseRunUpdateOne) SetBuildURL(s string) *ProblemCaseRunUpdat return pcruo } +// SetReason sets the "reason" field. +func (pcruo *ProblemCaseRunUpdateOne) SetReason(s string) *ProblemCaseRunUpdateOne { + pcruo.mutation.SetReason(s) + return pcruo +} + // Mutation returns the ProblemCaseRunMutation object of the builder. func (pcruo *ProblemCaseRunUpdateOne) Mutation() *ProblemCaseRunMutation { return pcruo.mutation @@ -307,6 +327,11 @@ func (pcruo *ProblemCaseRunUpdateOne) check() error { return &ValidationError{Name: "build_url", err: fmt.Errorf(`ent: validator failed for field "ProblemCaseRun.build_url": %w`, err)} } } + if v, ok := pcruo.mutation.Reason(); ok { + if err := problemcaserun.ReasonValidator(v); err != nil { + return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "ProblemCaseRun.reason": %w`, err)} + } + } return nil } @@ -366,6 +391,9 @@ func (pcruo *ProblemCaseRunUpdateOne) sqlSave(ctx context.Context) (_node *Probl if value, ok := pcruo.mutation.BuildURL(); ok { _spec.SetField(problemcaserun.FieldBuildURL, field.TypeString, value) } + if value, ok := pcruo.mutation.Reason(); ok { + _spec.SetField(problemcaserun.FieldReason, field.TypeString, value) + } _node = &ProblemCaseRun{config: pcruo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues diff --git a/cloudevents-server/ent/runtime.go b/cloudevents-server/ent/runtime.go index 7473e0c..9c05b28 100644 --- a/cloudevents-server/ent/runtime.go +++ b/cloudevents-server/ent/runtime.go @@ -21,4 +21,8 @@ func init() { problemcaserunDescBuildURL := problemcaserunFields[7].Descriptor() // problemcaserun.BuildURLValidator is a validator for the "build_url" field. It is called by the builders before save. problemcaserun.BuildURLValidator = problemcaserunDescBuildURL.Validators[0].(func(string) error) + // problemcaserunDescReason is the schema descriptor for reason field. + problemcaserunDescReason := problemcaserunFields[8].Descriptor() + // problemcaserun.ReasonValidator is a validator for the "reason" field. It is called by the builders before save. + problemcaserun.ReasonValidator = problemcaserunDescReason.Validators[0].(func(string) error) } diff --git a/cloudevents-server/ent/runtime/runtime.go b/cloudevents-server/ent/runtime/runtime.go index 8181044..29855c4 100644 --- a/cloudevents-server/ent/runtime/runtime.go +++ b/cloudevents-server/ent/runtime/runtime.go @@ -5,6 +5,6 @@ package runtime // The schema-stitching logic is generated in github.com/PingCAP-QE/ee-apps/cloudevents-server/ent/runtime.go const ( - Version = "v0.12.3" // Version of ent codegen. - Sum = "h1:N5lO2EOrHpCH5HYfiMOCHYbo+oh5M8GjT0/cx5x6xkk=" // Sum of ent codegen. + Version = "v0.12.4" // Version of ent codegen. + Sum = "h1:LddPnAyxls/O7DTXZvUGDj0NZIdGSu317+aoNLJWbD8=" // Sum of ent codegen. ) diff --git a/cloudevents-server/ent/schema/problemcaserun.go b/cloudevents-server/ent/schema/problemcaserun.go index 683fdbc..4a2e989 100644 --- a/cloudevents-server/ent/schema/problemcaserun.go +++ b/cloudevents-server/ent/schema/problemcaserun.go @@ -21,6 +21,7 @@ func (ProblemCaseRun) Fields() []ent.Field { field.Int("timecost_ms").Comment("timecost(milliseconds) of the test case run"), field.Time("report_time").Comment("report unit timestamp"), field.String("build_url").MaxLen(1024).Comment("CI build url"), + field.String("reason").MaxLen(32).Comment("failure reason"), } } diff --git a/cloudevents-server/pkg/events/custom/testcaserun/testcaserun.go b/cloudevents-server/pkg/events/custom/testcaserun/testcaserun.go index 847cbce..72d7158 100644 --- a/cloudevents-server/pkg/events/custom/testcaserun/testcaserun.go +++ b/cloudevents-server/pkg/events/custom/testcaserun/testcaserun.go @@ -45,26 +45,30 @@ func (h *Handler) addRecords(ctx context.Context, caseData map[string]ProblemCas SetRepo(repo). SetBranch(branch). SetSuiteName(target). - SetCaseName(tc). + SetCaseName(tc.Name). SetBuildURL(buildURL). SetTimecostMs(0). SetReportTime(reportTime). - SetFlaky(true), + SetFlaky(true). + SetReason(tc.Reason), ) } for tc, timecost := range caseResults.LongTime { - recordBuilders = append(recordBuilders, - h.Storage.Create(). - SetRepo(repo). - SetBranch(branch). - SetSuiteName(target). - SetCaseName(tc). - SetBuildURL(buildURL). - SetTimecostMs(int(timecost*1000)). - SetReportTime(reportTime). - SetFlaky(false), - ) + entry := h.Storage.Create(). + SetRepo(repo). + SetBranch(branch). + SetSuiteName(target). + SetCaseName(tc). + SetBuildURL(buildURL). + SetTimecostMs(int(timecost * 1000)). + SetReportTime(reportTime). + SetFlaky(false) + if timecost < 0 { + entry.SetReason(reasonNotFinished) + } + + recordBuilders = append(recordBuilders, entry) } } diff --git a/cloudevents-server/pkg/events/custom/testcaserun/types.go b/cloudevents-server/pkg/events/custom/testcaserun/types.go index 1dd0acf..4c6b5ad 100644 --- a/cloudevents-server/pkg/events/custom/testcaserun/types.go +++ b/cloudevents-server/pkg/events/custom/testcaserun/types.go @@ -1,7 +1,17 @@ package testcaserun +const ( + reasonNotFinished = "not_finished" + reasonUnknown = "unknown" +) + // ProblemCasesFromBazel present case run records from bazel. type ProblemCasesFromBazel struct { - NewFlaky []string `yaml:"new_flaky,omitempty" json:"new_flaky,omitempty"` + NewFlaky []flaky `yaml:"new_flaky,omitempty" json:"new_flaky,omitempty"` LongTime map[string]float64 `yaml:"long_time,omitempty" json:"long_time,omitempty"` } + +type flaky struct { + Name string `yaml:"name,omitempty" json:"name,omitempty"` + Reason string `yaml:"reason,omitempty" json:"reason,omitempty"` +}