2022-01-26 15:15:51 +05:30
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-26 15:15:51 +05:30
|
|
|
|
|
|
|
package migration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-11-21 14:06:59 +05:30
|
|
|
"time"
|
2022-01-26 15:15:51 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
2024-07-14 20:24:09 +05:30
|
|
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
2022-11-21 14:06:59 +05:30
|
|
|
"gopkg.in/yaml.v3"
|
2022-01-26 15:15:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Load project data from file, with optional validation
|
2023-07-05 00:06:08 +05:30
|
|
|
func Load(filename string, data any, validation bool) error {
|
2022-01-26 15:15:51 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-05 00:06:08 +05:30
|
|
|
func unmarshal(bs []byte, data any, isJSON bool) error {
|
2022-01-26 15:15:51 +05:30
|
|
|
if isJSON {
|
|
|
|
return json.Unmarshal(bs, data)
|
|
|
|
}
|
|
|
|
return yaml.Unmarshal(bs, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSchema(filename string) (*jsonschema.Schema, error) {
|
|
|
|
c := jsonschema.NewCompiler()
|
2024-07-14 20:24:09 +05:30
|
|
|
c.UseLoader(&SchemaLoader{})
|
2022-01-26 15:15:51 +05:30
|
|
|
return c.Compile(filename)
|
|
|
|
}
|
|
|
|
|
2023-07-05 00:06:08 +05:30
|
|
|
func validate(bs []byte, datatype any, isJSON bool) error {
|
|
|
|
var v any
|
2022-01-26 15:15:51 +05:30
|
|
|
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 {
|
2023-01-27 18:26:00 +05:30
|
|
|
log.Error("migration validation with %s failed:\n%#v", schemaFilename, err)
|
2022-01-26 15:15:51 +05:30
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-05 00:06:08 +05:30
|
|
|
func toStringKeys(val any) (any, error) {
|
2022-01-26 15:15:51 +05:30
|
|
|
var err error
|
|
|
|
switch val := val.(type) {
|
2023-07-05 00:06:08 +05:30
|
|
|
case map[string]any:
|
|
|
|
m := make(map[string]any)
|
2022-01-26 15:15:51 +05:30
|
|
|
for k, v := range val {
|
|
|
|
m[k], err = toStringKeys(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
2023-07-05 00:06:08 +05:30
|
|
|
case []any:
|
|
|
|
l := make([]any, len(val))
|
2022-01-26 15:15:51 +05:30
|
|
|
for i, v := range val {
|
|
|
|
l[i], err = toStringKeys(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l, nil
|
2022-11-21 14:06:59 +05:30
|
|
|
case time.Time:
|
|
|
|
return val.Format(time.RFC3339), nil
|
2022-01-26 15:15:51 +05:30
|
|
|
default:
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
}
|