gunk-dev/the-valley

approval.go

source69d7baef1f04main's published tip history
2.7 KiB
1package main
2
3import (
4 "fmt"
5
6 "the-valley/approval"
7)
8
9// An approval is one signed note stored as a blob at
10// refs/the-valley/approvals/<target>/c-<change ID> (approval.Ref): an
11// approver's statement that one recorded head of the change may land. A
12// new approval replaces the one before it.
13//
14// The hook holds an approval to its form alone. Whether the key that
15// signed it is an approval key, and whether the approval lets anything
16// land, is for whatever reads it to decide against the keys it holds.
17
18// approvalBudget bounds the one object an approval ref points at.
19var approvalBudget = budget{subject: "an approval", fileSize: approval.MaxNote, bytes: approval.MaxNote}
20
21// approval reads the blob id names, held to approval.MaxNote bytes before
22// its content is asked for.
23func (gitRepository) approval(id string) ([]byte, error) {
24 objects, err := openObjectsWithin(approvalBudget)
25 if err != nil {
26 return nil, err
27 }
28 defer objects.close()
29 return objects.read(id, "blob", approval.MaxNote)
30}
31
32// checkApproval decides a write to an approval ref, and returns the verb
33// and the reason that refuse it, or "" for the reason when the write is
34// allowed. A principal holding the approve grant creates, replaces and
35// deletes one; nobody else writes one, whatever change they own. The ref's
36// target is main, as a change record's is, and what it points at is a blob
37// of at most approval.MaxNote bytes holding an approval note
38// (approval.ParseNote) about the change and the target its ref names.
39func (p policy) checkApproval(principal string, u update) (verb, why string) {
40 target, id, ok := approval.ParseRef(u.ref)
41 if !ok || target != recordTarget {
42 return "write", fmt.Sprintf("an approval's ref is %s%s/c-<change ID>, with the change ID as 32 letters from k to z", approval.RefPrefix, recordTarget)
43 }
44 verb = "update"
45 switch {
46 case isZero(u.new):
47 verb = "delete"
48 case isZero(u.old):
49 verb = "create"
50 }
51 if principal == "" || !p.grants.holds(principal, grantApprove) {
52 holder := principal + " holds none"
53 if principal == "" {
54 holder = "a key that names no principal holds no grant"
55 }
56 return verb, fmt.Sprintf("%s takes writes only from a principal holding the %s, and %s", approval.RefPrefix, approveGrant, holder)
57 }
58 if verb == "delete" {
59 return verb, ""
60 }
61 body, err := p.repo.approval(u.new)
62 if err != nil {
63 return verb, "it " + err.Error()
64 }
65 s, err := approval.ParseNote(body)
66 switch {
67 case err != nil:
68 return verb, "it is not an approval note: " + err.Error()
69 case s.Change != id:
70 return verb, fmt.Sprintf("it approves the change %s, and its ref names %s", s.Change, id)
71 case s.Target != target:
72 return verb, fmt.Sprintf("it approves landing on %s, and its ref names %s", s.Target, target)
73 }
74 return verb, ""
75}