Skip to content

Commit 5694e2d

Browse files
Copilotpelikhan
andauthored
P2.1, P2.2, P2.3: Local duplicate consolidation
P2.1: Collapsed byte-identical templatable env-var builders - Extracted buildTemplatableEnvVar from buildTemplatableBoolEnvVar and buildTemplatableIntEnvVar - Kept original function names as thin wrappers for readability P2.2: Extracted anomaly-marker formatting (8 sites) - Added formatAnomalyTag(bool) string to audit_math_helpers.go - Added formatAnomalyNote(bool, string) string to audit_math_helpers.go - Replaced all 8 inline anomaly formatting blocks in audit_diff_render.go P2.3: Used sortedMapKeys at 3 map[string]string sites - safe_outputs_config_helpers.go - safe_scripts.go - safe_outputs_app_config.go All changes build successfully Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent 41893ba commit 5694e2d

6 files changed

Lines changed: 41 additions & 60 deletions

File tree

pkg/cli/audit_diff_render.go

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ func renderFirewallDiffMarkdownSection(diff *FirewallDiff) {
150150
for _, entry := range diff.NewDomains {
151151
total := entry.Run2Allowed + entry.Run2Blocked
152152
statusIcon := firewallStatusEmoji(entry.Run2Status)
153-
anomalyTag := ""
154-
if entry.IsAnomaly {
155-
anomalyTag = " ⚠️"
156-
}
153+
anomalyTag := formatAnomalyTag(entry.IsAnomaly)
157154
fmt.Fprintf(os.Stdout, "- %s `%s` (%d requests, %s)%s\n", statusIcon, entry.Domain, total, entry.Run2Status, anomalyTag)
158155
}
159156
fmt.Fprintln(os.Stdout)
@@ -173,10 +170,7 @@ func renderFirewallDiffMarkdownSection(diff *FirewallDiff) {
173170
for _, entry := range diff.StatusChanges {
174171
icon1 := firewallStatusEmoji(entry.Run1Status)
175172
icon2 := firewallStatusEmoji(entry.Run2Status)
176-
anomalyTag := ""
177-
if entry.IsAnomaly {
178-
anomalyTag = " ⚠️"
179-
}
173+
anomalyTag := formatAnomalyTag(entry.IsAnomaly)
180174
fmt.Fprintf(os.Stdout, "- `%s`: %s %s → %s %s%s\n", entry.Domain, icon1, entry.Run1Status, icon2, entry.Run2Status, anomalyTag)
181175
}
182176
fmt.Fprintln(os.Stdout)
@@ -205,10 +199,7 @@ func renderMCPToolsDiffMarkdownSection(diff *MCPToolsDiff) {
205199
if len(diff.NewTools) > 0 {
206200
fmt.Fprintf(os.Stdout, "**New tools (%d)**\n", len(diff.NewTools))
207201
for _, entry := range diff.NewTools {
208-
anomalyTag := ""
209-
if entry.IsAnomaly {
210-
anomalyTag = " ⚠️"
211-
}
202+
anomalyTag := formatAnomalyTag(entry.IsAnomaly)
212203
fmt.Fprintf(os.Stdout, "- `%s/%s` (%d calls)%s\n", entry.ServerName, entry.ToolName, entry.Run2CallCount, anomalyTag)
213204
}
214205
fmt.Fprintln(os.Stdout)
@@ -225,10 +216,7 @@ func renderMCPToolsDiffMarkdownSection(diff *MCPToolsDiff) {
225216
if len(diff.ChangedTools) > 0 {
226217
fmt.Fprintf(os.Stdout, "**Changed tools (%d)**\n", len(diff.ChangedTools))
227218
for _, entry := range diff.ChangedTools {
228-
anomalyTag := ""
229-
if entry.IsAnomaly {
230-
anomalyTag = " ⚠️"
231-
}
219+
anomalyTag := formatAnomalyTag(entry.IsAnomaly)
232220
errInfo := ""
233221
if entry.Run1ErrorCount > 0 || entry.Run2ErrorCount > 0 {
234222
errInfo = fmt.Sprintf(", errors: %d → %d", entry.Run1ErrorCount, entry.Run2ErrorCount)
@@ -327,10 +315,7 @@ func renderFirewallDiffPrettySection(diff *FirewallDiff) {
327315
}
328316
for _, entry := range diff.NewDomains {
329317
total := entry.Run2Allowed + entry.Run2Blocked
330-
anomalyNote := ""
331-
if entry.IsAnomaly {
332-
anomalyNote = "⚠️ " + entry.AnomalyNote
333-
}
318+
anomalyNote := formatAnomalyNote(entry.IsAnomaly, entry.AnomalyNote)
334319
config.Rows = append(config.Rows, []string{
335320
entry.Domain,
336321
firewallStatusEmoji(entry.Run2Status) + " " + entry.Run2Status,
@@ -365,10 +350,7 @@ func renderFirewallDiffPrettySection(diff *FirewallDiff) {
365350
Rows: make([][]string, 0, len(diff.StatusChanges)),
366351
}
367352
for _, entry := range diff.StatusChanges {
368-
anomalyNote := ""
369-
if entry.IsAnomaly {
370-
anomalyNote = "⚠️ " + entry.AnomalyNote
371-
}
353+
anomalyNote := formatAnomalyNote(entry.IsAnomaly, entry.AnomalyNote)
372354
config.Rows = append(config.Rows, []string{
373355
entry.Domain,
374356
firewallStatusEmoji(entry.Run1Status) + " " + entry.Run1Status,
@@ -415,10 +397,7 @@ func renderMCPToolsDiffPrettySection(diff *MCPToolsDiff) {
415397
Rows: make([][]string, 0, len(diff.NewTools)),
416398
}
417399
for _, entry := range diff.NewTools {
418-
anomalyNote := ""
419-
if entry.IsAnomaly {
420-
anomalyNote = "⚠️ " + entry.AnomalyNote
421-
}
400+
anomalyNote := formatAnomalyNote(entry.IsAnomaly, entry.AnomalyNote)
422401
config.Rows = append(config.Rows, []string{
423402
entry.ServerName,
424403
entry.ToolName,
@@ -452,10 +431,7 @@ func renderMCPToolsDiffPrettySection(diff *MCPToolsDiff) {
452431
Rows: make([][]string, 0, len(diff.ChangedTools)),
453432
}
454433
for _, entry := range diff.ChangedTools {
455-
anomalyNote := ""
456-
if entry.IsAnomaly {
457-
anomalyNote = "⚠️ " + entry.AnomalyNote
458-
}
434+
anomalyNote := formatAnomalyNote(entry.IsAnomaly, entry.AnomalyNote)
459435
config.Rows = append(config.Rows, []string{
460436
entry.ServerName,
461437
entry.ToolName,

pkg/cli/audit_math_helpers.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ func formatFloatDelta(value1, value2 float64) string {
5757
}
5858
return fmt.Sprintf("%.3f", delta)
5959
}
60+
61+
// formatAnomalyTag returns a warning emoji suffix for markdown rendering
62+
// when isAnomaly is true, otherwise returns an empty string.
63+
func formatAnomalyTag(isAnomaly bool) string {
64+
if isAnomaly {
65+
return " ⚠️"
66+
}
67+
return ""
68+
}
69+
70+
// formatAnomalyNote returns a formatted anomaly note for table rendering
71+
// with a warning emoji prefix when isAnomaly is true, otherwise returns an empty string.
72+
func formatAnomalyNote(isAnomaly bool, anomalyNote string) string {
73+
if isAnomaly {
74+
return "⚠️ " + anomalyNote
75+
}
76+
return ""
77+
}

pkg/workflow/safe_outputs_app_config.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,7 @@ func (c *Compiler) buildGitHubAppTokenMintStepWithMeta(app *GitHubAppConfig, per
293293
}
294294

295295
// Extract and sort keys for deterministic ordering
296-
keys := make([]string, 0, len(permissionFields))
297-
for key := range permissionFields {
298-
keys = append(keys, key)
299-
}
300-
sort.Strings(keys)
296+
keys := sortedMapKeys(permissionFields)
301297

302298
// Add permissions in sorted order
303299
for _, key := range keys {

pkg/workflow/safe_outputs_config_helpers.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package workflow
22

33
import (
44
"encoding/json"
5-
"sort"
65

76
"github.com/github/gh-aw/pkg/logger"
87
"github.com/github/gh-aw/pkg/stringutil"
@@ -24,11 +23,7 @@ func buildNormalizedSortedJSON(names []string, valueFn func(string) string) (str
2423
values[normalizedName] = valueFn(normalizedName)
2524
}
2625

27-
keys := make([]string, 0, len(values))
28-
for k := range values {
29-
keys = append(keys, k)
30-
}
31-
sort.Strings(keys)
26+
keys := sortedMapKeys(values)
3227

3328
ordered := make(map[string]string, len(keys))
3429
for _, k := range keys {

pkg/workflow/safe_scripts.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package workflow
22

33
import (
44
"encoding/json"
5-
"sort"
65
"strings"
76

87
"github.com/github/gh-aw/pkg/logger"
@@ -105,11 +104,7 @@ func buildCustomSafeOutputScriptsJSON(data *WorkflowData) string {
105104
}
106105

107106
// Sort keys for deterministic output
108-
keys := make([]string, 0, len(scriptMapping))
109-
for k := range scriptMapping {
110-
keys = append(keys, k)
111-
}
112-
sort.Strings(keys)
107+
keys := sortedMapKeys(scriptMapping)
113108

114109
ordered := make(map[string]string, len(keys))
115110
for _, k := range keys {

pkg/workflow/templatables.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ func (t *TemplatableBool) String() string {
156156
return string(*t)
157157
}
158158

159-
// buildTemplatableBoolEnvVar returns a YAML environment variable entry for a
160-
// templatable boolean field. If value is a GitHub Actions expression it is
159+
// buildTemplatableEnvVar returns a YAML environment variable entry for a
160+
// templatable field. If value is a GitHub Actions expression it is
161161
// embedded unquoted so that GitHub Actions can evaluate it at runtime;
162162
// otherwise the literal string is quoted. Returns nil if value is nil.
163-
func buildTemplatableBoolEnvVar(envVarName string, value *string) []string {
163+
func buildTemplatableEnvVar(envVarName string, value *string) []string {
164164
if value == nil {
165165
return nil
166166
}
@@ -171,6 +171,14 @@ func buildTemplatableBoolEnvVar(envVarName string, value *string) []string {
171171
return []string{fmt.Sprintf(" %s: %q\n", envVarName, v)}
172172
}
173173

174+
// buildTemplatableBoolEnvVar returns a YAML environment variable entry for a
175+
// templatable boolean field. If value is a GitHub Actions expression it is
176+
// embedded unquoted so that GitHub Actions can evaluate it at runtime;
177+
// otherwise the literal string is quoted. Returns nil if value is nil.
178+
func buildTemplatableBoolEnvVar(envVarName string, value *string) []string {
179+
return buildTemplatableEnvVar(envVarName, value)
180+
}
181+
174182
// AddTemplatableBool adds a templatable boolean field to the handler config.
175183
//
176184
// The stored JSON value depends on the content of *value:
@@ -200,14 +208,7 @@ func (b *handlerConfigBuilder) AddTemplatableBool(key string, value *string) *ha
200208
// embedded unquoted so that GitHub Actions can evaluate it at runtime;
201209
// otherwise the literal string is quoted. Returns nil if value is nil.
202210
func buildTemplatableIntEnvVar(envVarName string, value *string) []string {
203-
if value == nil {
204-
return nil
205-
}
206-
v := *value
207-
if isExpression(v) {
208-
return []string{fmt.Sprintf(" %s: %s\n", envVarName, v)}
209-
}
210-
return []string{fmt.Sprintf(" %s: %q\n", envVarName, v)}
211+
return buildTemplatableEnvVar(envVarName, value)
211212
}
212213

213214
// AddTemplatableInt adds a templatable integer field to the handler config.

0 commit comments

Comments
 (0)