internal/leaks/report.go

545ba0bcdea3b534903bd78c918c0c025a97eda7
gitbay/internal/leaks/report.go

76 lines · 1725 bytes

8 symbols in this file
 1package leaks
 2
 3import (
 4	"fmt"
 5	"strings"
 6
 7	"gitbay.org/gitbay/internal/ci"
 8)
 9
10// Modes a repository's secret scan runs in. The empty setting is
11// ModeWarn: scanning sends nothing anywhere and warning blocks nothing, so
12// it is on unless an admin turns it off.
13const (
14	ModeOff   = "off"
15	ModeWarn  = "warn"
16	ModeBlock = "block"
17)
18
19// Mode resolves a repository's setting.
20func Mode(setting string) string {
21	if setting == "" {
22		return ModeWarn
23	}
24	return setting
25}
26
27// Allowed drops findings under a path the repository allows, given as CI
28// path globs (`testdata/**`, `*.pem`).
29func Allowed(findings []Finding, allow []string) []Finding {
30	if len(allow) == 0 {
31		return findings
32	}
33	var out []Finding
34	for _, f := range findings {
35		skip := false
36		for _, g := range allow {
37			if ci.Match(g, f.Path) {
38				skip = true
39				break
40			}
41		}
42		if !skip {
43			out = append(out, f)
44		}
45	}
46	return out
47}
48
49// listed is how many findings a message names; the count covers the rest.
50const listed = 20
51
52// Describe lists findings for a person: one line each, commit, path, line
53// and rule. heading leads it.
54func Describe(heading string, findings []Finding, truncated bool) string {
55	var b strings.Builder
56	b.WriteString(heading)
57	for i, f := range findings {
58		if i == listed {
59			fmt.Fprintf(&b, "\n  and %d more", len(findings)-listed)
60			break
61		}
62		fmt.Fprintf(&b, "\n  %.10s %s:%d %s", f.Commit, f.Path, f.Line, f.Rule)
63	}
64	if truncated {
65		b.WriteString("\n  (scan stopped at its size limit; not every file was read)")
66	}
67	return b.String()
68}
69
70// Plural is "1 possible secret" or "n possible secrets".
71func Plural(n int) string {
72	if n == 1 {
73		return "1 possible secret"
74	}
75	return fmt.Sprintf("%d possible secrets", n)
76}