refactor: reduce CRAP scores in router/workflow/notification
ci / test (push) Successful in 3m42s

- llm_router.go: Extract getStringFromMap, firstNonEmpty, paramResolver
  - buildCronSpec: 12 → 4 complexity
  - buildParameters: 9 → 5 complexity

- routing_workflow.go: Extract stateMachine, stateResult types
  - RoutingWorkflow: 11 → 6 complexity
  - Separate executeTask/executePass/executeFail

- notification.go: Extract checker interface pattern
  - DeploymentPreCheckActivity: 10 → 5 complexity
  - goCheckers() returns language-specific checkers

- Added 7 new test cases for helper functions
- Coverage: internal/routing 63.6% → 66.0%
This commit is contained in:
Test
2026-09-03 08:50:21 -07:00
parent a0e64224a7
commit 1c16869126
6 changed files with 487 additions and 169 deletions
+76 -31
View File
@@ -203,6 +203,70 @@ type DeploymentPreCheckOutput struct {
Warnings []string `json:"warnings"`
}
// checker defines a deployment check
type checker struct {
name string
types []string // which checkTypes trigger this checker
isWarning bool // if true, failure goes to warnings not failures
run func(ctx context.Context, path string) (string, error)
}
// shouldRun checks if this checker should run for given checkType
func (c *checker) shouldRun(checkType string) bool {
for _, t := range c.types {
if t == checkType {
return true
}
}
return false
}
// goCheckers returns checkers for Go projects
func goCheckers() []*checker {
return []*checker{
{
name: "build",
types: []string{"all", "build"},
run: func(ctx context.Context, path string) (string, error) {
cmd := exec.CommandContext(ctx, "go", "build", "./...")
cmd.Dir = path
out, err := cmd.CombinedOutput()
return string(out), err
},
},
{
name: "test",
types: []string{"all", "test"},
run: func(ctx context.Context, path string) (string, error) {
cmd := exec.CommandContext(ctx, "go", "test", "-short", "./...")
cmd.Dir = path
out, err := cmd.CombinedOutput()
return string(out), err
},
},
{
name: "lint",
types: []string{"all", "lint"},
isWarning: true,
run: func(ctx context.Context, path string) (string, error) {
cmd := exec.CommandContext(ctx, "go", "vet", "./...")
cmd.Dir = path
out, err := cmd.CombinedOutput()
return string(out), err
},
},
}
}
// detectProjectCheckers returns checkers based on project type
func detectProjectCheckers(path string) []*checker {
if _, err := os.Stat(fmt.Sprintf("%s/go.mod", path)); err == nil {
return goCheckers()
}
// Add more project types here (Node, Python, etc.)
return nil
}
// DeploymentPreCheckActivity validates deployment readiness
func DeploymentPreCheckActivity(ctx context.Context, in DeploymentPreCheckInput) (DeploymentPreCheckOutput, error) {
logger := newActivityLogger(ctx)
@@ -226,38 +290,19 @@ func DeploymentPreCheckActivity(ctx context.Context, in DeploymentPreCheckInput)
return output, nil
}
// Check for Go project
isGo := false
if _, err := os.Stat(fmt.Sprintf("%s/go.mod", in.Path)); err == nil {
isGo = true
}
if isGo && (checkType == "all" || checkType == "build") {
// Try go build
cmd := exec.CommandContext(ctx, "go", "build", "./...")
cmd.Dir = in.Path
if buildOut, err := cmd.CombinedOutput(); err != nil {
output.Passed = false
output.Failures = append(output.Failures, fmt.Sprintf("build failed: %s", string(buildOut)))
// Run applicable checkers
for _, c := range detectProjectCheckers(in.Path) {
if !c.shouldRun(checkType) {
continue
}
}
if isGo && (checkType == "all" || checkType == "test") {
// Try go test
cmd := exec.CommandContext(ctx, "go", "test", "-short", "./...")
cmd.Dir = in.Path
if testOut, err := cmd.CombinedOutput(); err != nil {
output.Passed = false
output.Failures = append(output.Failures, fmt.Sprintf("tests failed: %s", string(testOut)))
}
}
if isGo && (checkType == "all" || checkType == "lint") {
// Try go vet
cmd := exec.CommandContext(ctx, "go", "vet", "./...")
cmd.Dir = in.Path
if vetOut, err := cmd.CombinedOutput(); err != nil {
output.Warnings = append(output.Warnings, fmt.Sprintf("vet issues: %s", string(vetOut)))
if out, err := c.run(ctx, in.Path); err != nil {
msg := fmt.Sprintf("%s failed: %s", c.name, out)
if c.isWarning {
output.Warnings = append(output.Warnings, msg)
} else {
output.Passed = false
output.Failures = append(output.Failures, msg)
}
}
}