| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package checks |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | |
| 8 | checksdb "github.com/tenseleyFlow/shithub/internal/checks/sqlc" |
| 9 | ) |
| 10 | |
| 11 | // MarkStaleForPreviousHead flips suites on `prevHeadSHA` whose status |
| 12 | // is not yet completed to (completed, conclusion='stale'). Called from |
| 13 | // push:process when a head ref moves AND the matching protection rule |
| 14 | // has `dismiss_stale_status_checks_on_push = true`. |
| 15 | // |
| 16 | // "Status complete + conclusion stale" preserves the audit trail — |
| 17 | // the runs themselves stay readable, but the suite no longer counts |
| 18 | // as in-progress. |
| 19 | func MarkStaleForPreviousHead(ctx context.Context, deps Deps, repoID int64, prevHeadSHA string) (int, error) { |
| 20 | if prevHeadSHA == "" { |
| 21 | return 0, nil |
| 22 | } |
| 23 | q := checksdb.New() |
| 24 | ids, err := q.ListCheckSuiteIDsForHead(ctx, deps.Pool, checksdb.ListCheckSuiteIDsForHeadParams{ |
| 25 | RepoID: repoID, HeadSha: prevHeadSHA, |
| 26 | }) |
| 27 | if err != nil { |
| 28 | return 0, err |
| 29 | } |
| 30 | for _, id := range ids { |
| 31 | if err := q.MarkCheckSuiteStale(ctx, deps.Pool, id); err != nil { |
| 32 | return 0, err |
| 33 | } |
| 34 | } |
| 35 | return len(ids), nil |
| 36 | } |
| 37 |