|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "maps" |
| 7 | + "os/exec" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/databricks/cli/libs/jsonschema" |
| 12 | +) |
| 13 | + |
| 14 | +// Version when bundle/schema/jsonschema.json was added to the repo. |
| 15 | +var embeddedSchemaVersion = [3]int{0, 229, 0} |
| 16 | + |
| 17 | +// computeSinceVersions computes when each field was first introduced by analyzing git history. |
| 18 | +// It returns a map from "typePath.fieldName" to the version string (e.g., "v0.229.0"). |
| 19 | +// This function always recomputes versions at runtime without storing state. |
| 20 | +func computeSinceVersions() (map[string]string, error) { |
| 21 | + versions, err := getVersionTags() |
| 22 | + if err != nil { |
| 23 | + return nil, fmt.Errorf("getting version tags: %w", err) |
| 24 | + } |
| 25 | + if len(versions) == 0 { |
| 26 | + return nil, nil |
| 27 | + } |
| 28 | + |
| 29 | + sinceVersions := make(map[string]string) |
| 30 | + for _, version := range versions { |
| 31 | + schema, err := getSchemaAtVersion(version) |
| 32 | + if err != nil { |
| 33 | + continue |
| 34 | + } |
| 35 | + |
| 36 | + for field := range flattenSchema(schema) { |
| 37 | + if _, seen := sinceVersions[field]; !seen { |
| 38 | + sinceVersions[field] = version |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return sinceVersions, nil |
| 44 | +} |
| 45 | + |
| 46 | +// parseVersion parses a version tag like "v0.228.0" into [0, 228, 0]. |
| 47 | +func parseVersion(tag string) ([3]int, error) { |
| 48 | + tag = strings.TrimPrefix(tag, "v") |
| 49 | + parts := strings.Split(tag, ".") |
| 50 | + if len(parts) < 3 { |
| 51 | + return [3]int{}, fmt.Errorf("invalid version tag: %s", tag) |
| 52 | + } |
| 53 | + var v [3]int |
| 54 | + for i := range 3 { |
| 55 | + n, err := strconv.Atoi(parts[i]) |
| 56 | + if err != nil { |
| 57 | + return [3]int{}, fmt.Errorf("invalid version component: %s", parts[i]) |
| 58 | + } |
| 59 | + v[i] = n |
| 60 | + } |
| 61 | + return v, nil |
| 62 | +} |
| 63 | + |
| 64 | +// compareVersions returns -1 if a < b, 0 if a == b, 1 if a > b. |
| 65 | +func compareVersions(a, b [3]int) int { |
| 66 | + for i := range 3 { |
| 67 | + if a[i] < b[i] { |
| 68 | + return -1 |
| 69 | + } |
| 70 | + if a[i] > b[i] { |
| 71 | + return 1 |
| 72 | + } |
| 73 | + } |
| 74 | + return 0 |
| 75 | +} |
| 76 | + |
| 77 | +// getVersionTags returns sorted list of version tags from git (oldest first). |
| 78 | +func getVersionTags() ([]string, error) { |
| 79 | + cmd := exec.Command("git", "tag", "--list", "v*", "--sort=version:refname") |
| 80 | + output, err := cmd.Output() |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("failed to get git tags: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + var tags []string |
| 86 | + for _, line := range strings.Split(string(output), "\n") { |
| 87 | + tag := strings.TrimSpace(line) |
| 88 | + if tag == "" { |
| 89 | + continue |
| 90 | + } |
| 91 | + v, err := parseVersion(tag) |
| 92 | + if err != nil { |
| 93 | + continue |
| 94 | + } |
| 95 | + if compareVersions(v, embeddedSchemaVersion) >= 0 { |
| 96 | + tags = append(tags, tag) |
| 97 | + } |
| 98 | + } |
| 99 | + return tags, nil |
| 100 | +} |
| 101 | + |
| 102 | +// getSchemaAtVersion extracts the JSON schema from the embedded file at a given version. |
| 103 | +func getSchemaAtVersion(version string) (map[string]any, error) { |
| 104 | + cmd := exec.Command("git", "show", version+":bundle/schema/jsonschema.json") |
| 105 | + output, err := cmd.Output() |
| 106 | + if err != nil { |
| 107 | + return nil, fmt.Errorf("failed to get schema at %s: %w", version, err) |
| 108 | + } |
| 109 | + |
| 110 | + var schema map[string]any |
| 111 | + if err := json.Unmarshal(output, &schema); err != nil { |
| 112 | + return nil, fmt.Errorf("failed to parse schema at %s: %w", version, err) |
| 113 | + } |
| 114 | + return schema, nil |
| 115 | +} |
| 116 | + |
| 117 | +// flattenSchema extracts all field paths from a JSON schema. |
| 118 | +// Returns a map of "typePath.fieldName" -> true for all fields in the schema. |
| 119 | +func flattenSchema(schema map[string]any) map[string]bool { |
| 120 | + fields := make(map[string]bool) |
| 121 | + |
| 122 | + if defs, ok := schema["$defs"].(map[string]any); ok { |
| 123 | + typeDefs := walkDefs(defs, "") |
| 124 | + for typePath, propNames := range typeDefs { |
| 125 | + for _, propName := range propNames { |
| 126 | + fields[typePath+"."+propName] = true |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + rootType := "github.com/databricks/cli/bundle/config.Root" |
| 132 | + if props, ok := schema["properties"].(map[string]any); ok { |
| 133 | + for propName := range props { |
| 134 | + fields[rootType+"."+propName] = true |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return fields |
| 139 | +} |
| 140 | + |
| 141 | +// walkDefs recursively walks $defs to extract type definitions. |
| 142 | +func walkDefs(defs map[string]any, prefix string) map[string][]string { |
| 143 | + result := make(map[string][]string) |
| 144 | + |
| 145 | + for key, value := range defs { |
| 146 | + valueMap, ok := value.(map[string]any) |
| 147 | + if !ok { |
| 148 | + continue |
| 149 | + } |
| 150 | + |
| 151 | + currentPath := prefix |
| 152 | + if currentPath != "" { |
| 153 | + currentPath += "/" + key |
| 154 | + } else { |
| 155 | + currentPath = key |
| 156 | + } |
| 157 | + |
| 158 | + props := extractProperties(valueMap) |
| 159 | + if props != nil { |
| 160 | + var propNames []string |
| 161 | + for propName := range props { |
| 162 | + propNames = append(propNames, propName) |
| 163 | + } |
| 164 | + result[currentPath] = propNames |
| 165 | + } else if _, hasType := valueMap["type"]; !hasType { |
| 166 | + // It's a nested namespace, recurse into it |
| 167 | + maps.Copy(result, walkDefs(valueMap, currentPath)) |
| 168 | + } |
| 169 | + } |
| 170 | + return result |
| 171 | +} |
| 172 | + |
| 173 | +// extractProperties extracts the properties map from a schema definition, |
| 174 | +// checking direct properties first, then oneOf/anyOf variants. |
| 175 | +func extractProperties(valueMap map[string]any) map[string]any { |
| 176 | + if props, ok := valueMap["properties"].(map[string]any); ok { |
| 177 | + return props |
| 178 | + } |
| 179 | + |
| 180 | + // Check oneOf and anyOf variants for properties |
| 181 | + for _, key := range []string{"oneOf", "anyOf"} { |
| 182 | + if variants, ok := valueMap[key].([]any); ok { |
| 183 | + for _, variant := range variants { |
| 184 | + if variantMap, ok := variant.(map[string]any); ok { |
| 185 | + if props, ok := variantMap["properties"].(map[string]any); ok { |
| 186 | + return props |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return nil |
| 194 | +} |
| 195 | + |
| 196 | +// addSinceVersionToSchema applies sinceVersion annotations to the generated schema. |
| 197 | +// The sinceVersions map uses keys in the format "typePath.fieldName". |
| 198 | +func addSinceVersionToSchema(s *jsonschema.Schema, sinceVersions map[string]string) { |
| 199 | + if sinceVersions == nil || s == nil { |
| 200 | + return |
| 201 | + } |
| 202 | + |
| 203 | + // Apply to root properties |
| 204 | + rootType := "github.com/databricks/cli/bundle/config.Root" |
| 205 | + for propName, prop := range s.Properties { |
| 206 | + key := rootType + "." + propName |
| 207 | + if version, ok := sinceVersions[key]; ok { |
| 208 | + prop.SinceVersion = version |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + // Apply to $defs - the definitions are nested maps like: |
| 213 | + // {"github.com": {"databricks": {"cli": {"bundle": {"config.Root": Schema{...}}}}}} |
| 214 | + if s.Definitions == nil { |
| 215 | + return |
| 216 | + } |
| 217 | + |
| 218 | + walkDefinitions(s.Definitions, "", sinceVersions) |
| 219 | +} |
| 220 | + |
| 221 | +// walkDefinitions recursively walks the nested definitions map and applies sinceVersion. |
| 222 | +func walkDefinitions(defs map[string]any, pathPrefix string, sinceVersions map[string]string) { |
| 223 | + for key, value := range defs { |
| 224 | + var currentPath string |
| 225 | + if pathPrefix != "" { |
| 226 | + currentPath = pathPrefix + "/" + key |
| 227 | + } else { |
| 228 | + currentPath = key |
| 229 | + } |
| 230 | + |
| 231 | + // Try to convert to *jsonschema.Schema |
| 232 | + if schema, ok := value.(*jsonschema.Schema); ok { |
| 233 | + addSinceVersionToProperties(schema, currentPath, sinceVersions) |
| 234 | + continue |
| 235 | + } |
| 236 | + |
| 237 | + // Try to convert to jsonschema.Schema (non-pointer) |
| 238 | + if schema, ok := value.(jsonschema.Schema); ok { |
| 239 | + addSinceVersionToProperties(&schema, currentPath, sinceVersions) |
| 240 | + // Update the map with the modified schema |
| 241 | + defs[key] = schema |
| 242 | + continue |
| 243 | + } |
| 244 | + |
| 245 | + // Otherwise, it's a nested map - recurse into it |
| 246 | + if nestedMap, ok := value.(map[string]any); ok { |
| 247 | + walkDefinitions(nestedMap, currentPath, sinceVersions) |
| 248 | + } |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +// addSinceVersionToProperties applies sinceVersion to schema properties. |
| 253 | +func addSinceVersionToProperties(s *jsonschema.Schema, typePath string, sinceVersions map[string]string) { |
| 254 | + if s == nil { |
| 255 | + return |
| 256 | + } |
| 257 | + |
| 258 | + // Apply to direct properties |
| 259 | + for propName, prop := range s.Properties { |
| 260 | + key := typePath + "." + propName |
| 261 | + if version, ok := sinceVersions[key]; ok { |
| 262 | + prop.SinceVersion = version |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + // Handle OneOf variants (need to use index to modify in place) |
| 267 | + for i := range s.OneOf { |
| 268 | + addSinceVersionToProperties(&s.OneOf[i], typePath, sinceVersions) |
| 269 | + } |
| 270 | + |
| 271 | + // Handle AnyOf variants (need to use index to modify in place) |
| 272 | + for i := range s.AnyOf { |
| 273 | + addSinceVersionToProperties(&s.AnyOf[i], typePath, sinceVersions) |
| 274 | + } |
| 275 | +} |
0 commit comments