| 1 | package change |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // A check request asks a host's check worker to build the checks a commit |
| 6 | // requires, outside any change. It is a ref pointing straight at the |
| 7 | // commit: |
| 8 | // |
| 9 | // refs/the-valley/check-requests/<principal>/<worktree>/<attempt> |
| 10 | // |
| 11 | // <principal> is the principal that filed it. <worktree> and <attempt> are |
| 12 | // tokens the requesting client makes, each CheckRequestToken lowercase |
| 13 | // hexadecimal digits: a client names the worktree it asks from with one, |
| 14 | // and each request it files from there with the other, so a rerun of the |
| 15 | // same commit is a new ref. The push hook admits the ref |
| 16 | // (valleyhook/checkrequest.go), and the worker reads it (status/worker.go). |
| 17 | |
| 18 | // CheckRequestPrefix opens every check request's ref. |
| 19 | const CheckRequestPrefix = "refs/the-valley/check-requests/" |
| 20 | |
| 21 | // CheckRequestToken is how many lowercase hexadecimal digits a check |
| 22 | // request's worktree and attempt tokens each hold. |
| 23 | const CheckRequestToken = 16 |
| 24 | |
| 25 | // ParseCheckRequestRef splits a check request's ref into its principal, |
| 26 | // worktree and attempt. It reports false for a ref that is not |
| 27 | // refs/the-valley/check-requests/<principal>/<worktree>/<attempt> with a |
| 28 | // principal's name as the principal, and CheckRequestToken lowercase |
| 29 | // hexadecimal digits as each token. |
| 30 | func ParseCheckRequestRef(ref string) (principal, worktree, attempt string, ok bool) { |
| 31 | rest, found := strings.CutPrefix(ref, CheckRequestPrefix) |
| 32 | if !found { |
| 33 | return "", "", "", false |
| 34 | } |
| 35 | parts := strings.Split(rest, "/") |
| 36 | if len(parts) != 3 || !ValidPrincipal(parts[0]) || !requestToken(parts[1]) || !requestToken(parts[2]) { |
| 37 | return "", "", "", false |
| 38 | } |
| 39 | return parts[0], parts[1], parts[2], true |
| 40 | } |
| 41 | |
| 42 | // requestToken reports whether s is a check request's worktree or attempt |
| 43 | // token. |
| 44 | func requestToken(s string) bool { |
| 45 | if len(s) != CheckRequestToken { |
| 46 | return false |
| 47 | } |
| 48 | for _, c := range []byte(s) { |
| 49 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 50 | return false |
| 51 | } |
| 52 | } |
| 53 | return true |
| 54 | } |