|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +func CheckIfNilInWire() { |
| 13 | + app, err := InitializeApp() |
| 14 | + if err != nil { |
| 15 | + log.Panic(err) |
| 16 | + } |
| 17 | + nilFieldsMap := make(map[string]bool) |
| 18 | + checkNilFields(app, nilFieldsMap) |
| 19 | + fmt.Println("NIL Fields present in impls are: ", nilFieldsMap) |
| 20 | + //Writes the length of nilFieldsMap to a file (e.g., output.env) so that we can export this file's data in a pre-CI pipeline bash script and fail the pre-CI pipeline if the length of nilFieldsMap is greater than zero. |
| 21 | + err = writeResultToFile(len(nilFieldsMap)) |
| 22 | + if err != nil { |
| 23 | + return |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func checkNilFields(obj interface{}, nilObjMap map[string]bool) { |
| 28 | + val := reflect.ValueOf(obj) |
| 29 | + if val.Kind() == reflect.Ptr { |
| 30 | + val = val.Elem() |
| 31 | + } |
| 32 | + if val.Kind() != reflect.Struct { |
| 33 | + return |
| 34 | + } |
| 35 | + valName := val.Type().Name() |
| 36 | + for i := 0; i < val.NumField(); i++ { |
| 37 | + field := val.Field(i) |
| 38 | + fieldName := val.Type().Field(i).Name |
| 39 | + pkgPath := val.Type().PkgPath() |
| 40 | + if pkgPath != "main" && !strings.Contains(pkgPath, "devtron-labs/devtron") { |
| 41 | + //package not from this repo, ignoring |
| 42 | + continue |
| 43 | + } |
| 44 | + if skipUnnecessaryFieldsForCheck(fieldName, valName) { // skip unnecessary fileds and values |
| 45 | + continue |
| 46 | + } |
| 47 | + if !canFieldTypeBeNil(field) { // field can not be nil, skip |
| 48 | + continue |
| 49 | + } else if field.IsNil() { // check if the field is nil |
| 50 | + mapEntry := fmt.Sprintf("%s.%s", valName, fieldName) |
| 51 | + nilObjMap[mapEntry] = true |
| 52 | + continue |
| 53 | + } |
| 54 | + if canSkipFieldStructCheck(fieldName, valName) { |
| 55 | + continue |
| 56 | + } |
| 57 | + if !isExported(fieldName) && !field.CanInterface() { |
| 58 | + unexportedField := GetUnexportedField(field) |
| 59 | + checkNilFields(unexportedField, nilObjMap) |
| 60 | + } else { |
| 61 | + // Recurse |
| 62 | + checkNilFields(field.Interface(), nilObjMap) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func canFieldTypeBeNil(field reflect.Value) bool { |
| 68 | + kind := field.Kind() |
| 69 | + switch kind { |
| 70 | + case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer, |
| 71 | + reflect.Interface, reflect.Slice: |
| 72 | + return true |
| 73 | + default: //other types can not be nil |
| 74 | + return false |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func canSkipFieldStructCheck(fieldName, valName string) bool { |
| 79 | + fieldName = strings.ToLower(fieldName) |
| 80 | + valName = strings.ToLower(valName) |
| 81 | + if valName == "githubclient" && (fieldName == "client" || fieldName == "gitopshelper") { |
| 82 | + return true |
| 83 | + } |
| 84 | + for _, str := range []string{"logger", "dbconnection", "syncedenforcer"} { |
| 85 | + if fieldName == str { |
| 86 | + return true |
| 87 | + } |
| 88 | + } |
| 89 | + return false |
| 90 | +} |
| 91 | + |
| 92 | +func skipUnnecessaryFieldsForCheck(fieldName, valName string) bool { |
| 93 | + fieldName = strings.ToLower(fieldName) |
| 94 | + valName = strings.ToLower(valName) |
| 95 | + if valName == "cicdconfig" { |
| 96 | + return true |
| 97 | + } |
| 98 | + fieldAndValName := map[string][]string{ |
| 99 | + "app": {"enforcerv2", "server"}, |
| 100 | + "gitfactory": {"client"}, |
| 101 | + "argocdconnectionmanagerimpl": {"argocdsettings"}, |
| 102 | + "enforcerimpl": {"cache", "enforcerv2"}, |
| 103 | + "helmappclientimpl": {"applicationserviceclient"}, |
| 104 | + "modulecronserviceimpl": {"cron"}, |
| 105 | + "oteltracingserviceimpl": {"traceprovider"}, |
| 106 | + "terminalaccessrepositoryimpl": {"templatescache"}, |
| 107 | + } |
| 108 | + if _, ok := fieldAndValName[valName]; ok { |
| 109 | + for _, ignoreFieldName := range fieldAndValName[valName] { |
| 110 | + if ignoreFieldName == fieldName { |
| 111 | + return true |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return false |
| 116 | +} |
| 117 | +func GetUnexportedField(field reflect.Value) interface{} { |
| 118 | + return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface() |
| 119 | +} |
| 120 | + |
| 121 | +func isExported(fieldName string) bool { |
| 122 | + return strings.ToUpper(fieldName[0:1]) == fieldName[0:1] |
| 123 | +} |
| 124 | + |
| 125 | +func writeResultToFile(data int) error { |
| 126 | + file, err := os.Create("/test/output.env") |
| 127 | + if err != nil { |
| 128 | + log.Println("Failed to create file:", err) |
| 129 | + return err |
| 130 | + } |
| 131 | + defer file.Close() |
| 132 | + _, err = file.WriteString(fmt.Sprintf("OUTPUT=%d", data)) |
| 133 | + if err != nil { |
| 134 | + log.Println("Failed to write to file:", err) |
| 135 | + return err |
| 136 | + } |
| 137 | + return nil |
| 138 | +} |
0 commit comments