Skip to content

Commit

Permalink
fix(ignore): apply sorting when displaying all ignores (#1407)
Browse files Browse the repository at this point in the history
* fix(ignore): apply sorting when displaying all ignores

* test: add some coverage for ignore

* chore: pr feedback
  • Loading branch information
gotbadger authored Nov 20, 2023
1 parent 629757b commit 086b4df
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 3 deletions.
42 changes: 42 additions & 0 deletions e2e/ignore/.snapshots/TestShowAll-show-all
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

--

5b926801859e02e36224dc3a648a2fda_0
├─ Ignored At: 2023-10-13T14:57:07Z
├─ Author: gotbadger
├─ False positive? Yes
└─ Comment: variable is safe

dc369468d8a5d5bb522d023ae7485376_0
├─ Ignored At: 2023-11-13T14:45:21Z
├─ Author: gotbadger
├─ False positive? Yes
└─ Comment: secret placeholder

fff78d76f2c44a5768dcb4d55a3bd276_0
├─ Ignored At: 2023-11-13T14:48:31Z
├─ Author: gotbadger
├─ False positive? No
└─ Comment: data hash

23d5ed741c83e2e54cc77bdf597bd15d_0
├─ Ignored At: 2023-11-13T14:52:08Z
├─ Author: gotbadger
├─ False positive? Yes
└─ Comment: token generation

68a86d90f28db878612eb5f699c06543_0
├─ Ignored At: 2023-11-13T15:00:28Z
├─ Author: gotbadger
├─ False positive? No
└─ Comment: low risk

6c3dd0e4ba8f4c427dffaea6c6696986_0
├─ Ignored At: 2023-11-15T11:51:54Z
├─ Author: gotbadger
├─ False positive? Yes
└─ Comment: token gen




10 changes: 10 additions & 0 deletions e2e/ignore/.snapshots/TestShowIndividual-show-individual
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

--

68a86d90f28db878612eb5f699c06543_0
├─ Ignored At: 2023-11-13T15:00:28Z
├─ Author: gotbadger
├─ False positive? No
└─ Comment: low risk


46 changes: 46 additions & 0 deletions e2e/ignore/ignore_show_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ignore_test

import (
"path/filepath"
"testing"

"github.com/bearer/bearer/e2e/internal/testhelper"
)

func newIgnoreTest(name string, arguments []string) testhelper.TestCase {
arguments = append([]string{
"ignore"},
arguments...,
)
return testhelper.NewTestCase(name, arguments, testhelper.TestCaseOptions{
DisplayProgressBar: true,
DisplayStdErr: true,
IgnoreForce: true,
})
}

func TestShowAll(t *testing.T) {
tests := []testhelper.TestCase{
newIgnoreTest("show-all", []string{
"show",
"--all",
"--ignore-file",
filepath.Join("e2e", "ignore", "testdata/test.ignore"),
}),
}

testhelper.RunTests(t, tests)
}

func TestShowIndividual(t *testing.T) {
tests := []testhelper.TestCase{
newIgnoreTest("show-individual", []string{
"show",
"68a86d90f28db878612eb5f699c06543_0",
"--ignore-file",
filepath.Join("e2e", "ignore", "testdata/test.ignore"),
}),
}

testhelper.RunTests(t, tests)
}
38 changes: 38 additions & 0 deletions e2e/ignore/testdata/test.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"23d5ed741c83e2e54cc77bdf597bd15d_0": {
"author": "gotbadger",
"comment": "token generation",
"false_positive": true,
"ignored_at": "2023-11-13T14:52:08Z"
},
"5b926801859e02e36224dc3a648a2fda_0": {
"author": "gotbadger",
"comment": "variable is safe",
"false_positive": true,
"ignored_at": "2023-10-13T14:57:07Z"
},
"68a86d90f28db878612eb5f699c06543_0": {
"author": "gotbadger",
"comment": "low risk",
"false_positive": false,
"ignored_at": "2023-11-13T15:00:28Z"
},
"6c3dd0e4ba8f4c427dffaea6c6696986_0": {
"author": "gotbadger",
"comment": "token gen",
"false_positive": true,
"ignored_at": "2023-11-15T11:51:54Z"
},
"dc369468d8a5d5bb522d023ae7485376_0": {
"author": "gotbadger",
"comment": "secret placeholder",
"false_positive": true,
"ignored_at": "2023-11-13T14:45:21Z"
},
"fff78d76f2c44a5768dcb4d55a3bd276_0": {
"author": "gotbadger",
"comment": "data hash",
"false_positive": false,
"ignored_at": "2023-11-13T14:48:31Z"
}
}
16 changes: 13 additions & 3 deletions internal/commands/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -107,9 +108,18 @@ $ bearer ignore show <fingerprint>`,

cmd.Print("\n")
if options.IgnoreShowOptions.All {
// show all fingerprints
for fingerprintId, fingerprint := range ignoredFingerprints {
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, fingerprint, options.GeneralOptions.NoColor))
// show all fingerprints sorted by date
keys := make([]string, 0, len(ignoredFingerprints))
for key := range ignoredFingerprints {
keys = append(keys, key)
}

sort.SliceStable(keys, func(i, j int) bool {
return ignoredFingerprints[keys[i]].IgnoredAt < ignoredFingerprints[keys[j]].IgnoredAt
})

for _, k := range keys {
cmd.Print(ignore.DisplayIgnoredEntryTextString(k, ignoredFingerprints[k], options.GeneralOptions.NoColor))
cmd.Print("\n\n")
}
} else {
Expand Down

0 comments on commit 086b4df

Please sign in to comment.