// Package tryevent is what the checker says on its stream checks when a whole try (a try of a delivery group, not narrowed // to a level or to some tests) starts and ends (issue 1921), and what the indexer, which hears it, reads: a try has no pull // request, so the indexer writes the candidate of its group from the members at their commits, and deletes it at the end. // The events carry the members, so no part reads the checker's state. package tryevent import ( "encoding/json" "fmt" "regexp" ) // The events of the checker's stream checks. const ( Started = "try.started" Ended = "try.ended" ) // IDOK is the shape of a try's id, which is also the name of its group (the key of its candidate). var IDOK = regexp.MustCompile(`^try-[0-9]{1,20}$`) // Member is one repository of the try: the exact commit and the trunk commit it is merged onto. type Member struct { Repository string `json:"repository"` Head string `json:"head"` Trunk string `json:"trunk"` } // Event is the body of both events: the try, and for Started its members; for Ended the state it ended in. type Event struct { ID string `json:"id"` Members []Member `json:"members,omitempty"` State string `json:"state,omitempty"` } // Decode reads an event body; a started event names its members. func Decode(event string, body []byte) (Event, error) { var e Event if err := json.Unmarshal(body, &e); err != nil { return e, err } if !IDOK.MatchString(e.ID) { return e, fmt.Errorf("%q is not a try's id, try-", e.ID) } if event == Started && len(e.Members) == 0 { return e, fmt.Errorf("the try %s starts with no member", e.ID) } return e, nil } // Open is one try that waits or runs, as the checker's tool checker_tries answers it. type Open struct { ID string `json:"id"` State string `json:"state"` Whole bool `json:"whole"` Members []Member `json:"members"` }