-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better linting and error handling (#81)
* fix: allow CI pipeline to fail if the Change Request changes scm-engine configuration file otherwise, continue to fail open * fix: MergeRequestIDUint() possible overflow * feat: more schema validation and linting * pre-rework * feat: first draf of 'gitlab lint' command working * feat: more linting and code gen for JSON schema * feat: lint the configuration file
- Loading branch information
Showing
20 changed files
with
532 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ | |
/schema/ignore | ||
/scm-engine | ||
/scm-engine.exe | ||
/scm-engine.schema.json | ||
scm-engine.schema.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/jippi/scm-engine/pkg/config" | ||
"github.com/jippi/scm-engine/pkg/generated/resources" | ||
"github.com/jippi/scm-engine/pkg/scm/gitlab" | ||
"github.com/jippi/scm-engine/pkg/state" | ||
"github.com/santhosh-tekuri/jsonschema/v6" | ||
"github.com/urfave/cli/v2" | ||
slogctx "github.com/veqryn/slog-context" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func Lint(cCtx *cli.Context) error { | ||
ctx := cCtx.Context | ||
ctx = state.WithConfigFilePath(ctx, cCtx.String(FlagConfigFile)) | ||
|
||
// Read raw YAML file | ||
raw, err := os.ReadFile(state.ConfigFilePath(ctx)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Parse the YAML file into lose Go shape | ||
var yamlOutput any | ||
if err := yaml.Unmarshal(raw, &yamlOutput); err != nil { | ||
return err | ||
} | ||
|
||
// Setup file loaders for reading the JSON schema file | ||
loader := jsonschema.SchemeURLLoader{ | ||
"file": jsonschema.FileLoader{}, | ||
"http": newHTTPURLLoader(), | ||
"https": newHTTPURLLoader(), | ||
"embed": &EmbedLoader{}, | ||
} | ||
|
||
// Create json schema compiler | ||
compiler := jsonschema.NewCompiler() | ||
compiler.UseLoader(loader) | ||
|
||
// Compile the schema into validator format | ||
sch, err := compiler.Compile(cCtx.String("schema")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Validate the json output | ||
if err := sch.Validate(yamlOutput); err != nil { | ||
return err | ||
} | ||
|
||
// Load the configuration file via our Go struct | ||
cfg, err := config.LoadFile(state.ConfigFilePath(ctx)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(cfg.Includes) != 0 { | ||
slogctx.Warn(ctx, "Configuration file contains 'include' settings, those are currently unsupported by 'lint' command and will be ignored") | ||
} | ||
|
||
// To scm-engine specific linting last | ||
return cfg.Lint(ctx, &gitlab.Context{}) | ||
} | ||
|
||
type EmbedLoader struct{} | ||
|
||
func (l *EmbedLoader) Load(url string) (any, error) { | ||
return jsonschema.UnmarshalJSON(strings.NewReader(resources.JSONSchema)) | ||
} | ||
|
||
type HTTPURLLoader http.Client | ||
|
||
func (l *HTTPURLLoader) Load(url string) (any, error) { | ||
client := (*http.Client)(l) | ||
|
||
resp, err := client.Get(url) //nolint | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
resp.Body.Close() | ||
|
||
return nil, fmt.Errorf("%s returned status code %d", url, resp.StatusCode) | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
return jsonschema.UnmarshalJSON(resp.Body) | ||
} | ||
|
||
func newHTTPURLLoader() *HTTPURLLoader { | ||
httpLoader := HTTPURLLoader(http.Client{ | ||
Timeout: 15 * time.Second, | ||
}) | ||
|
||
return &httpLoader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package main | ||
|
||
//go:generate go run ./pkg/generated/main.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.