gunk-dev/the-valley

houserules.go

source68d12192bbd3main's published tip history
3.3 KiB
1// Package houserules reads the operator's house rules: one file, kept in a
2// registered project's repository, that klaus adds to the context of every
3// worker, consult and review. The file is read from origin's default branch,
4// never from a working tree.
5package houserules
6
7import (
8 "context"
9 "crypto/sha256"
10 "encoding/hex"
11 "fmt"
12 "strings"
13
14 "the-valley/flow/internal/config"
15 "the-valley/flow/internal/git"
16 "the-valley/flow/internal/project"
17 "the-valley/flow/internal/run"
18)
19
20// Rules is the house rules as read at one moment. The zero value is no house
21// rules configured.
22type Rules struct {
23 Source string // "<repo>:<path>@<commit>", or "<repo>:<path>" when unread
24 Digest string // sha256 of Text; empty when Missing
25 Text string
26 Missing bool // configured, but the file could not be read
27 Reason string
28}
29
30// Load reads the file cfg names. It never fails: a file that cannot be read
31// gives Rules with Missing set and the reason, for the caller to warn once and
32// carry on.
33func Load(ctx context.Context, cfg *config.HouseRulesConfig, reg *project.Registry) Rules {
34 if cfg == nil || cfg.Repo == "" || cfg.Path == "" {
35 return Rules{}
36 }
37 r := Rules{Source: cfg.Repo + ":" + cfg.Path, Missing: true}
38 if reg == nil {
39 r.Reason = "no project registry"
40 return r
41 }
42 clone, ok := reg.Get(cfg.Repo)
43 if !ok {
44 r.Reason = fmt.Sprintf("project %q is not registered", cfg.Repo)
45 return r
46 }
47 branch := "main"
48 if c, err := config.Load(clone); err == nil && c.DefaultBranch != "" {
49 branch = c.DefaultBranch
50 }
51 if err := git.FetchRemoteBranch(ctx, clone, branch); err != nil {
52 r.Reason = fmt.Sprintf("fetching origin/%s: %s", branch, strings.TrimSpace(err.Error()))
53 return r
54 }
55 commit := git.RefCommit(ctx, clone, "refs/remotes/origin/"+branch)
56 if commit == "" {
57 r.Reason = fmt.Sprintf("origin/%s not found", branch)
58 return r
59 }
60 text, err := git.ShowFile(ctx, clone, commit, cfg.Path)
61 if err != nil {
62 r.Reason = fmt.Sprintf("origin/%s has no readable %s", branch, cfg.Path)
63 return r
64 }
65 sum := sha256.Sum256([]byte(text))
66 return Rules{Source: r.Source + "@" + commit[:min(12, len(commit))], Digest: hex.EncodeToString(sum[:]), Text: text}
67}
68
69// Warning is what to tell the operator when the rules could not be read, or
70// empty.
71func (r Rules) Warning() string {
72 if !r.Missing {
73 return ""
74 }
75 return fmt.Sprintf("house rules %s could not be read (%s); continuing without them", r.Source, r.Reason)
76}
77
78// Section is the rules as a delimited section for a prompt, or empty when
79// there are none. The text is the operator's own, so it is instruction, but
80// the section says where it came from.
81func (r Rules) Section() string {
82 if r.Text == "" {
83 return ""
84 }
85 return fmt.Sprintf("<house-rules source=%q>\n%s\n</house-rules>", r.Source, strings.TrimSpace(r.Text))
86}
87
88// Append adds the section after prompt.
89func (r Rules) Append(prompt string) string {
90 if s := r.Section(); s != "" {
91 return strings.TrimRight(prompt, "\n") + "\n\n## House rules\nThe operator's rules for all their projects follow. The project's own instructions add to them.\n\n" + s + "\n"
92 }
93 return prompt
94}
95
96// Record is the rules as run state keeps them, or nil when none are
97// configured.
98func (r Rules) Record() *run.HouseRules {
99 if r.Source == "" {
100 return nil
101 }
102 return &run.HouseRules{Source: r.Source, Digest: r.Digest, Missing: r.Missing}
103}