Skip to content

Commit 9f8d729

Browse files
committed
add sourcing config from default branch
1 parent fc9213f commit 9f8d729

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ require (
103103
github.com/quasoft/websspi v1.1.2
104104
github.com/redis/go-redis/v9 v9.7.3
105105
github.com/robfig/cron/v3 v3.0.1
106+
github.com/rs/zerolog v1.33.0
106107
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
107108
github.com/sassoftware/go-rpmutils v0.4.0
108109
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3
109110
github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92
111+
github.com/spf13/viper v1.20.0
110112
github.com/stretchr/testify v1.10.0
111113
github.com/syndtr/goleveldb v1.0.0
112114
github.com/tstranex/u2f v1.0.0
@@ -286,7 +288,6 @@ require (
286288
github.com/rivo/uniseg v0.4.7 // indirect
287289
github.com/rogpeppe/go-internal v1.14.1 // indirect
288290
github.com/rs/xid v1.6.0 // indirect
289-
github.com/rs/zerolog v1.33.0 // indirect
290291
github.com/russross/blackfriday/v2 v2.1.0 // indirect
291292
github.com/sagikazarmark/locafero v0.8.0 // indirect
292293
github.com/shopspring/decimal v1.4.0 // indirect
@@ -297,7 +298,6 @@ require (
297298
github.com/spf13/afero v1.14.0 // indirect
298299
github.com/spf13/cast v1.7.1 // indirect
299300
github.com/spf13/pflag v1.0.6 // indirect
300-
github.com/spf13/viper v1.20.0 // indirect
301301
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
302302
github.com/subosito/gotenv v1.6.0 // indirect
303303
github.com/tetratelabs/wazero v1.9.0 // indirect

routers/private/hook_pre_receive.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import (
2929

3030
"github.com/gitleaks/go-gitdiff/gitdiff"
3131
"github.com/rs/zerolog"
32+
"github.com/spf13/viper"
3233
"github.com/zricethezav/gitleaks/v8/cmd/scm"
34+
gitleaks_config "github.com/zricethezav/gitleaks/v8/config"
3335
gitleaks "github.com/zricethezav/gitleaks/v8/detect"
3436
gitleaks_log "github.com/zricethezav/gitleaks/v8/logging"
3537
)
@@ -562,10 +564,10 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string,
562564
if newCommitID == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
563565
return
564566
}
565-
566-
detector, err := gitleaks.NewDetectorDefaultConfig()
567+
config, _, _ := git.NewCommand("show").AddDynamicArguments(repo.DefaultBranch+":.gitleaks.toml").RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
568+
detector, err := newDetector(config)
567569
if err != nil {
568-
ctx.Status(http.StatusTeapot)
570+
ctx.JSON(http.StatusTeapot, private.Response{Err: err.Error(), UserMsg: err.Error()})
569571
return
570572
}
571573

@@ -640,3 +642,26 @@ func (g *giteacmd) Wait() (err error) {
640642
func init() {
641643
gitleaks_log.Logger = zerolog.Nop()
642644
}
645+
646+
func newDetector(config string) (*gitleaks.Detector, error) {
647+
viper.SetConfigType("toml")
648+
var err error
649+
if len(config) > 0 {
650+
err = viper.ReadConfig(strings.NewReader(config))
651+
} else {
652+
err = viper.ReadConfig(strings.NewReader(gitleaks_config.DefaultConfig))
653+
}
654+
if err != nil {
655+
return nil, err
656+
}
657+
var vc gitleaks_config.ViperConfig
658+
err = viper.Unmarshal(&vc)
659+
if err != nil {
660+
return nil, err
661+
}
662+
cfg, err := vc.Translate()
663+
if err != nil {
664+
return nil, err
665+
}
666+
return gitleaks.NewDetector(cfg), nil
667+
}

0 commit comments

Comments
 (0)