Skip to content
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

Print policy violations only if any rule was violated (AST-66024) #879

Merged
merged 13 commits into from
Sep 22, 2024
23 changes: 16 additions & 7 deletions internal/commands/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,13 +791,22 @@ func writeConsoleSummary(summary *wrappers.ResultSummary, featureFlagsWrapper wr
}

func printPoliciesSummary(summary *wrappers.ResultSummary) {
fmt.Printf(tableLine + "\n")
if summary.Policies.BreakBuild {
fmt.Printf(" Policy Management Violation - Break Build Enabled: \n")
} else {
fmt.Printf(" Policy Management Violation: \n")
}
hasViolations := false
if len(summary.Policies.Policies) > 0 {
AlvoBen marked this conversation as resolved.
Show resolved Hide resolved
for _, policy := range summary.Policies.Policies {
AlvoBen marked this conversation as resolved.
Show resolved Hide resolved
if len(policy.RulesViolated) > 0 {
hasViolations = true
break
}
}
}
if hasViolations {
AlvoBen marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf(tableLine + "\n")
if summary.Policies.BreakBuild {
fmt.Printf(" Policy Management Violation - Break Build Enabled: \n")
} else {
fmt.Printf(" Policy Management Violation: \n")
}
for _, police := range summary.Policies.Policies {
if len(police.RulesViolated) > 0 {
fmt.Printf(" Policy: %s | Break Build: %t | Violated Rules: ", police.Name, police.BreakBuild)
Expand All @@ -807,8 +816,8 @@ func printPoliciesSummary(summary *wrappers.ResultSummary) {
}
fmt.Printf("\n")
}
fmt.Printf("\n")
AlvoBen marked this conversation as resolved.
Show resolved Hide resolved
}
fmt.Printf("\n")
}

func printAPIsSecuritySummary(summary *wrappers.ResultSummary) {
Expand Down
29 changes: 29 additions & 0 deletions internal/commands/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package commands

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -1138,3 +1139,31 @@ func createEmptyResultSummary() *wrappers.ResultSummary {
},
}
}
func TestPrintPoliciesSummary_WhenNoRolViolated_ShouldNotContainPolicyViolation(t *testing.T) {
summary := &wrappers.ResultSummary{
Policies: &wrappers.PolicyResponseModel{
Status: "Success",
Policies: []wrappers.Policy{
{
RulesViolated: []string{},
},
},
BreakBuild: false,
},
}
r, w, _ := os.Pipe()
old := os.Stdout
os.Stdout = w

printPoliciesSummary(summary)

w.Close()
os.Stdout = old

var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
t.Fatalf("failed to copy output: %v", err) // Handle the error if io.Copy fails
}
output := buf.String()
assert.Assert(t, !strings.Contains(output, "Policy Management Violation "), "Output should not contain 'Policy Management Violation'")
}
3 changes: 3 additions & 0 deletions internal/commands/util/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ func policiesToPrPolicies(policy *wrappers.PolicyResponseModel) []wrappers.PrPol
var prPolicies []wrappers.PrPolicy
if policy != nil {
for _, policy := range policy.Policies {
if len(policy.RulesViolated) == 0 {
continue
}
prPolicy := wrappers.PrPolicy{}
prPolicy.Name = policy.Name
prPolicy.BreakBuild = policy.BreakBuild
Expand Down
7 changes: 7 additions & 0 deletions internal/commands/util/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ func TestIfScanRunning_WhenScanDone_ShouldReturnFalse(t *testing.T) {
scanRunning, _ := isScanRunningOrQueued(scansMockWrapper, "ScanNotRunning")
asserts.False(t, scanRunning)
}

func TestPRDecorationGithub_WhenNoViolatedPolicies_ShouldNotReturnPolicy(t *testing.T) {
prMockWrapper := &mock.PolicyMockWrapper{}
policyResponse, _, _ := prMockWrapper.EvaluatePolicy(nil)
prPolicy := policiesToPrPolicies(policyResponse)
asserts.True(t, len(prPolicy) == 0)
}
Loading