forgejo/modules/migration/file_format.go
Earl Warren af5df243d4 chore: replace gopkg.in/yaml.v3 with go.yaml.in/yaml/v3 (#8956)
- gopkg.in/yaml.v3 is archived and unmaintained
- go.yaml.in/yaml/v3 is a compatible fork under the umbrella
  of https://yaml.org/

### Tests

There is no need for more tests than already provided: it is like an upgrade to a minor version, only from a fork. I browsed the changes and there are some bug fixes. They all seem reasonably minimal. It is not one of those forks that went crazy with breaking changes 😁 And there is a non zero chance that [a bug that matters to Forgejo Actions](https://github.com/yaml/go-yaml/issues/76) is fixed there. It is rare and can wait but it did happen on Codeberg.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8956
Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-08-20 15:31:12 +02:00

110 lines
2.1 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package migration
import (
"fmt"
"os"
"strings"
"time"
"forgejo.org/modules/json"
"forgejo.org/modules/log"
"github.com/santhosh-tekuri/jsonschema/v6"
"go.yaml.in/yaml/v3"
)
// Load project data from file, with optional validation
func Load(filename string, data any, validation bool) error {
isJSON := strings.HasSuffix(filename, ".json")
bs, err := os.ReadFile(filename)
if err != nil {
return err
}
if validation {
err := validate(bs, data, isJSON)
if err != nil {
return err
}
}
return unmarshal(bs, data, isJSON)
}
func unmarshal(bs []byte, data any, isJSON bool) error {
if isJSON {
return json.Unmarshal(bs, data)
}
return yaml.Unmarshal(bs, data)
}
func getSchema(filename string) (*jsonschema.Schema, error) {
c := jsonschema.NewCompiler()
c.UseLoader(&SchemaLoader{})
return c.Compile(filename)
}
func validate(bs []byte, datatype any, isJSON bool) error {
var v any
err := unmarshal(bs, &v, isJSON)
if err != nil {
return err
}
if !isJSON {
v, err = toStringKeys(v)
if err != nil {
return err
}
}
var schemaFilename string
switch datatype := datatype.(type) {
case *[]*Issue:
schemaFilename = "issue.json"
case *[]*Milestone:
schemaFilename = "milestone.json"
default:
return fmt.Errorf("file_format:validate: %T has not a validation implemented", datatype)
}
sch, err := getSchema(schemaFilename)
if err != nil {
return err
}
err = sch.Validate(v)
if err != nil {
log.Error("migration validation with %s failed:\n%#v", schemaFilename, err)
}
return err
}
func toStringKeys(val any) (any, error) {
var err error
switch val := val.(type) {
case map[string]any:
m := make(map[string]any)
for k, v := range val {
m[k], err = toStringKeys(v)
if err != nil {
return nil, err
}
}
return m, nil
case []any:
l := make([]any, len(val))
for i, v := range val {
l[i], err = toStringKeys(v)
if err != nil {
return nil, err
}
}
return l, nil
case time.Time:
return val.Format(time.RFC3339), nil
default:
return val, nil
}
}