| 1 | package approval |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "the-valley/change" |
| 7 | ) |
| 8 | |
| 9 | // RefPrefix opens every stored approval's ref. An approval is stored as |
| 10 | // its note, one blob, at RefPrefix<target>/<request name>, where the |
| 11 | // request name is the one the change's integration request is filed |
| 12 | // under (change.RequestName): refs/the-valley/approvals/main/c-<change ID>. |
| 13 | // A change has one approval ref per target, and a new approval replaces |
| 14 | // the one before it. |
| 15 | const RefPrefix = "refs/the-valley/approvals/" |
| 16 | |
| 17 | // Ref is the ref a change's approval for target is stored at. |
| 18 | func Ref(target, id string) string { |
| 19 | return RefPrefix + target + "/" + change.RequestName(id) |
| 20 | } |
| 21 | |
| 22 | // ParseRef splits an approval's ref into its target and change ID. It |
| 23 | // reports false for a ref that is not Ref of a branch name and a change |
| 24 | // ID. |
| 25 | func ParseRef(ref string) (target, id string, ok bool) { |
| 26 | rest, found := strings.CutPrefix(ref, RefPrefix) |
| 27 | if !found { |
| 28 | return "", "", false |
| 29 | } |
| 30 | slash := strings.LastIndexByte(rest, '/') |
| 31 | if slash < 0 { |
| 32 | return "", "", false |
| 33 | } |
| 34 | target, name := rest[:slash], rest[slash+1:] |
| 35 | id, ok = change.RequestID(name) |
| 36 | if !ok || !change.ValidBranch(target) { |
| 37 | return "", "", false |
| 38 | } |
| 39 | return target, id, true |
| 40 | } |