github.com/kubeshop/testkube@v1.17.23/pkg/tcl/testworkflowstcl/testworkflowcontroller/notification.go (about)

     1  // Copyright 2024 Testkube.
     2  //
     3  // Licensed as a Testkube Pro file under the Testkube Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //	https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt
     8  
     9  package testworkflowcontroller
    10  
    11  import (
    12  	"encoding/json"
    13  	"time"
    14  
    15  	"github.com/kubeshop/testkube/cmd/tcl/testworkflow-init/data"
    16  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    17  	"github.com/kubeshop/testkube/pkg/log"
    18  )
    19  
    20  type Notification struct {
    21  	Timestamp time.Time                    `json:"ts"`
    22  	Result    *testkube.TestWorkflowResult `json:"result,omitempty"`
    23  	Ref       string                       `json:"ref,omitempty"`
    24  	Log       string                       `json:"log,omitempty"`
    25  	Output    *data.Instruction            `json:"output,omitempty"`
    26  }
    27  
    28  func (n *Notification) ToInternal() testkube.TestWorkflowExecutionNotification {
    29  	return testkube.TestWorkflowExecutionNotification{
    30  		Ts:     n.Timestamp,
    31  		Result: n.Result,
    32  		Ref:    n.Ref,
    33  		Log:    n.Log,
    34  		Output: InstructionToInternal(n.Output),
    35  	}
    36  }
    37  
    38  func InstructionToInternal(instruction *data.Instruction) *testkube.TestWorkflowOutput {
    39  	if instruction == nil {
    40  		return nil
    41  	}
    42  	value := map[string]interface{}(nil)
    43  	if instruction.Value != nil {
    44  		v, _ := json.Marshal(instruction.Value)
    45  		e := json.Unmarshal(v, &value)
    46  		if e != nil {
    47  			log.DefaultLogger.Warnf("invalid output passed from TestWorkflow - %v", instruction.Value)
    48  		}
    49  	}
    50  	if v, ok := instruction.Value.(map[string]interface{}); ok {
    51  		value = v
    52  	}
    53  	return &testkube.TestWorkflowOutput{
    54  		Ref:   instruction.Ref,
    55  		Name:  instruction.Name,
    56  		Value: value,
    57  	}
    58  }