2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-09-27 00:38:15 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2023-11-03 10:44:03 +00:00
|
|
|
"bytes"
|
2021-09-27 00:38:15 +00:00
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2022-04-19 08:40:48 +00:00
|
|
|
"io"
|
2021-09-27 00:38:15 +00:00
|
|
|
|
2022-07-17 15:23:31 +00:00
|
|
|
"codeberg.org/6543/go-yaml2json"
|
2023-04-29 12:49:41 +00:00
|
|
|
"codeberg.org/6543/xyaml"
|
2024-05-24 20:35:04 +00:00
|
|
|
json_schema "github.com/xeipuuv/gojsonschema"
|
2023-04-29 12:49:41 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-09-27 00:38:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed schema.json
|
|
|
|
var schemaDefinition []byte
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Lint lints an io.Reader against the Woodpecker `schema.json`.
|
2024-05-24 20:35:04 +00:00
|
|
|
func Lint(r io.Reader) ([]json_schema.ResultError, error) {
|
|
|
|
schemaLoader := json_schema.NewBytesLoader(schemaDefinition)
|
2023-04-29 12:49:41 +00:00
|
|
|
|
|
|
|
// read yaml config
|
|
|
|
rBytes, err := io.ReadAll(r)
|
2021-09-27 00:38:15 +00:00
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
return nil, fmt.Errorf("failed to load yml file %w", err)
|
2021-09-27 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 12:49:41 +00:00
|
|
|
// resolve sequence merges
|
|
|
|
yamlDoc := new(yaml.Node)
|
|
|
|
if err := xyaml.Unmarshal(rBytes, yamlDoc); err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse yml file %w", err)
|
2023-04-29 12:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert to json
|
|
|
|
jsonDoc, err := yaml2json.ConvertNode(yamlDoc)
|
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
return nil, fmt.Errorf("failed to convert yaml %w", err)
|
2023-04-29 12:49:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 20:35:04 +00:00
|
|
|
documentLoader := json_schema.NewBytesLoader(jsonDoc)
|
|
|
|
result, err := json_schema.Validate(schemaLoader, documentLoader)
|
2021-09-27 00:38:15 +00:00
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
return nil, fmt.Errorf("validation failed %w", err)
|
2021-09-27 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !result.Valid() {
|
2024-01-10 21:56:42 +00:00
|
|
|
return result.Errors(), fmt.Errorf("config not valid")
|
2021-09-27 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-11-03 10:44:03 +00:00
|
|
|
|
2024-05-24 20:35:04 +00:00
|
|
|
func LintString(s string) ([]json_schema.ResultError, error) {
|
2023-11-03 10:44:03 +00:00
|
|
|
return Lint(bytes.NewBufferString(s))
|
|
|
|
}
|