Code scanning frequency per repository, within an instance range !606

merged merged by cmc on 2026-11-05 10:35 UTC · krz/gitbay:security-frequency into main

15 files changed, +289 −26

Layout: unified · split

.gitbay/wiki/Admin.org +10 −2
@@ -462,8 +462,16 @@ The instance's code scan, which a repository admin turns on with
462462 runner host that serves a scanned repository. Build it from
463463 =deploy/Containerfile.security= (Opengrep and its rules, both pinned
464464 by sha256); the file has the commands.
465- =schedule= ("0 4 * * *") — when each enabled repository's default
466 branch is scanned. =repo security run= queues one now.
465- =most_often= ("daily") and =least_often= ("monthly") — the range of
466 frequencies a repository may choose with =repo settings
467 security-frequency=, from =daily=, =weekly= (Sunday) and =monthly=
468 (the 1st). A repository whose choice falls outside a narrowed range
469 scans at the nearest frequency in it, from its next scan on.
470- =default= ("daily") — the frequency of a repository that has not
471 chosen one; within the range.
472- =hour= (4) — the hour, UTC, every scan is due. =repo security run=
473 queues one now.
474- =schedule= is gone; a config that sets it is refused at load.
467475- =steps= — shell steps that write SARIF into =$GITBAY_RESULTS=. The
468476 default runs Opengrep with the rules in the image at
469477 =/opt/opengrep-rules=.
.gitbay/wiki/Parity.org +1
@@ -251,6 +251,7 @@ there shows the app's rendering rather than the one the web page shows.
251251| code scan now | yes | no | yes |
252252| code scan dismiss, reopen | yes | yes | no |
253253| code scan ignores, rules | yes | yes | no |
254| code scan frequency | yes | yes | no |
254255| dependency checks on/off | yes | yes | yes |
255256| dependency status | yes | yes | yes |
256257| advisory dismiss, reopen | yes | yes | no |
.gitbay/wiki/Users.org +1
@@ -250,6 +250,7 @@ gitbay repo security dismiss you/project 3f9a0c1d2e --reason false-positive # o
250250gitbay repo security reopen you/project 3f9a0c1d2e
251251gitbay repo settings security-ignore you/project 'testdata/**' # paths set aside from the scan
252252gitbay repo settings security-exclude-rule you/project swift.lang.crypto.insecure-random
253gitbay repo settings security-frequency you/project weekly # daily, weekly or monthly, as the instance allows
253254gitbay repo settings git-daemon you/project on # expose over git://
254255gitbay repo topics add you/project cli forge # free-form tags, shown on the web
255256gitbay repo search forge # find repos by name/description/topic
cmd/gitbay/main.go +1
@@ -651,6 +651,7 @@ func repoCmd() *cobra.Command {
651651 pass("secret-scan-allow", passOpts{server: []string{"repo", "settings", "secret-scan-allow"}, needsRepo: true}),
652652 pass("security-ignore", passOpts{server: []string{"repo", "settings", "security-ignore"}, needsRepo: true}),
653653 pass("security-exclude-rule", passOpts{server: []string{"repo", "settings", "security-exclude-rule"}, needsRepo: true}),
654 pass("security-frequency", passOpts{server: []string{"repo", "settings", "security-frequency"}, needsRepo: true}),
654655 pass("require-mr", passOpts{server: []string{"repo", "settings", "require-mr"}, needsRepo: true}),
655656 pass("description", passOpts{server: []string{"repo", "settings", "description"}, needsRepo: true}),
656657 pass("website", passOpts{server: []string{"repo", "settings", "website"}, needsRepo: true}),
cmd/gitbay/summaries_gen.go +1
@@ -220,6 +220,7 @@ var summaries = map[string]string{
220220 "repo settings secret-scan": "scan pushes for credentials: off, warn the pusher, or block the push",
221221 "repo settings secret-scan-allow": "paths whose secret scan findings are ignored",
222222 "repo settings security-exclude-rule": "code scanning rules, or families of them, whose findings are set aside",
223 "repo settings security-frequency": "how often the code scan runs, within what the instance allows",
223224 "repo settings security-ignore": "paths whose code scanning findings are set aside",
224225 "repo settings show": "show settings",
225226 "repo settings unprotect": "unprotect a branch",
internal/ci/sched.go +8 −1
@@ -31,6 +31,13 @@ func NextRun(expr string, after time.Time) string {
3131 return isoNow(n)
3232}
3333
34// SecurityNextRun is when a repository that chose freq is next scanned:
35// at the configured hour, daily, on Sunday, or on the 1st.
36func SecurityNextRun(sec config.Security, freq string, after time.Time) string {
37 field := map[string]string{"daily": "* * *", "weekly": "* * 0", "monthly": "1 * *"}[sec.Frequency(freq)]
38 return NextRun(fmt.Sprintf("0 %d %s", sec.Hour, field), after.UTC())
39}
40
3441// Scheduler fires scheduled builds. repoDir maps a repo to its bare path.
3542// Security is the instance's code scanning job; with no image it never
3643// queues.
@@ -139,7 +146,7 @@ func (s *Scheduler) runSecurityDue(now time.Time) {
139146 return
140147 }
141148 for _, repo := range due {
142 s.St.SetSecurityNext(repo.ID, NextRun(s.Security.Schedule, now))
149 s.St.SetSecurityNext(repo.ID, SecurityNextRun(s.Security, repo.Settings.SecurityFrequency, now))
143150 if _, err := QueueSecurityScan(s.St, s.Security, s.RepoDir(repo.OwnerName, repo.Name), repo); err != nil {
144151 slog.Warn("scheduler: queueing security scan", "repo", repo.Path(), "err", err)
145152 }
internal/ci/sched_test.go +23 −1
@@ -177,7 +177,7 @@ func TestSchedulerQueuesSecurityScan(t *testing.T) {
177177 t.Fatalf("queued with no image: %+v", builds)
178178 }
179179
180 s.Security = config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: []string{"true"}}
180 s.Security = config.Security{Image: "localhost/gitbay-security:1", Hour: 4, MostOften: "daily", LeastOften: "monthly", Default: "daily", Steps: []string{"true"}}
181181 st.SetSecurityNext(repoID, now.Add(-time.Minute).UTC().Format("2006-01-02T15:04:05Z"))
182182 s.RunDue(now)
183183 builds, _ := st.ListBuilds(repoID, store.BuildFilter{}, 10)
@@ -204,3 +204,25 @@ func TestSecurityJobNameIsReserved(t *testing.T) {
204204 t.Fatalf("Parse = %v", err)
205205 }
206206}
207
208func TestSecurityNextRun(t *testing.T) {
209 sec := config.Default().Security
210 sec.Hour = 6
211 // Wednesday 2026-11-04 10:00 UTC.
212 after := time.Date(2026, 11, 4, 10, 0, 0, 0, time.UTC).In(time.FixedZone("UTC-6", -6*3600))
213 for chosen, want := range map[string]string{
214 "": "2026-11-05T06:00:00Z",
215 "daily": "2026-11-05T06:00:00Z",
216 "weekly": "2026-11-08T06:00:00Z",
217 "monthly": "2026-12-01T06:00:00Z",
218 } {
219 if got := SecurityNextRun(sec, chosen, after); got != want {
220 t.Errorf("%q: %s, want %s", chosen, got, want)
221 }
222 }
223 // A choice the instance no longer allows scans at the nearest allowed.
224 sec.LeastOften, sec.Default = "weekly", "weekly"
225 if got := SecurityNextRun(sec, "monthly", after); got != "2026-11-08T06:00:00Z" {
226 t.Errorf("monthly outside the range: %s", got)
227 }
228}
internal/config/config.go +58 −8
@@ -12,6 +12,7 @@ import (
1212 "net"
1313 "os"
1414 "path/filepath"
15 "slices"
1516 "strconv"
1617 "strings"
1718 "time"
@@ -231,11 +232,46 @@ type Deps struct {
231232// enable` schedules. It is unavailable until an operator names Image, an
232233// image built from deploy/Containerfile.security and provisioned on the
233234// runner hosts. Steps write SARIF into $GITBAY_RESULTS; the runner uploads
234// it when they succeed.
235// it when they succeed. Repositories scan at a frequency between MostOften
236// and LeastOften, Default unless they choose one, at Hour UTC.
235237type Security struct {
236 Image string `toml:"image"`
237 Schedule string `toml:"schedule"`
238 Steps []string `toml:"steps"`
238 Image string `toml:"image"`
239 Hour int `toml:"hour"`
240 MostOften string `toml:"most_often"`
241 LeastOften string `toml:"least_often"`
242 Default string `toml:"default"`
243 Steps []string `toml:"steps"`
244}
245
246// ScanFrequencies are the code scan frequencies, most often first. Weekly
247// scans run on Sunday, monthly ones on the 1st.
248var ScanFrequencies = []string{"daily", "weekly", "monthly"}
249
250// Frequencies returns the frequencies a repository may choose, most often
251// first.
252func (s Security) Frequencies() []string {
253 lo, hi := s.bounds()
254 return ScanFrequencies[lo : hi+1]
255}
256
257// Frequency is the frequency a repository scans at: its choice, or the
258// default, moved to the nearest one the instance allows.
259func (s Security) Frequency(chosen string) string {
260 if chosen == "" {
261 chosen = s.Default
262 }
263 lo, hi := s.bounds()
264 return ScanFrequencies[max(lo, min(slices.Index(ScanFrequencies, chosen), hi))]
265}
266
267// bounds are the indexes of MostOften and LeastOften in ScanFrequencies,
268// or the whole list when they are unset or out of order.
269func (s Security) bounds() (int, int) {
270 lo, hi := slices.Index(ScanFrequencies, s.MostOften), slices.Index(ScanFrequencies, s.LeastOften)
271 if lo < 0 || hi < lo {
272 return 0, len(ScanFrequencies) - 1
273 }
274 return lo, hi
239275}
240276
241277// DefaultSecuritySteps run Opengrep with the rules the image carries.
@@ -591,7 +627,8 @@ func Default() Config {
591627 GitDaemon: GitDaemon{Port: 9418},
592628 Mirrors: Mirrors{PullIntervalMinutes: 15},
593629 Deps: Deps{CheckIntervalHours: 24},
594 Security: Security{Schedule: "0 4 * * *", Steps: DefaultSecuritySteps},
630 Security: Security{Hour: 4, MostOften: "daily", LeastOften: "monthly", Default: "daily",
631 Steps: DefaultSecuritySteps},
595632 Limits: Limits{
596633 MaxPackBytes: 2 << 30, // 2 GiB
597634 MaxBlobBytes: 100 << 20,
@@ -612,6 +649,9 @@ func Load(path string) (Config, error) {
612649 if err != nil {
613650 return cfg, err
614651 }
652 if md.IsDefined("security", "schedule") {
653 return cfg, errors.New("security.schedule is replaced by security.hour, most_often, least_often and default")
654 }
615655 if u := md.Undecoded(); len(u) > 0 {
616656 return cfg, fmt.Errorf("unknown config key %q", u[0].String())
617657 }
@@ -675,10 +715,20 @@ func (c Config) Validate() error {
675715 if c.Server.Root == "" {
676716 errs = append(errs, errors.New("server.root is required"))
677717 }
718 sec := c.Security
719 if sec.Hour < 0 || sec.Hour > 23 {
720 errs = append(errs, fmt.Errorf("security.hour must be 0 to 23, got %d", sec.Hour))
721 }
722 if err := oneOf("security.most_often", sec.MostOften, ScanFrequencies...); err != nil {
723 errs = append(errs, err)
724 } else if err := oneOf("security.least_often", sec.LeastOften, ScanFrequencies...); err != nil {
725 errs = append(errs, err)
726 } else if slices.Index(ScanFrequencies, sec.MostOften) > slices.Index(ScanFrequencies, sec.LeastOften) {
727 errs = append(errs, fmt.Errorf("security.most_often %q is less often than security.least_often %q", sec.MostOften, sec.LeastOften))
728 } else if !slices.Contains(sec.Frequencies(), sec.Default) {
729 errs = append(errs, fmt.Errorf("security.default must be one of %v, got %q", sec.Frequencies(), sec.Default))
730 }
678731 if c.Security.Image != "" {
679 if len(strings.Fields(c.Security.Schedule)) != 5 {
680 errs = append(errs, errors.New("security.schedule must be a five-field cron expression"))
681 }
682732 if len(c.Security.Steps) == 0 {
683733 errs = append(errs, errors.New("security.steps must name at least one step when security.image is set"))
684734 }
internal/config/config_test.go +35
@@ -519,3 +519,38 @@ func TestMailInboundPassword(t *testing.T) {
519519 t.Fatal("two lines accepted")
520520 }
521521}
522
523func TestSecurityFrequencies(t *testing.T) {
524 cfg, err := Load(writeConfig(t, minimal))
525 if err != nil {
526 t.Fatal(err)
527 }
528 if s := cfg.Security; s.Hour != 4 || s.Default != "daily" || strings.Join(s.Frequencies(), ",") != "daily,weekly,monthly" {
529 t.Fatalf("defaults: %+v %v", s, s.Frequencies())
530 }
531 cfg, err = Load(writeConfig(t, minimal+"\n[security]\nhour = 0\nmost_often = \"weekly\"\ndefault = \"weekly\"\n"))
532 if err != nil {
533 t.Fatal(err)
534 }
535 s := cfg.Security
536 if s.Hour != 0 || strings.Join(s.Frequencies(), ",") != "weekly,monthly" {
537 t.Fatalf("narrowed: %+v %v", s, s.Frequencies())
538 }
539 for chosen, want := range map[string]string{"": "weekly", "daily": "weekly", "weekly": "weekly", "monthly": "monthly"} {
540 if got := s.Frequency(chosen); got != want {
541 t.Errorf("Frequency(%q) = %q, want %q", chosen, got, want)
542 }
543 }
544 for body, want := range map[string]string{
545 "schedule = \"0 4 * * *\"\n": "security.schedule",
546 "hour = 24\n": "security.hour",
547 "most_often = \"hourly\"\n": "security.most_often",
548 "least_often = \"yearly\"\n": "security.least_often",
549 "most_often = \"monthly\"\nleast_often = \"weekly\"\n": "security.most_often",
550 "least_often = \"weekly\"\ndefault = \"monthly\"\n": "security.default",
551 } {
552 if _, err := Load(writeConfig(t, minimal+"\n[security]\n"+body)); err == nil || !strings.Contains(err.Error(), want) {
553 t.Errorf("want %q, got %v\n%s", want, err, body)
554 }
555 }
556}
internal/control/security.go +45 −2
@@ -15,6 +15,7 @@ import (
1515 "gitbay.org/gitbay/internal/botissue"
1616 "gitbay.org/gitbay/internal/ci"
1717 "gitbay.org/gitbay/internal/codescan"
18 "gitbay.org/gitbay/internal/config"
1819 "gitbay.org/gitbay/internal/deps"
1920 "gitbay.org/gitbay/internal/gitutil"
2021 "gitbay.org/gitbay/internal/policy"
@@ -70,6 +71,11 @@ func init() {
7071 Usage: "repo settings security-exclude-rule <owner/name> [<rule>...] (none clears the list)",
7172 Examples: []string{"repo settings security-exclude-rule krz/gitbay swift.lang.crypto.insecure-random"},
7273 Run: runSecurityExcludeRule})
74 register(Command{Path: []string{"repo", "settings", "security-frequency"},
75 Summary: "how often the code scan runs, within what the instance allows",
76 Usage: "repo settings security-frequency <owner/name> daily|weekly|monthly",
77 Examples: []string{"repo settings security-frequency krz/gitbay weekly"},
78 Run: runSecurityFrequency})
7379 register(Command{Path: []string{"runner", "results"},
7480 Summary: "upload a code scan's SARIF from stdin",
7581 Usage: "runner results <build-id>",
@@ -81,6 +87,7 @@ func init() {
8187// findings, the ones dismissed, and how many it set aside.
8288type SecurityOut struct {
8389 Enabled bool `json:"enabled"`
90 Frequency string `json:"frequency,omitempty"`
8491 NextRun string `json:"next_run,omitempty"`
8592 LastResults string `json:"last_results,omitempty"`
8693 IssueNumber int64 `json:"issue_number,omitempty"`
@@ -116,7 +123,7 @@ func runSecurityEnable(c *Ctx, args []string) int {
116123 if c.Cfg.Security.Image == "" {
117124 return c.fail(protocol.ExitFailure, "this instance has no code scanning image; an operator sets [security] image")
118125 }
119 next := ci.NextRun(c.Cfg.Security.Schedule, time.Now())
126 next := ci.SecurityNextRun(c.Cfg.Security, repo.Settings.SecurityFrequency, time.Now())
120127 if err := c.Store.EnableSecurityScan(repo.ID, next); err != nil {
121128 return c.fail(protocol.ExitFailure, "%v", err)
122129 }
@@ -196,7 +203,8 @@ func runSecurityStatus(c *Ctx, args []string) int {
196203 if err != nil {
197204 return c.fail(protocol.ExitFailure, "%v", err)
198205 }
199 out := SecurityOut{Enabled: true, NextRun: scan.NextRun, LastResults: scan.LastResults,
206 freq := c.Cfg.Security.Frequency(repo.Settings.SecurityFrequency)
207 out := SecurityOut{Enabled: true, Frequency: freq, NextRun: scan.NextRun, LastResults: scan.LastResults,
200208 IssueNumber: scan.IssueNumber, Findings: []SecurityFinding{}, Dismissed: []SecurityFinding{}}
201209 json.Unmarshal([]byte(scan.Aside), &out.Aside)
202210 for _, f := range codescan.ByPlace(reported) {
@@ -217,6 +225,7 @@ func runSecurityStatus(c *Ctx, args []string) int {
217225 v := c.view(w)
218226 v.fields(
219227 "scanning", "on",
228 "frequency", freq,
220229 "next scan", scan.NextRun,
221230 "last results", c.when(scan.LastResults),
222231 "tracked in", tracked,
@@ -241,6 +250,7 @@ func runSecurityStatus(c *Ctx, args []string) int {
241250 }, func() screen {
242251 s := screen{fields: []field{
243252 {"Scanning", []cell{cGlyph("ok"), cText("on")}},
253 {"Frequency", []cell{cText(freq)}},
244254 {"Next scan", []cell{cText(scan.NextRun)}},
245255 {"Last results", []cell{cAge(scan.LastResults)}},
246256 }}
@@ -568,6 +578,39 @@ func resolveFingerprint(findings []store.SecurityFinding, given string) (string,
568578 return match, ""
569579}
570580
581// runSecurityFrequency sets how often the repository is scanned and, when
582// scanning is on, moves the next scan to match.
583func runSecurityFrequency(c *Ctx, args []string) int {
584 if len(args) != 2 || !slices.Contains(config.ScanFrequencies, args[1]) {
585 return c.usage()
586 }
587 freq := args[1]
588 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
589 if code >= 0 {
590 return code
591 }
592 if allowed := c.Cfg.Security.Frequencies(); !slices.Contains(allowed, freq) {
593 return c.fail(protocol.ExitFailure, "this instance scans %s", strings.Join(allowed, ", "))
594 }
595 if _, err := c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) { s.SecurityFrequency = freq }); err != nil {
596 return c.fail(protocol.ExitFailure, "%v", err)
597 }
598 next := ""
599 if _, err := c.Store.SecurityScanFor(repo.ID); err == nil {
600 next = ci.SecurityNextRun(c.Cfg.Security, freq, time.Now())
601 if err := c.Store.SetSecurityNext(repo.ID, next); err != nil {
602 return c.fail(protocol.ExitFailure, "%v", err)
603 }
604 }
605 return c.emit(map[string]any{"frequency": freq, "next_run": next}, func(w io.Writer) {
606 if next == "" {
607 fmt.Fprintf(w, "%s scans %s once scanning is on\n", repo.Path(), freq)
608 return
609 }
610 fmt.Fprintf(w, "%s scans %s; next scan %s\n", repo.Path(), freq, next)
611 })
612}
613
571614// maxSecurityList bounds the ignore and exclude-rule settings.
572615const maxSecurityList = 50
573616
internal/control/security_test.go +52 −2
@@ -7,6 +7,7 @@ import (
77 "path/filepath"
88 "strings"
99 "testing"
10 "time"
1011
1112 "gitbay.org/gitbay/internal/ci"
1213 "gitbay.org/gitbay/internal/codescan"
@@ -24,7 +25,8 @@ const testSARIF = `{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"opengre
2425func TestSecurityScanRoundTrip(t *testing.T) {
2526 st, repo, u, root := browseFixture(t)
2627 p := repo.Path()
27 sec := config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: config.DefaultSecuritySteps}
28 sec := config.Default().Security
29 sec.Image = "localhost/gitbay-security:1"
2830
2931 c, out := runnerCtx(st, u.ID, root)
3032 if code := runSecurityEnable(c, []string{p}); code != protocol.ExitFailure || !strings.Contains(out.String(), "no code scanning image") {
@@ -202,7 +204,8 @@ const twoFindings = `{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"openg
202204func TestSecurityDismiss(t *testing.T) {
203205 st, repo, u, root := browseFixture(t)
204206 p := repo.Path()
205 sec := config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: config.DefaultSecuritySteps}
207 sec := config.Default().Security
208 sec.Image = "localhost/gitbay-security:1"
206209 run := func(f func(*Ctx, []string) int, stdin string, args ...string) (int, string) {
207210 t.Helper()
208211 c, out := runnerCtx(st, u.ID, root)
@@ -310,3 +313,50 @@ func TestDefaultBranchRefusesOptionShape(t *testing.T) {
310313 t.Errorf("a normal branch: exit %d: %s", code, out)
311314 }
312315}
316
317func TestSecurityFrequency(t *testing.T) {
318 st, repo, u, root := browseFixture(t)
319 p := repo.Path()
320 sec := config.Default().Security
321 sec.Image = "localhost/gitbay-security:1"
322 run := func(f func(*Ctx, []string) int, args ...string) (int, string) {
323 t.Helper()
324 c, out := runnerCtx(st, u.ID, root)
325 c.Cfg.Security = sec
326 c.JSON = true
327 return f(c, args), out.String()
328 }
329 // Before scanning is on the choice is kept and nothing is scheduled.
330 if code, out := run(runSecurityFrequency, p, "monthly"); code != protocol.ExitOK || !strings.Contains(out, `"next_run":""`) {
331 t.Fatalf("monthly while off: %d %s", code, out)
332 }
333 run(runSecurityEnable, p)
334 if sc, _ := st.SecurityScanFor(repo.ID); !strings.Contains(sc.NextRun, "-01T04:00:00Z") {
335 t.Fatalf("enable ignored the monthly choice: %q", sc.NextRun)
336 }
337 code, out := run(runSecurityFrequency, p, "weekly")
338 if code != protocol.ExitOK {
339 t.Fatalf("weekly: %d %s", code, out)
340 }
341 sc, _ := st.SecurityScanFor(repo.ID)
342 next, err := time.Parse("2006-01-02T15:04:05Z", sc.NextRun)
343 if err != nil || next.Weekday() != time.Sunday || next.Hour() != 4 || !strings.Contains(out, sc.NextRun) {
344 t.Fatalf("next run %q (%v), out %s", sc.NextRun, err, out)
345 }
346 if _, out := run(runSecurityStatus, p); !strings.Contains(out, `"frequency":"weekly"`) {
347 t.Fatalf("status: %s", out)
348 }
349 if code, _ := run(runSecurityFrequency, p, "hourly"); code != protocol.ExitUsage {
350 t.Fatalf("hourly: exit %d", code)
351 }
352 // Narrowed: daily is refused, and the stored monthly scans weekly.
353 sec.LeastOften, sec.Default, sec.MostOften = "weekly", "weekly", "weekly"
354 if code, out := run(runSecurityFrequency, p, "daily"); code != protocol.ExitFailure || !strings.Contains(out, "scans weekly") {
355 t.Fatalf("daily outside the range: %d %s", code, out)
356 }
357 sec.MostOften, sec.LeastOften = "daily", "daily"
358 sec.Default = "daily"
359 if _, out := run(runSecurityStatus, p); !strings.Contains(out, `"frequency":"daily"`) {
360 t.Fatalf("weekly outside the range: %s", out)
361 }
362}
internal/httpd/depspage_test.go +25
@@ -75,6 +75,31 @@ func TestSettingsPageRendersSecurityTriage(t *testing.T) {
7575 }
7676}
7777
78// The frequency dropdown offers what the instance allows, with the
79// repository's frequency selected.
80func TestSettingsPageRendersScanFrequency(t *testing.T) {
81 var sb strings.Builder
82 err := web.Render(&sb, "settings.html", settingsPage{
83 repoPage: testRepoPage(), ScanAvailable: true,
84 ScanFrequencies: []string{"weekly", "monthly"}, ScanFrequency: "weekly", ScanHour: 4,
85 })
86 if err != nil {
87 t.Fatal(err)
88 }
89 page := sb.String()
90 for _, want := range []string{
91 `value="security-frequency"`, `<option value="weekly" selected>weekly</option>`,
92 `<option value="monthly">monthly</option>`, "04:00 UTC",
93 } {
94 if !strings.Contains(page, want) {
95 t.Errorf("settings page does not show %q", want)
96 }
97 }
98 if strings.Contains(page, `value="daily"`) {
99 t.Error("offers a frequency the instance does not allow")
100 }
101}
102
78103// The advisories offer dismissal, the dismissed ones reopening, and the
79104// ones nothing imports are named.
80105func TestSettingsPageRendersAdvisoryTriage(t *testing.T) {
internal/httpd/settings.go +20 −10
@@ -26,14 +26,19 @@ type settingsPage struct {
2626 // ScanAvailable is whether the instance has a code scanning image;
2727 // without one the toggle would only ever be refused.
2828 ScanAvailable bool
29 Runners []store.RepoRunner
30 Access []accessRow
31 Hooks []hookRow
32 Deliveries []deliveryRow
33 Notice string
34 Saved bool
35 Reauth bool // Notice is the stale-session refusal: link to sign in
36 Submitted map[string]string
29 // ScanFrequencies are the frequencies the instance allows, ScanFrequency
30 // the one this repository scans at, and ScanHour the hour UTC.
31 ScanFrequencies []string
32 ScanFrequency string
33 ScanHour int
34 Runners []store.RepoRunner
35 Access []accessRow
36 Hooks []hookRow
37 Deliveries []deliveryRow
38 Notice string
39 Saved bool
40 Reauth bool // Notice is the stale-session refusal: link to sign in
41 Submitted map[string]string
3742}
3843
3944type accessRow struct {
@@ -113,8 +118,11 @@ func (s *Server) settingsFormWith(w http.ResponseWriter, r *http.Request, u stor
113118 repoPage: p, Topics: topics, Branches: branches,
114119 DepsEnabled: deps.Enabled, Deps: deps,
115120 Security: security, ScanAvailable: s.cfg.Security.Image != "",
116 Runners: runners,
117 Access: access, Hooks: hooks, Deliveries: deliveries,
121 ScanFrequencies: s.cfg.Security.Frequencies(),
122 ScanFrequency: s.cfg.Security.Frequency(repo.Settings.SecurityFrequency),
123 ScanHour: s.cfg.Security.Hour,
124 Runners: runners,
125 Access: access, Hooks: hooks, Deliveries: deliveries,
118126 Notice: notice,
119127 Saved: strings.HasPrefix(notice, "Saved "),
120128 Reauth: s.reauthNotice(w, notice, r.URL.Path),
@@ -169,6 +177,8 @@ func (s *Server) settingsSubmit(w http.ResponseWriter, r *http.Request, u store.
169177 verb = "enable"
170178 }
171179 argv = []string{"repo", "security", verb, repo}
180 case "security-frequency":
181 argv = []string{"repo", "settings", "security-frequency", repo, v("security-frequency")}
172182 case "deps-dismiss":
173183 argv = []string{"repo", "deps", "dismiss", repo, v("advisory"), "--reason", v("reason")}
174184 case "deps-reopen":
internal/store/repos.go +3
@@ -46,6 +46,9 @@ type RepoSettings struct {
4646 // or families, whose code scanning findings are set aside.
4747 SecurityIgnore []string `json:"security_ignore,omitempty"`
4848 SecurityExcludeRules []string `json:"security_exclude_rules,omitempty"`
49 // SecurityFrequency is the code scan frequency chosen, empty for the
50 // instance default.
51 SecurityFrequency string `json:"security_frequency,omitempty"`
4952}
5053
5154// Path returns the canonical owner/name form.
internal/web/templates/settings.html +6
@@ -180,6 +180,12 @@
180180 <div class="check"><input type="checkbox" id="security-enabled" name="security" value="on"{{if .Security.Enabled}} checked{{end}}></div>
181181 <div><button type="submit" class="btn">Save</button></div>
182182</form>
183{{if .ScanAvailable}}<form method="post" action="{{$base}}" class="setform">
184 <input type="hidden" name="field" value="security-frequency">
185 <div><label for="security-frequency">Frequency</label><p class="hint">Daily, weekly on Sunday, or monthly on the 1st, at {{printf "%02d:00" .ScanHour}} UTC.</p></div>
186 <div><select id="security-frequency" name="security-frequency">{{$freq := .ScanFrequency}}{{range .ScanFrequencies}}<option value="{{.}}"{{if eq . $freq}} selected{{end}}>{{.}}</option>{{end}}</select></div>
187 <div><button type="submit" class="btn">Save</button></div>
188</form>{{end}}
183189{{if .Security.Enabled}}
184190<p class="meta">Next scan {{.Security.NextRun}} · last results {{if .Security.LastResults}}{{when .Security.LastResults}}{{else}}never{{end}}{{if .Security.IssueNumber}} · <a href="/{{.Repo.OwnerName}}/{{.Repo.Name}}/issues/{{.Security.IssueNumber}}">#{{.Security.IssueNumber}}</a>{{end}}</p>
185191{{with .Security.Aside}}{{if or .Ignored .Unused}}<p class="meta">Set aside by the last scan:{{if .Ignored}} {{.Ignored}} under the paths or rules below, or marked vendored or generated in <code>.gitattributes</code>.{{end}}{{range $lib, $n := .Unused}} {{$n}} from {{$lib}} rules, which no manifest names.{{end}}</p>{{end}}{{end}}