| 1 | package knot |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // check holds one knot to every rule about it alone, in the order a |
| 10 | // reader would want to hear them: what it is, then each field. |
| 11 | func (k *Knot) check(path string) *Error { |
| 12 | t, ok := TypeNamed(k.Type) |
| 13 | switch { |
| 14 | case k.Type == "": |
| 15 | return refuse(RuleRequired, path, "a knot has a type, one of "+strings.Join(TypeNames(), ", ")) |
| 16 | case !ok: |
| 17 | return refuse(RuleType, path, fmt.Sprintf("its type is %q, and a knot's type is one of %s", k.Type, strings.Join(TypeNames(), ", "))) |
| 18 | } |
| 19 | switch { |
| 20 | case k.ID == "": |
| 21 | return refuse(RuleRequired, path, "a knot has an id") |
| 22 | case !IsLocalID(k.ID) || !strings.HasPrefix(k.ID, t.Prefix+"-"): |
| 23 | return refuse(RuleIDShape, path, fmt.Sprintf("its id is %q, and a%s %s's id is %s- and 7 hex digits", k.ID, article(t.Name), t.Name, t.Prefix)) |
| 24 | case k.Status == "": |
| 25 | return refuse(RuleRequired, path, fmt.Sprintf("a knot has a status; a%s %s's is one of %s", article(t.Name), t.Name, strings.Join(t.Statuses(), ", "))) |
| 26 | case !slices.Contains(t.Statuses(), k.Status): |
| 27 | return refuse(RuleStatus, path, fmt.Sprintf("its status is %q, and a%s %s's status is one of %s", k.Status, article(t.Name), t.Name, strings.Join(t.Statuses(), ", "))) |
| 28 | case k.Title == "": |
| 29 | return refuse(RuleRequired, path, "a knot has a title") |
| 30 | case k.Created == "": |
| 31 | return refuse(RuleRequired, path, "a knot has a created date") |
| 32 | case !date.MatchString(k.Created): |
| 33 | return refuse(RuleFieldShape, path, fmt.Sprintf("created is %q, and a date is YYYY-MM-DD", k.Created)) |
| 34 | } |
| 35 | if err := k.checkFieldsOnType(path, t); err != nil { |
| 36 | return err |
| 37 | } |
| 38 | if err := k.checkShapes(path); err != nil { |
| 39 | return err |
| 40 | } |
| 41 | switch { |
| 42 | case t.Name == "outcome" && k.BlockedBy == nil: |
| 43 | return refuse(RuleRequired, path, "an outcome has a blocked_by list, [] when it waits on nothing") |
| 44 | case t.Name == "idea" && k.Status == "graduated" && k.GraduatedInto == "": |
| 45 | return refuse(RuleGraduated, path, "a graduated idea names the outcome or question it graduated into, in graduated_into") |
| 46 | case k.GraduatedInto != "" && k.Status != "graduated": |
| 47 | return refuse(RuleGraduated, path, fmt.Sprintf("it names graduated_into, and its status is %s: only a graduated idea names one", k.Status)) |
| 48 | } |
| 49 | return k.checkLive(path) |
| 50 | } |
| 51 | |
| 52 | // checkFieldsOnType refuses a field the knot's type does not carry. |
| 53 | func (k *Knot) checkFieldsOnType(path string, t Type) *Error { |
| 54 | for _, f := range Fields { |
| 55 | if !f.carries(t.Name) && k.holds(f.Name) { |
| 56 | return refuse(RuleFieldOnType, path, fmt.Sprintf("a%s %s does not carry %s; it carries %s", article(t.Name), t.Name, f.Name, strings.Join(FieldNames(t.Name), ", "))) |
| 57 | } |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // holds reports whether the knot holds field, empty or not for a list. |
| 63 | func (k *Knot) holds(field string) bool { |
| 64 | switch field { |
| 65 | case "blocked_by": |
| 66 | return k.BlockedBy != nil |
| 67 | case "changes": |
| 68 | return k.Changes != nil |
| 69 | } |
| 70 | return k.Get(field) != "" |
| 71 | } |
| 72 | |
| 73 | // checkShapes holds each field that is there to its shape. |
| 74 | func (k *Knot) checkShapes(path string) *Error { |
| 75 | shape := func(field, value, what string) *Error { |
| 76 | return refuse(RuleFieldShape, path, fmt.Sprintf("%s is %q, and it is %s", field, value, what)) |
| 77 | } |
| 78 | const aName = "a name of letters, digits, '.', '_' and '-', opening with a letter or digit" |
| 79 | switch { |
| 80 | case k.CreatedBy != "" && !name.MatchString(k.CreatedBy): |
| 81 | return shape("created_by", k.CreatedBy, "a principal's name: "+aName) |
| 82 | case k.Owner != "" && !name.MatchString(k.Owner): |
| 83 | return shape("owner", k.Owner, "a principal's name: "+aName) |
| 84 | case k.Parent != "" && !IsLocalID(k.Parent): |
| 85 | return shape("parent", k.Parent, "an outcome's id in this graph, oc- and 7 hex digits") |
| 86 | case k.Parent != "" && !strings.HasPrefix(k.Parent, "oc-"): |
| 87 | return refuse(RuleParentOutcome, path, fmt.Sprintf("parent is %s, and a parent is an outcome, oc- and 7 hex digits", k.Parent)) |
| 88 | case k.ClaimedBy != "" && !name.MatchString(k.ClaimedBy): |
| 89 | return shape("claimed_by", k.ClaimedBy, "a run's ID: "+aName) |
| 90 | case k.ClaimedAt != "" && !IsTimestamp(k.ClaimedAt): |
| 91 | return shape("claimed_at", k.ClaimedAt, "a UTC time, YYYY-MM-DDTHH:MM:SSZ") |
| 92 | case k.Target != "" && !name.MatchString(k.Target) && !qualifiedID.MatchString(k.Target): |
| 93 | return shape("target", k.Target, "a project's name, or <project>:<id>") |
| 94 | case k.GraduatedInto != "" && (!IsLocalID(k.GraduatedInto) || !strings.HasPrefix(k.GraduatedInto, "oc-") && !strings.HasPrefix(k.GraduatedInto, "qn-")): |
| 95 | return shape("graduated_into", k.GraduatedInto, "an outcome's or a question's id in this graph, oc- or qn- and 7 hex digits") |
| 96 | } |
| 97 | for _, ref := range k.BlockedBy { |
| 98 | if !IsLocalID(ref) && !qualifiedID.MatchString(ref) { |
| 99 | return shape("an entry of blocked_by", ref, "a knot's id, or <project>:<id> for another project's") |
| 100 | } |
| 101 | } |
| 102 | for _, link := range k.Changes { |
| 103 | if !valleyChange.MatchString(link) && !githubPR.MatchString(link) { |
| 104 | return shape("an entry of changes", link, "valley:<project>/<change ID>, or https://github.com/<owner>/<repo>/pull/<n>") |
| 105 | } |
| 106 | } |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | // checkLive holds a knot to the rules the live graph adds to the |
| 111 | // archive's: who created it, its limits, its claim, and the cutoff. |
| 112 | func (k *Knot) checkLive(path string) *Error { |
| 113 | switch { |
| 114 | case k.CreatedBy == "": |
| 115 | return refuse(RuleProvenance, path, "a knot names the principal that created it, in created_by") |
| 116 | case k.Created <= IDRuleCutoff: |
| 117 | return refuse(RuleCutoff, path, fmt.Sprintf("created is %s, and a knot is created after %s", k.Created, IDRuleCutoff)) |
| 118 | case (k.ClaimedBy == "") != (k.ClaimedAt == ""): |
| 119 | return refuse(RuleClaim, path, "claimed_by and claimed_at come together: a claim names its run and when it was made") |
| 120 | case strings.ContainsAny(k.Title, "\n\t") || checkText(path, []byte(k.Title)) != nil: |
| 121 | return refuse(RuleText, path, "its title is one line, with no tab or other control character, written or escaped") |
| 122 | case strings.ContainsAny(k.Source, "\n\t") || checkText(path, []byte(k.Source)) != nil: |
| 123 | return refuse(RuleText, path, "its source is one line, with no tab or other control character, written or escaped") |
| 124 | case len(k.Title) > MaxTitle: |
| 125 | return refuse(RuleLimit, path, fmt.Sprintf("its title is %d bytes, more than the %d a title may be", len(k.Title), MaxTitle)) |
| 126 | case len(k.Source) > MaxTitle: |
| 127 | return refuse(RuleLimit, path, fmt.Sprintf("its source is %d bytes, more than the %d a source may be", len(k.Source), MaxTitle)) |
| 128 | case len(k.BlockedBy) > MaxListItems || len(k.Changes) > MaxListItems: |
| 129 | return refuse(RuleLimit, path, fmt.Sprintf("a list holds more than the %d entries one may", MaxListItems)) |
| 130 | } |
| 131 | for field, value := range map[string]string{"created_by": k.CreatedBy, "owner": k.Owner, "claimed_by": k.ClaimedBy, "target": k.Target} { |
| 132 | if len(value) > MaxName+12 || (field != "target" && len(value) > MaxName) { |
| 133 | return refuse(RuleLimit, path, fmt.Sprintf("%s is %d bytes, more than the %d a name may be", field, len(value), MaxName)) |
| 134 | } |
| 135 | } |
| 136 | for _, ref := range k.BlockedBy { |
| 137 | if len(ref) > MaxName+12 { |
| 138 | return refuse(RuleLimit, path, fmt.Sprintf("blocked_by names %q, longer than a reference may be", ref)) |
| 139 | } |
| 140 | } |
| 141 | for _, link := range k.Changes { |
| 142 | if len(link) > MaxLink { |
| 143 | return refuse(RuleLimit, path, fmt.Sprintf("changes holds a link of %d bytes, more than the %d one may be", len(link), MaxLink)) |
| 144 | } |
| 145 | } |
| 146 | return nil |
| 147 | } |
| 148 | |
| 149 | func article(word string) string { |
| 150 | if strings.ContainsRune("aeiou", rune(word[0])) { |
| 151 | return "n" |
| 152 | } |
| 153 | return "" |
| 154 | } |