-
Notifications
You must be signed in to change notification settings - Fork 0
Koanf, commitlint-scope self-food #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #$schema: https://github.com/thumbrise/commitlint-scope/blob/main/docs/schema/config.v3.json | ||
|
|
||
| # Scope parsing customization. Not required, if you follow common conventional header. In example: 'type!(scope): subject' | ||
| #scopeRegex: ^[a-z]+(?:\((?P<scope>[^)]+)\))?!?:\s | ||
|
|
||
| # Patterns list: each item specifies a list of scopes and the corresponding file glob patterns. | ||
| patterns: | ||
| # - scopes: ["auth"] | ||
| # files: ["services/auth/**"] | ||
| # | ||
| # - scopes: ["migrations", "sql"] | ||
| # files: ["database/migrations/*.sql"] | ||
| # | ||
| # - scopes: ["frontend", "assets"] | ||
| # files: ["**/assets/**", "**/frontend/**"] | ||
| # | ||
| # - scopes: ["docs", "md"] | ||
| # files: ["**/*.md"] | ||
| # | ||
| # - scopes: ["some.dot.scope", "any-anotherscope"] | ||
| # files: ["**/rail.v1.json"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,61 +3,70 @@ package validator | |
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "reflect" | ||
| "regexp" | ||
|
|
||
| "github.com/go-viper/mapstructure/v2" | ||
| "github.com/spf13/viper" | ||
| "github.com/knadh/koanf/parsers/yaml" | ||
| "github.com/knadh/koanf/providers/file" | ||
| "github.com/knadh/koanf/v2" | ||
| ) | ||
|
|
||
| const ConfigName = ".commitlint-scope" | ||
| const ConfigName = ".commitlint-scope.yaml" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep the config filename contract consistent. Line 16 turns 🤖 Prompt for AI Agents |
||
|
|
||
| type PatternItem struct { | ||
| Scopes []string `mapstructure:"scopes"` | ||
| Files []string `mapstructure:"files"` | ||
| Scopes []string `koanf:"scopes"` | ||
| Files []string `koanf:"files"` | ||
| } | ||
|
|
||
| type Config struct { | ||
| ScopeRegex *regexp.Regexp `mapstructure:"scopeRegex"` | ||
| Patterns []PatternItem `mapstructure:"patterns"` | ||
| ScopeRegex *regexp.Regexp `koanf:"scopeRegex"` | ||
| Patterns []PatternItem `koanf:"patterns"` | ||
| } | ||
|
|
||
| var ErrConfigRead = errors.New("error reading config") | ||
| var ( | ||
| ErrConfigRead = errors.New("error reading config") | ||
| ErrRegexDecode = errors.New("expected string for regexp decode") | ||
| ) | ||
|
|
||
| func LoadConfig() (Config, error) { | ||
| v := viper.New() | ||
| v.SetConfigName(ConfigName) | ||
| v.AddConfigPath(".") | ||
| v.SetDefault("scopeRegex", regexp.MustCompile(`^[a-z]+(?:\((?P<scope>[^)]+)\))?!?:\s`)) | ||
| k := koanf.New(".") | ||
|
|
||
| var cfg Config | ||
| defaultRegex := `^[a-z]+(?:\((?P<scope>[^)]+)\))?!?:\s` | ||
| _ = k.Set("scopeRegex", defaultRegex) | ||
|
|
||
| if err := v.ReadInConfig(); err != nil { | ||
| if _, ok := errors.AsType[viper.ConfigFileNotFoundError](err); !ok { | ||
| if err := k.Load(file.Provider(ConfigName), yaml.Parser()); err != nil { | ||
| if !os.IsNotExist(err) { | ||
| return Config{}, fmt.Errorf("%w: %w", ErrConfigRead, err) | ||
| } | ||
| } | ||
|
|
||
| if err := v.Unmarshal(&cfg, regexDecode); err != nil { | ||
| var cfg Config | ||
|
|
||
| unmarshalConf := koanf.UnmarshalConf{ | ||
| DecoderConfig: &mapstructure.DecoderConfig{ | ||
| Result: &cfg, | ||
| DecodeHook: regexDecodeHook, | ||
| }, | ||
| } | ||
|
|
||
| if err := k.UnmarshalWithConf("", &cfg, unmarshalConf); err != nil { | ||
| return Config{}, fmt.Errorf("%w: %w", ErrConfigRead, err) | ||
| } | ||
|
|
||
| return cfg, nil | ||
| } | ||
|
|
||
| var ErrRegexDecode = errors.New("expected string for regexp decode") | ||
|
|
||
| func regexDecode(cfg *mapstructure.DecoderConfig) { | ||
| cfg.DecodeHook = func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) { | ||
| if from.Kind() == reflect.String && to == reflect.TypeOf(®exp.Regexp{}) { | ||
| val, ok := data.(string) | ||
| if !ok { | ||
| return nil, fmt.Errorf("%w got %T", ErrRegexDecode, data) | ||
| } | ||
|
|
||
| return regexp.Compile(val) | ||
| func regexDecodeHook(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) { | ||
| if from.Kind() == reflect.String && to == reflect.TypeOf(®exp.Regexp{}) { | ||
| val, ok := data.(string) | ||
| if !ok { | ||
| return nil, fmt.Errorf("%w got %T", ErrRegexDecode, data) | ||
| } | ||
|
|
||
| return data, nil | ||
| return regexp.Compile(val) | ||
| } | ||
|
|
||
| return data, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: thumbrise/commitlint-scope
Length of output: 1530
🏁 Script executed:
Repository: thumbrise/commitlint-scope
Length of output: 207
🌐 Web query:
actions/checkout persist-credentials default true💡 Result:
In GitHub Actions, the actions/checkout input persist-credentials defaults to true (i.e., credentials are persisted unless you set persist-credentials: false). [1][2] What “persist-credentials” does: - When persist-credentials is enabled, actions/checkout stores the auth token in the local git config so later steps can run authenticated git commands; the token is removed during post-job cleanup. [2][1] How to opt out: - Set persist-credentials: false on the checkout step where you don’t need authenticated git operations (e.g., read-only workflows). [2][3]
Citations:
Harden commitlint-scope job supply-chain + token hygiene
actions/checkout@v6to a full commit SHA (line 66) instead of a moving tag.thumbrise/commitlint-scope:latest-alpine(line 75) with an immutable image digest.persist-credentials: falseto the checkout step (currently unset, so it defaults totrue).🧰 Tools
🪛 zizmor (1.25.2)
[warning] 66-68: credential persistence through GitHub Actions artifacts (artipacked): does not set persist-credentials: false
(artipacked)
[error] 66-66: unpinned action reference (unpinned-uses): action is not pinned to a hash (required by blanket policy)
(unpinned-uses)
🤖 Prompt for AI Agents