-
Notifications
You must be signed in to change notification settings - Fork 427
Consolidate ~25 duplicate helper implementations #39720
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
0a1700d
75c2ed5
41893ba
5694e2d
c69d264
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 |
|---|---|---|
|
|
@@ -119,10 +119,7 @@ func DisplayDependencyReport(report *DependencyReport) { | |
| fmt.Fprintf(os.Stderr, "Outdated: %d (%.0f%%)\n", len(report.Outdated), outdatedPercentage) | ||
| fmt.Fprintf(os.Stderr, "Security advisories: %d\n", len(report.Advisories)) | ||
|
|
||
| v0Percentage := 0.0 | ||
| if report.TotalDeps > 0 { | ||
| v0Percentage = float64(report.V0Count) / float64(report.TotalDeps) * 100 | ||
| } | ||
| v0Percentage := safePercent(report.V0Count, report.TotalDeps) | ||
| fmt.Fprintf(os.Stderr, "v0.x dependencies: %d (%.0f%%)", report.V0Count, v0Percentage) | ||
| if v0Percentage > 30 { | ||
| fmt.Fprintf(os.Stderr, " ⚠️") | ||
|
|
@@ -157,16 +154,10 @@ func DisplayDependencyReport(report *DependencyReport) { | |
| } | ||
| fmt.Fprintln(os.Stderr, "") | ||
|
|
||
| v1Percentage := 0.0 | ||
| if report.TotalDeps > 0 { | ||
| v1Percentage = float64(report.V1PlusCount) / float64(report.TotalDeps) * 100 | ||
| } | ||
| v1Percentage := safePercent(report.V1PlusCount, report.TotalDeps) | ||
| fmt.Fprintf(os.Stderr, "v1.x (stable): %d (%.0f%%)\n", report.V1PlusCount, v1Percentage) | ||
|
|
||
| v2Percentage := 0.0 | ||
| if report.TotalDeps > 0 { | ||
| v2Percentage = float64(report.V2PlusCount) / float64(report.TotalDeps) * 100 | ||
| } | ||
| v2Percentage := safePercent(report.V2PlusCount, report.TotalDeps) | ||
| fmt.Fprintf(os.Stderr, "v2+ (mature): %d (%.0f%%)\n", report.V2PlusCount, v2Percentage) | ||
| fmt.Fprintln(os.Stderr, "") | ||
|
|
||
|
|
@@ -201,14 +192,9 @@ func DisplayDependencyReportJSON(report *DependencyReport) error { | |
| outdatedPercentage = float64(len(report.Outdated)) / float64(report.DirectDeps) * 100 | ||
| } | ||
|
|
||
|
Contributor
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. [/zoom-out] 💡 Suggested change// Before (unchanged by this PR, but inconsistent):
outdatedPercentage := 0.0
if report.DirectDeps > 0 {
outdatedPercentage = float64(len(report.Outdated)) / float64(report.DirectDeps) * 100
}
// After:
outdatedPercentage := safePercent(len(report.Outdated), report.DirectDeps)The same pattern also applies to the |
||
| v0Percentage := 0.0 | ||
| v1Percentage := 0.0 | ||
| v2Percentage := 0.0 | ||
| if report.TotalDeps > 0 { | ||
| v0Percentage = float64(report.V0Count) / float64(report.TotalDeps) * 100 | ||
| v1Percentage = float64(report.V1PlusCount) / float64(report.TotalDeps) * 100 | ||
| v2Percentage = float64(report.V2PlusCount) / float64(report.TotalDeps) * 100 | ||
| } | ||
| v0Percentage := safePercent(report.V0Count, report.TotalDeps) | ||
| v1Percentage := safePercent(report.V1PlusCount, report.TotalDeps) | ||
| v2Percentage := safePercent(report.V2PlusCount, report.TotalDeps) | ||
|
|
||
| // Build JSON-friendly output structure | ||
| output := map[string]any{ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,10 +93,7 @@ func CalculateWorkflowHealth(workflowName string, runs []WorkflowRun, threshold | |
| } | ||
|
|
||
| totalRuns := len(runs) | ||
| successRate := 0.0 | ||
| if totalRuns > 0 { | ||
| successRate = float64(successCount) / float64(totalRuns) * 100 | ||
| } | ||
| successRate := safePercent(successCount, totalRuns) | ||
|
|
||
| avgDuration := time.Duration(durationStats.Mean()) | ||
| avgTokens := int(tokenStats.Mean()) | ||
|
|
@@ -175,7 +172,7 @@ func calculateSuccessRate(runs []WorkflowRun) float64 { | |
| } | ||
| } | ||
|
|
||
| return float64(successCount) / float64(len(runs)) * 100 | ||
| return safePercent(successCount, len(runs)) | ||
|
Contributor
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. [/tdd] The NaN→0 behaviour change in 💡 Suggested testfunc TestCalculateSuccessRateEmpty(t *testing.T) {
got := calculateSuccessRate(nil)
if got != 0.0 {
t.Errorf("expected 0.0 for empty runs, got %v", got)
}
}The fix is correct and important — zero-guarding before the division prevents NaN propagation into health summary structs. Anchoring it with a test makes the intent explicit and prevents future regression. |
||
| } | ||
|
|
||
| // CalculateHealthSummary calculates aggregated health metrics across all workflows | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import ( | |
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/github/gh-aw/pkg/sliceutil" | ||
| ) | ||
|
|
||
| // TestRenderLogsConsoleUnified tests the unified console rendering | ||
|
|
@@ -222,7 +224,7 @@ func TestAddUniqueWorkflow(t *testing.T) { | |
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := addUniqueWorkflow(tt.workflows, tt.workflow) | ||
| result := sliceutil.MergeUnique(tt.workflows, tt.workflow) | ||
|
Contributor
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. [/zoom-out] 💡 Suggested fixOption A — rename in place: // TestMergeUniqueDeduplicatesWorkflows verifies that MergeUnique correctly
// deduplicates workflow names as previously guaranteed by addUniqueWorkflow.
func TestMergeUniqueDeduplicatesWorkflows(t *testing.T) {Option B — remove it and rely on |
||
| if len(result) != len(tt.expected) { | ||
| t.Errorf("Expected length %d, got %d", len(tt.expected), len(result)) | ||
| } | ||
|
|
@@ -429,7 +431,7 @@ func TestAggregateSummaryItems(t *testing.T) { | |
| }, | ||
| func(summary *MissingToolSummary, tool MissingToolReport) { | ||
| summary.Count++ | ||
| summary.Workflows = addUniqueWorkflow(summary.Workflows, tool.WorkflowName) | ||
| summary.Workflows = sliceutil.MergeUnique(summary.Workflows, tool.WorkflowName) | ||
| summary.RunIDs = append(summary.RunIDs, tool.RunID) | ||
| }, | ||
| func(summary *MissingToolSummary) { | ||
|
|
||
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.
[/improve-codebase-architecture]
formatAnomalyTagandformatAnomalyNoteare string-formatting helpers, not arithmetic — placing them inaudit_math_helpers.gostretches the file beyond its conceptual boundary. The math helpers file already containssafePercent,formatPercent,formatVolumeChange, andformatFloatDelta; the anomaly helpers belong in a separateaudit_format_helpers.goalongsiderenderFirewallDiff*concerns, or at least noted as a deliberate placement choice.💡 Suggestion
Move both functions to
pkg/cli/audit_format_helpers.go(new file) so thataudit_math_helpers.gostays focused on numeric calculations and the anomaly formatting has a natural home near the render functions that use it.