|
7 | 7 |
|
8 | 8 | "github.com/infracost/go-proto/pkg/event" |
9 | 9 | "github.com/infracost/go-proto/pkg/rat" |
| 10 | + protoevent "github.com/infracost/proto/gen/go/infracost/parser/event" |
10 | 11 | "github.com/infracost/proto/gen/go/infracost/provider" |
11 | 12 | ) |
12 | 13 |
|
@@ -234,7 +235,7 @@ func (data *Data) processGuardrailResults(inputs *Inputs) { |
234 | 235 | entry := GovernanceEntry{ |
235 | 236 | Title: title, |
236 | 237 | Blocking: result.BlockPR, |
237 | | - Message: formatGuardrailMessage(result), |
| 238 | + Message: formatGuardrailMessage(result, data.Currency), |
238 | 239 | } |
239 | 240 |
|
240 | 241 | table.Entries = append(table.Entries, entry) |
@@ -438,19 +439,57 @@ func formatTagIssues(resource event.TagPolicyResultResource) []string { |
438 | 439 | return issues |
439 | 440 | } |
440 | 441 |
|
441 | | -// formatGuardrailMessage builds the trigger description for a guardrail result. |
442 | | -func formatGuardrailMessage(result event.GuardrailResult) string { |
| 442 | +// formatGuardrailMessage builds the trigger description for a guardrail result, |
| 443 | +// matching the dashboard's buildTriggerReason logic. |
| 444 | +// See: dashboard/api/src/services/guardrails.ts buildTriggerReason (~line 1008) |
| 445 | +func formatGuardrailMessage(result event.GuardrailResult, currency string) string { |
443 | 446 | var parts []string |
444 | 447 |
|
445 | | - if result.Increase != nil && result.Increase.GreaterThanZero() { |
446 | | - parts = append(parts, fmt.Sprintf("Cost increase: %s/mo", result.Increase.StringFixed(2))) |
| 448 | + if result.Scope == protoevent.Guardrail_PROJECT { |
| 449 | + parts = append(parts, "At least one project exceeded per-project threshold.") |
447 | 450 | } |
448 | | - if result.PercentIncrease != nil && result.PercentIncrease.GreaterThanZero() { |
449 | | - parts = append(parts, fmt.Sprintf("(%s%%)", result.PercentIncrease.StringFixed(0))) |
| 451 | + |
| 452 | + hasIncrease := result.IncreaseThreshold != nil |
| 453 | + hasPercent := result.IncreasePercentThreshold != nil |
| 454 | + |
| 455 | + if hasIncrease { |
| 456 | + cost := formatCost(result.Increase, currency) |
| 457 | + if hasPercent { |
| 458 | + parts = append(parts, fmt.Sprintf( |
| 459 | + "Cost increased by %s (%s%%), threshold was %s and %s%%.", |
| 460 | + cost, |
| 461 | + result.PercentIncrease.StringFixed(0), |
| 462 | + formatCost(result.IncreaseThreshold, currency), |
| 463 | + result.IncreasePercentThreshold.StringFixed(0), |
| 464 | + )) |
| 465 | + } else { |
| 466 | + parts = append(parts, fmt.Sprintf( |
| 467 | + "Cost increased by %s, threshold was %s.", |
| 468 | + cost, |
| 469 | + formatCost(result.IncreaseThreshold, currency), |
| 470 | + )) |
| 471 | + } |
| 472 | + } else if hasPercent { |
| 473 | + if result.PercentIncrease != nil && !result.PercentIncrease.IsZero() { |
| 474 | + parts = append(parts, fmt.Sprintf( |
| 475 | + "Cost increased by %s (%s%%), threshold was %s%%.", |
| 476 | + formatCost(result.Increase, currency), |
| 477 | + result.PercentIncrease.StringFixed(0), |
| 478 | + result.IncreasePercentThreshold.StringFixed(0), |
| 479 | + )) |
| 480 | + } |
| 481 | + } |
| 482 | + |
| 483 | + if result.TotalThreshold != nil { |
| 484 | + parts = append(parts, fmt.Sprintf( |
| 485 | + "New monthly cost was %s, threshold was %s.", |
| 486 | + formatCost(result.TotalMonthlyCost, currency), |
| 487 | + formatCost(result.TotalThreshold, currency), |
| 488 | + )) |
450 | 489 | } |
451 | 490 |
|
452 | | - if len(result.TriggeringProjectNames) > 0 { |
453 | | - parts = append(parts, fmt.Sprintf("in %s", formatProjectNamesLabel(result.TriggeringProjectNames))) |
| 491 | + if result.Message != "" { |
| 492 | + parts = append(parts, result.Message) |
454 | 493 | } |
455 | 494 |
|
456 | 495 | return strings.Join(parts, " ") |
|
0 commit comments