|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// settingsTTL is how long a GitHub answer that rarely changes (branch rules, allowed merge methods) is reused. |
| 12 | +const settingsTTL = 24 * time.Hour |
| 13 | + |
| 14 | +// githubSettingsPath is where land keeps those answers; empty when there is no user cache dir, which turns caching off. |
| 15 | +func githubSettingsPath() string { |
| 16 | + dir, err := os.UserCacheDir() |
| 17 | + if err != nil { |
| 18 | + return "" |
| 19 | + } |
| 20 | + return filepath.Join(dir, "rgit", "github-settings.json") |
| 21 | +} |
| 22 | + |
| 23 | +// settingsHintMarkers are refusal texts that say a repo's branch rules or merge methods changed. |
| 24 | +var settingsHintMarkers = []string{ |
| 25 | + "gh006", "gh013", "repository rule violations", "protected branch", "merge method", |
| 26 | + "squash merges are not allowed", "merge commits are not allowed", |
| 27 | +} |
| 28 | + |
| 29 | +func settingsHint(out string) bool { |
| 30 | + lower := strings.ToLower(out) |
| 31 | + for _, m := range settingsHintMarkers { |
| 32 | + if strings.Contains(lower, m) { |
| 33 | + return true |
| 34 | + } |
| 35 | + } |
| 36 | + return false |
| 37 | +} |
| 38 | + |
| 39 | +// dropSettingsOnHint forgets slug's cached settings when a refused push, PR or merge says they changed. |
| 40 | +func dropSettingsOnHint(gh gitHub, slug, out string) { |
| 41 | + c, ok := gh.(*ghSettingsCache) |
| 42 | + if !ok || slug == "" || !settingsHint(out) { |
| 43 | + return |
| 44 | + } |
| 45 | + c.forget(slug) |
| 46 | +} |
| 47 | + |
| 48 | +// ghSettingsCache is a gitHub decorator that keeps branch rules and merge-method answers on disk for |
| 49 | +// settingsTTL. Only successful answers are stored. An empty path turns the cache off. |
| 50 | +type ghSettingsCache struct { |
| 51 | + inner gitHub |
| 52 | + path string |
| 53 | + now func() time.Time |
| 54 | +} |
| 55 | + |
| 56 | +func newGHSettingsCache(inner gitHub, path string) *ghSettingsCache { |
| 57 | + return &ghSettingsCache{inner: inner, path: path, now: time.Now} |
| 58 | +} |
| 59 | + |
| 60 | +type ghSettingsFile struct { |
| 61 | + Repos map[string]*ghRepoSettings `json:"repos"` |
| 62 | +} |
| 63 | + |
| 64 | +type ghRepoSettings struct { |
| 65 | + Rules map[string]ghRulesEntry `json:"rules,omitempty"` |
| 66 | + SquashMerge *ghFlagEntry `json:"squashMerge,omitempty"` |
| 67 | + MergeCommit *ghFlagEntry `json:"mergeCommit,omitempty"` |
| 68 | +} |
| 69 | + |
| 70 | +type ghRulesEntry struct { |
| 71 | + Read time.Time `json:"read"` |
| 72 | + Types []string `json:"types"` |
| 73 | +} |
| 74 | + |
| 75 | +type ghFlagEntry struct { |
| 76 | + Read time.Time `json:"read"` |
| 77 | + Allowed bool `json:"allowed"` |
| 78 | +} |
| 79 | + |
| 80 | +// load reads the cache file; a missing or unreadable file is empty. |
| 81 | +func (c *ghSettingsCache) load() ghSettingsFile { |
| 82 | + var f ghSettingsFile |
| 83 | + if c.path == "" { |
| 84 | + return f |
| 85 | + } |
| 86 | + b, err := os.ReadFile(c.path) |
| 87 | + if err != nil || json.Unmarshal(b, &f) != nil { |
| 88 | + return ghSettingsFile{} |
| 89 | + } |
| 90 | + return f |
| 91 | +} |
| 92 | + |
| 93 | +// save writes f through a temp file and a rename, so a concurrent land never reads a partial file. |
| 94 | +// A failed write only costs a later re-read. |
| 95 | +func (c *ghSettingsCache) save(f ghSettingsFile) { |
| 96 | + if c.path == "" { |
| 97 | + return |
| 98 | + } |
| 99 | + b, err := json.Marshal(f) |
| 100 | + if err != nil { |
| 101 | + return |
| 102 | + } |
| 103 | + dir := filepath.Dir(c.path) |
| 104 | + if err := os.MkdirAll(dir, 0o750); err != nil { |
| 105 | + return |
| 106 | + } |
| 107 | + tmp, err := os.CreateTemp(dir, ".github-settings-*.json") |
| 108 | + if err != nil { |
| 109 | + return |
| 110 | + } |
| 111 | + _, werr := tmp.Write(b) |
| 112 | + cerr := tmp.Close() |
| 113 | + if werr != nil || cerr != nil || os.Rename(tmp.Name(), c.path) != nil { |
| 114 | + _ = os.Remove(tmp.Name()) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func (c *ghSettingsCache) fresh(read time.Time) bool { |
| 119 | + age := c.now().Sub(read) |
| 120 | + return age >= 0 && age < settingsTTL |
| 121 | +} |
| 122 | + |
| 123 | +// update applies mutate to slug's settings, reading the file fresh so other keys survive. |
| 124 | +func (c *ghSettingsCache) update(slug string, mutate func(*ghRepoSettings)) { |
| 125 | + f := c.load() |
| 126 | + if f.Repos == nil { |
| 127 | + f.Repos = map[string]*ghRepoSettings{} |
| 128 | + } |
| 129 | + rs := f.Repos[slug] |
| 130 | + if rs == nil { |
| 131 | + rs = &ghRepoSettings{} |
| 132 | + f.Repos[slug] = rs |
| 133 | + } |
| 134 | + mutate(rs) |
| 135 | + c.save(f) |
| 136 | +} |
| 137 | + |
| 138 | +// forget drops slug's cached answers so the next land reads them again. |
| 139 | +func (c *ghSettingsCache) forget(slug string) { |
| 140 | + f := c.load() |
| 141 | + if _, ok := f.Repos[slug]; !ok { |
| 142 | + return |
| 143 | + } |
| 144 | + delete(f.Repos, slug) |
| 145 | + c.save(f) |
| 146 | +} |
| 147 | + |
| 148 | +func (c *ghSettingsCache) branchRuleTypes(slug, branch string) ([]string, error) { |
| 149 | + if rs := c.load().Repos[slug]; rs != nil { |
| 150 | + if e, ok := rs.Rules[branch]; ok && c.fresh(e.Read) { |
| 151 | + return e.Types, nil |
| 152 | + } |
| 153 | + } |
| 154 | + types, err := c.inner.branchRuleTypes(slug, branch) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + c.update(slug, func(rs *ghRepoSettings) { |
| 159 | + if rs.Rules == nil { |
| 160 | + rs.Rules = map[string]ghRulesEntry{} |
| 161 | + } |
| 162 | + rs.Rules[branch] = ghRulesEntry{Read: c.now(), Types: types} |
| 163 | + }) |
| 164 | + return types, nil |
| 165 | +} |
| 166 | + |
| 167 | +func (c *ghSettingsCache) squashMergeAllowed(slug string) (bool, error) { |
| 168 | + if rs := c.load().Repos[slug]; rs != nil && rs.SquashMerge != nil && c.fresh(rs.SquashMerge.Read) { |
| 169 | + return rs.SquashMerge.Allowed, nil |
| 170 | + } |
| 171 | + ok, err := c.inner.squashMergeAllowed(slug) |
| 172 | + if err != nil { |
| 173 | + return false, err |
| 174 | + } |
| 175 | + c.update(slug, func(rs *ghRepoSettings) { |
| 176 | + rs.SquashMerge = &ghFlagEntry{Read: c.now(), Allowed: ok} |
| 177 | + }) |
| 178 | + return ok, nil |
| 179 | +} |
| 180 | + |
| 181 | +func (c *ghSettingsCache) mergeCommitAllowed(slug string) (bool, error) { |
| 182 | + if rs := c.load().Repos[slug]; rs != nil && rs.MergeCommit != nil && c.fresh(rs.MergeCommit.Read) { |
| 183 | + return rs.MergeCommit.Allowed, nil |
| 184 | + } |
| 185 | + ok, err := c.inner.mergeCommitAllowed(slug) |
| 186 | + if err != nil { |
| 187 | + return false, err |
| 188 | + } |
| 189 | + c.update(slug, func(rs *ghRepoSettings) { |
| 190 | + rs.MergeCommit = &ghFlagEntry{Read: c.now(), Allowed: ok} |
| 191 | + }) |
| 192 | + return ok, nil |
| 193 | +} |
| 194 | + |
| 195 | +// The rest is live state and passes straight through. |
| 196 | + |
| 197 | +func (c *ghSettingsCache) createPR(dir, head, body string) (string, error) { |
| 198 | + return c.inner.createPR(dir, head, body) |
| 199 | +} |
| 200 | + |
| 201 | +func (c *ghSettingsCache) mergePR(dir, head string, auto bool, strategy string) error { |
| 202 | + return c.inner.mergePR(dir, head, auto, strategy) |
| 203 | +} |
| 204 | + |
| 205 | +func (c *ghSettingsCache) prNumber(dir, head string) (int, error) { |
| 206 | + return c.inner.prNumber(dir, head) |
| 207 | +} |
| 208 | + |
| 209 | +func (c *ghSettingsCache) pullRequest(owner, repo string, number int) (prInfo, error) { |
| 210 | + return c.inner.pullRequest(owner, repo, number) |
| 211 | +} |
| 212 | + |
| 213 | +func (c *ghSettingsCache) latestWorkflowRun(owner, repo, branch, sha string) (workflowRun, error) { |
| 214 | + return c.inner.latestWorkflowRun(owner, repo, branch, sha) |
| 215 | +} |
0 commit comments