github.com/devtron-labs/ci-runner@v0.0.0-20240518055909-b2672f3349d7/helper/pluginBean.go (about) 1 package helper 2 3 import ( 4 "encoding/json" 5 "fmt" 6 ) 7 8 type RefPluginObject struct { 9 Id int `json:"id"` 10 Steps []*StepObject `json:"steps"` 11 } 12 13 const STEP_TYPE_INLINE = "INLINE" 14 15 //const STEP_TYPE_REF_PLUGIN = "REF_PLUGIN" 16 17 /*script string, 18 envInputVars map[string]string, 19 outputVars []string 20 trigger/skip ConditionObject 21 success/fail condition 22 ArtifactPaths 23 */ 24 25 type StepObject struct { 26 Name string `json:"name"` 27 Index int `json:"index"` 28 StepType string `json:"stepType"` // REF_PLUGIN or INLINE 29 ExecutorType ExecutorType `json:"executorType"` //continer_image/ shell 30 RefPluginId int `json:"refPluginId"` 31 Script string `json:"script"` 32 InputVars []*VariableObject `json:"inputVars"` 33 ExposedPorts map[int]int `json:"exposedPorts"` //map of host:container 34 OutputVars []*VariableObject `json:"outputVars"` 35 TriggerSkipConditions []*ConditionObject `json:"triggerSkipConditions"` 36 SuccessFailureConditions []*ConditionObject `json:"successFailureConditions"` 37 DockerImage string `json:"dockerImage"` 38 Command string `json:"command"` 39 Args []string `json:"args"` 40 CustomScriptMount *MountPath `json:"customScriptMount"` // destination path - storeScriptAt 41 SourceCodeMount *MountPath `json:"sourceCodeMount"` // destination path - mountCodeToContainerPath 42 ExtraVolumeMounts []*MountPath `json:"extraVolumeMounts"` // filePathMapping 43 ArtifactPaths []string `json:"artifactPaths"` 44 TriggerIfParentStageFail bool `json:"triggerIfParentStageFail"` 45 } 46 47 type MountPath struct { 48 SrcPath string `json:"sourcePath"` 49 DstPath string `json:"destinationPath"` 50 } 51 52 // ------------ 53 type Format int 54 55 const ( 56 STRING Format = iota 57 NUMBER 58 BOOL 59 DATE 60 ) 61 62 func (d Format) ValuesOf(format string) (Format, error) { 63 if format == "NUMBER" || format == "number" { 64 return NUMBER, nil 65 } else if format == "BOOL" || format == "bool" || format == "boolean" { 66 return BOOL, nil 67 } else if format == "STRING" || format == "string" { 68 return STRING, nil 69 } else if format == "DATE" || format == "date" { 70 return DATE, nil 71 } 72 return STRING, fmt.Errorf("invalid Format: %s", format) 73 } 74 75 func (d Format) String() string { 76 return [...]string{"NUMBER", "BOOL", "STRING", "DATE"}[d] 77 } 78 79 func (t Format) MarshalJSON() ([]byte, error) { 80 return json.Marshal(t.String()) 81 } 82 83 func (t *Format) UnmarshalJSON(b []byte) error { 84 var s string 85 err := json.Unmarshal(b, &s) 86 if err != nil { 87 return err 88 } 89 format, err := t.ValuesOf(s) 90 if err != nil { 91 return err 92 } 93 *t = format 94 return nil 95 } 96 97 // --------------- 98 type ExecutorType int 99 100 const ( 101 CONTAINER_IMAGE ExecutorType = iota 102 SHELL 103 PLUGIN // Added to avoid un-marshaling error in REF_PLUGIN type steps, otherwise this value won't be used 104 ) 105 106 func (d ExecutorType) ValueOf(executorType string) (ExecutorType, error) { 107 if executorType == "CONTAINER_IMAGE" { 108 return CONTAINER_IMAGE, nil 109 } else if executorType == "SHELL" { 110 return SHELL, nil 111 } else if executorType == "PLUGIN" { 112 return PLUGIN, nil 113 } 114 return SHELL, fmt.Errorf("invalid executorType: %s", executorType) 115 } 116 func (d ExecutorType) String() string { 117 return [...]string{"CONTAINER_IMAGE", "SHELL"}[d] 118 } 119 func (t ExecutorType) MarshalJSON() ([]byte, error) { 120 return json.Marshal(t.String()) 121 } 122 123 func (t *ExecutorType) UnmarshalJSON(b []byte) error { 124 var s string 125 err := json.Unmarshal(b, &s) 126 if err != nil { 127 return err 128 } 129 execType, err := t.ValueOf(s) 130 if err != nil { 131 return err 132 } 133 *t = execType 134 return nil 135 } 136 137 // ----- 138 type VariableType int 139 140 const ( 141 VALUE VariableType = iota 142 REF_PRE_CI 143 REF_POST_CI 144 REF_GLOBAL 145 REF_PLUGIN 146 ) 147 148 func (d VariableType) ValueOf(variableType string) (VariableType, error) { 149 if variableType == "VALUE" { 150 return VALUE, nil 151 } else if variableType == "REF_PRE_CI" { 152 return REF_PRE_CI, nil 153 } else if variableType == "REF_POST_CI" { 154 return REF_POST_CI, nil 155 } else if variableType == "REF_GLOBAL" { 156 return REF_GLOBAL, nil 157 } else if variableType == "REF_PLUGIN" { 158 return REF_PLUGIN, nil 159 } 160 return VALUE, fmt.Errorf("invalid variableType %s", variableType) 161 } 162 func (d VariableType) String() string { 163 return [...]string{"VALUE", "REF_PRE_CI", "REF_POST_CI", "REF_GLOBAL", "REF_PLUGIN"}[d] 164 } 165 func (t VariableType) MarshalJSON() ([]byte, error) { 166 return json.Marshal(t.String()) 167 } 168 169 func (t *VariableType) UnmarshalJSON(b []byte) error { 170 var s string 171 err := json.Unmarshal(b, &s) 172 if err != nil { 173 return err 174 } 175 variableType, err := t.ValueOf(s) 176 if err != nil { 177 return err 178 } 179 *t = variableType 180 return nil 181 } 182 183 // --------------- 184 type VariableObject struct { 185 Name string `json:"name"` 186 Format Format `json:"format"` 187 //only for input type 188 Value string `json:"value"` 189 // GlobalVarName string `json:"globalVarName"` 190 ReferenceVariableName string `json:"referenceVariableName"` 191 VariableType VariableType `json:"variableType"` 192 ReferenceVariableStepIndex int `json:"referenceVariableStepIndex"` 193 VariableStepIndexInPlugin int `json:"variableStepIndexInPlugin"` 194 TypedValue interface{} `json:"-"` //typeCased and deduced 195 } 196 197 func (v *VariableObject) TypeCheck() error { 198 typedValue, err := TypeConverter(v.Value, v.Format) 199 if err != nil { 200 return err 201 } 202 v.TypedValue = typedValue 203 return nil 204 } 205 206 // ---------- 207 type ConditionType int 208 209 const ( 210 TRIGGER = iota 211 SKIP 212 PASS 213 FAIL 214 ) 215 216 func (d ConditionType) ValueOf(conditionType string) (ConditionType, error) { 217 if conditionType == "TRIGGER" { 218 return TRIGGER, nil 219 } else if conditionType == "SKIP" { 220 return SKIP, nil 221 } else if conditionType == "PASS" { 222 return PASS, nil 223 } else if conditionType == "FAIL" { 224 return FAIL, nil 225 } 226 return PASS, fmt.Errorf("invalid conditionType: %s", conditionType) 227 } 228 func (d ConditionType) String() string { 229 return [...]string{"TRIGGER", "SKIP", "PASS", "FAIL"}[d] 230 } 231 232 func (t ConditionType) MarshalJSON() ([]byte, error) { 233 return json.Marshal(t.String()) 234 } 235 236 func (t *ConditionType) UnmarshalJSON(b []byte) error { 237 var s string 238 err := json.Unmarshal(b, &s) 239 if err != nil { 240 return err 241 } 242 executorType, err := t.ValueOf(s) 243 if err != nil { 244 return err 245 } 246 *t = executorType 247 return nil 248 } 249 250 //------