chore: copy latest schema to stable path for easier diff (#2586)

Because we generate a new JSON schema file every time the schema version
changes, the git diff always shows that the file is completely new.
Therefore, every time the file is re-generated, also write the schema to
a stable path, so that the actual changes to the schema are easily
visible in the git diff of the latest schema file.

Signed-off-by: Will Murphy <will.murphy@anchore.com>
This commit is contained in:
William Murphy 2024-02-02 17:09:28 -05:00 committed by GitHub
parent 98de2e2f62
commit b735106848
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2266 additions and 3 deletions

File diff suppressed because it is too large Load Diff

View File

@ -115,7 +115,7 @@ func build() *jsonschema.Schema {
// ensure the generated list of names is stable between runs // ensure the generated list of names is stable between runs
sort.Strings(metadataNames) sort.Strings(metadataNames)
var metadataTypes = []map[string]string{ metadataTypes := []map[string]string{
// allow for no metadata to be provided // allow for no metadata to be provided
{"type": "null"}, {"type": "null"},
} }
@ -134,7 +134,7 @@ func build() *jsonschema.Schema {
} }
func encode(schema *jsonschema.Schema) []byte { func encode(schema *jsonschema.Schema) []byte {
var newSchemaBuffer = new(bytes.Buffer) newSchemaBuffer := new(bytes.Buffer)
enc := json.NewEncoder(newSchemaBuffer) enc := json.NewEncoder(newSchemaBuffer)
// prevent > and < from being escaped in the payload // prevent > and < from being escaped in the payload
enc.SetEscapeHTML(false) enc.SetEscapeHTML(false)
@ -154,6 +154,7 @@ func write(schema []byte) {
os.Exit(1) os.Exit(1)
} }
schemaPath := filepath.Join(repoRoot, "schema", "json", fmt.Sprintf("schema-%s.json", internal.JSONSchemaVersion)) schemaPath := filepath.Join(repoRoot, "schema", "json", fmt.Sprintf("schema-%s.json", internal.JSONSchemaVersion))
latestSchemaPath := filepath.Join(repoRoot, "schema", "json", "schema-latest.json")
if _, err := os.Stat(schemaPath); !os.IsNotExist(err) { if _, err := os.Stat(schemaPath); !os.IsNotExist(err) {
// check if the schema is the same... // check if the schema is the same...
@ -182,13 +183,23 @@ func write(schema []byte) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer fh.Close()
_, err = fh.Write(schema) _, err = fh.Write(schema)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer fh.Close() latestFile, err := os.Create(latestSchemaPath)
if err != nil {
panic(err)
}
defer latestFile.Close()
_, err = latestFile.Write(schema)
if err != nil {
panic(err)
}
fmt.Printf("Wrote new schema to %q\n", schemaPath) fmt.Printf("Wrote new schema to %q\n", schemaPath)
} }