gunk-dev/the-valley

checkrequest.go

sourced9c8073fd613main's published tip history
3.2 KiB
1package main
2
3import (
4 "bytes"
5 "fmt"
6 "strings"
7
8 "the-valley/change"
9)
10
11// maxCheckRequests is how many check requests one principal may hold in a
12// repository. The bound is soft, as the bound on change records is
13// (record.go): pushes running at once each count the requests there before
14// either installs a ref, so together they can pass it by the requests they
15// create. Once a principal is over it, its every later create is refused.
16const maxCheckRequests = 32
17
18// checkRequests counts the check requests principal holds now, past the
19// bound by one at most: for-each-ref stops printing there.
20func (gitRepository) checkRequests(principal string) (int, error) {
21 out, err := run(nil, "for-each-ref", fmt.Sprintf("--count=%d", maxCheckRequests+1), "--format=x", change.CheckRequestPrefix+principal+"/")
22 if err != nil {
23 return 0, err
24 }
25 return bytes.Count(out, []byte("\n")), nil
26}
27
28// requestCreates counts the check requests the push creates under
29// principal's own name, the only ones it may create.
30func requestCreates(principal string, updates []update) int {
31 n := 0
32 for _, u := range updates {
33 if principal != "" && strings.HasPrefix(u.ref, change.CheckRequestPrefix+principal+"/") && isZero(u.old) && !isZero(u.new) {
34 n++
35 }
36 }
37 return n
38}
39
40// checkCheckRequest decides a write to a check request's ref, and returns
41// the verb and the reason that refuse it, or "" for the reason when the
42// write is allowed. A named principal creates, replaces and deletes the
43// requests under its own name, and a holder of the approve grant deletes
44// any. A request points at a commit, which the worker builds the required
45// checks of; nothing else reads it, so it decides nothing that lands.
46func (p policy) checkCheckRequest(principal string, u update) (verb, why string) {
47 owner, _, _, ok := change.ParseCheckRequestRef(u.ref)
48 if !ok {
49 return "write", fmt.Sprintf("a check request's ref is %s<principal>/<worktree>/<attempt>, with the worktree and the attempt as %d lowercase hexadecimal digits each", change.CheckRequestPrefix, change.CheckRequestToken)
50 }
51 if principal == "" {
52 return "write", "a check request takes writes only from a named principal, and a key that names no principal is none"
53 }
54 if isZero(u.new) {
55 if principal != owner && !p.grants.holds(principal, grantApprove) {
56 return "delete", fmt.Sprintf("a check request is deleted by the principal it is filed under or by a holder of the %s, and %s is neither", approveGrant, principal)
57 }
58 return "delete", ""
59 }
60 verb = "update"
61 if isZero(u.old) {
62 verb = "create"
63 }
64 if principal != owner {
65 return verb, fmt.Sprintf("a principal files check requests under its own name, %s%s/, and this one is under %s", change.CheckRequestPrefix, principal, owner)
66 }
67 if verb == "create" && p.existingRequests+p.requestCreates > maxCheckRequests {
68 return verb, fmt.Sprintf("%s holds %d check requests in %s, and this push would create %d more, past the %d one principal may hold", principal, p.existingRequests, p.project, p.requestCreates, maxCheckRequests)
69 }
70 kind, err := p.repo.kind(u.new)
71 switch {
72 case err != nil:
73 return verb, fmt.Sprintf("%s %v", u.new, err)
74 case kind != "commit":
75 return verb, fmt.Sprintf("%s is a %s, and a check request points at a commit", u.new, kind)
76 }
77 return verb, ""
78}