Skip to content

Commit 63512a0

Browse files
authored
refactor: use slices.SortFunc instead of sort.Slice (#39)
1 parent 80346a6 commit 63512a0

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

internal/patrol/patrol.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package patrol
33

44
import (
5+
"cmp"
56
"errors"
67
"fmt"
78
"os"
@@ -11,12 +12,12 @@ import (
1112
"sheriff/internal/publish"
1213
"sheriff/internal/scanner"
1314
"sheriff/internal/slack"
14-
"sort"
1515
"sync"
1616

1717
"github.com/elliotchance/pie/v2"
1818
"github.com/rs/zerolog/log"
1919
gogitlab "github.com/xanzy/go-gitlab"
20+
"golang.org/x/exp/slices"
2021
)
2122

2223
const tempScanDir = "tmp_scans"
@@ -145,8 +146,8 @@ func (s *sheriffService) scanAndGetReports(locations []config.ProjectLocation) (
145146
reports = append(reports, r)
146147
}
147148

148-
sort.Slice(reports, func(i int, j int) bool {
149-
return len(reports[i].Vulnerabilities) > len(reports[j].Vulnerabilities)
149+
slices.SortFunc(reports, func(a, b scanner.Report) int {
150+
return cmp.Compare(len(b.Vulnerabilities), len(a.Vulnerabilities))
150151
})
151152

152153
return

internal/publish/to_gitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// severityScoreOrder represents the order of SeverityScoreKind by their score in descending order
17-
// which is how we want to display it in the
17+
// which is how we want to display it in the Issue report
1818
var severityScoreOrder = getSeverityScoreOrder(scanner.SeverityScoreThresholds)
1919

2020
// now is a function that returns the current time

internal/publish/to_slack.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package publish
22

33
import (
4+
"cmp"
45
"errors"
56
"fmt"
67
"sheriff/internal/scanner"
78
"sheriff/internal/slack"
8-
"sort"
9+
"slices"
910
"strings"
1011
"sync"
1112
"time"
@@ -259,8 +260,8 @@ func getSeverityScoreOrder(thresholds map[scanner.SeverityScoreKind]float64) []s
259260
for kind := range thresholds {
260261
kinds = append(kinds, kind)
261262
}
262-
sort.Slice(kinds, func(i, j int) bool {
263-
return thresholds[kinds[i]] > thresholds[kinds[j]]
263+
slices.SortFunc(kinds, func(a, b scanner.SeverityScoreKind) int {
264+
return cmp.Compare(thresholds[b], thresholds[a])
264265
})
265266

266267
return kinds

0 commit comments

Comments
 (0)