Skip to content

Commit 6fef244

Browse files
committed
feat: add AutoBuild(Failed)?Count
1 parent a42806c commit 6fef244

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

cmd/murphy/internal/internalcmd/scanner_scan.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"os"
8+
"path/filepath"
9+
"time"
10+
711
"github.com/murphysecurity/murphysec/cmd/murphy/internal/common"
812
"github.com/murphysecurity/murphysec/env"
913
"github.com/murphysecurity/murphysec/infra/exitcode"
@@ -15,9 +19,6 @@ import (
1519
"github.com/murphysecurity/murphysec/utils"
1620
"github.com/murphysecurity/murphysec/utils/must"
1721
"github.com/spf13/cobra"
18-
"os"
19-
"path/filepath"
20-
"time"
2122
)
2223

2324
func scannerScanCmd() *cobra.Command {
@@ -76,13 +77,17 @@ func scannerScanRun(cmd *cobra.Command, args []string) {
7677
ScannerShouldEnableMavenBackupScan bool `json:"scanner_should_enable_maven_backup_scan"`
7778
ScannerShouldEnableGradleBackupScan bool `json:"scanner_should_enable_gradle_backup_scan"`
7879
ScanWarnings []scanerr.Param `json:"scan_warnings"`
80+
AutoBuildCount int `json:"auto_build_count"`
81+
AutoBuildFailedCount int `json:"auto_build_failed_count"`
7982
}
8083
w := wrapper{
8184
Modules: utils.NoNilSlice(scantask.Modules),
8285
ComponentCodeFragment: utils.NoNilSlice(scantask.CodeFragments),
8386
ScannerShouldEnableMavenBackupScan: env.ScannerShouldEnableMavenBackupScan,
8487
ScannerShouldEnableGradleBackupScan: env.ScannerShouldEnableGradleBackupScan,
8588
ScanWarnings: scanerr.GetAll(ctx),
89+
AutoBuildCount: scantask.AutoBuildCount,
90+
AutoBuildFailedCount: scantask.AutoBuildFailedCount,
8691
}
8792
if env.WaitAfterScannerScan {
8893
logger.Warn("client will wait here!")

model/inspection_task.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
type InspectionTask struct {
99
scanTask *ScanTask
1010
inspectionDir string
11+
noCopy
1112
}
1213

1314
func (i *InspectionTask) MavenModuleName() []string {
@@ -63,3 +64,27 @@ func (i *InspectionTask) AddModule(module Module) {
6364
module.ModulePath = filepath.ToSlash(module.ModulePath)
6465
i.scanTask.Modules = append(i.scanTask.Modules, module)
6566
}
67+
68+
type RegisteredAutoBuild struct {
69+
task *ScanTask
70+
marked bool
71+
noCopy
72+
}
73+
74+
func (r *RegisteredAutoBuild) MarkFailed() {
75+
if r.marked {
76+
return
77+
}
78+
r.marked = true
79+
r.task.AutoBuildFailedCount++
80+
}
81+
82+
func (r *RegisteredAutoBuild) MarkDisabled() {
83+
r.marked = true
84+
r.task.AutoBuildCount--
85+
}
86+
87+
func (i *InspectionTask) RegisterAutoBuild() *RegisteredAutoBuild {
88+
i.scanTask.AutoBuildCount++
89+
return &RegisteredAutoBuild{task: i.scanTask}
90+
}

model/nocopy.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package model
2+
3+
type noCopy struct{}
4+
5+
func (*noCopy) Lock() {}
6+
func (*noCopy) Unlock() {}

model/scantask.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ type ScanTask struct {
2222
IsInternalCmd bool
2323
IsAutonomous bool
2424
MavenModuleName []string
25+
26+
AutoBuildCount int
27+
28+
AutoBuildFailedCount int
2529
}
2630

2731
func (s *ScanTask) BuildInspectionTask(dir string) *InspectionTask {

module/gradle/gradle.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
"context"
66
_ "embed"
77
"fmt"
8+
"io"
9+
"io/fs"
10+
"os"
11+
"path"
12+
"path/filepath"
13+
"strings"
14+
"sync"
15+
816
"github.com/murphysecurity/murphysec/env"
917
"github.com/murphysecurity/murphysec/infra/logctx"
1018
"github.com/murphysecurity/murphysec/infra/sl"
@@ -14,13 +22,6 @@ import (
1422
"golang.org/x/exp/slices"
1523
"golang.org/x/sync/errgroup"
1624
"gopkg.in/yaml.v3"
17-
"io"
18-
"io/fs"
19-
"os"
20-
"path"
21-
"path/filepath"
22-
"strings"
23-
"sync"
2425
)
2526

2627
type Inspector struct{}
@@ -47,9 +48,11 @@ func (Inspector) InspectProject(ctx context.Context) error {
4748
useGradle = false
4849
}
4950
if useGradle {
51+
registeredAutoBuild := task.RegisterAutoBuild()
5052
logger.Info(gradleEnv.Version.String())
5153
rs, e = evalGradleDependencies(ctx, dir, gradleEnv)
5254
if e != nil {
55+
registeredAutoBuild.MarkFailed()
5356
logger.Warnf("gradle failed: %s", e)
5457
}
5558
}

module/maven/maven.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ func ScanMavenProject(ctx context.Context, task *model.InspectionTask) ([]model.
4949
var useBackupResolver = false
5050
var deps *DepsMap
5151

52+
registeredAutoBuild := task.RegisterAutoBuild()
5253
// check maven version, skip maven scan if check fail
5354
mvnCmdInfo, e := CheckMvnCommand(ctx, task.IsNoBuild())
5455
if e != nil {
5556
if errors.Is(e, ErrMvnDisabled) {
5657
scanerr.Add(ctx, scanerr.Param{Kind: scanerr.KindBuildDisabled})
58+
registeredAutoBuild.MarkDisabled()
5759
} else if errors.Is(e, ErrMvnNotFound) {
60+
registeredAutoBuild.MarkFailed()
5861
scanerr.Add(ctx, scanerr.Param{Kind: scanerr.KindMavenNotFound})
5962
log.Sugar().Warnf("Mvn command not found %v", e)
6063
}
@@ -64,6 +67,7 @@ func ScanMavenProject(ctx context.Context, task *model.InspectionTask) ([]model.
6467
var e error
6568
deps, e = ScanDepsByPluginCommand(ctx, dir, mvnCmdInfo)
6669
if e != nil {
70+
registeredAutoBuild.MarkFailed()
6771
log.Error("Scan maven dependencies failed", zap.Error(e))
6872
useBackupResolver = true
6973
}

0 commit comments

Comments
 (0)