package meshcatalogclient import ( "crypto/sha256" "encoding/hex" "fmt" "regexp" "sort" "strings" seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-sdk/go/keys" ) // FingerprintSet is the `fpset` of a candidate's pull requests: the first 12 hex digits of the sha256 over the lines // `=` or `=none:` of every module with an entry in a pull's fps, sorted and joined by a newline, // so it is the same for every order and changes with any fingerprint or reason. A manifest digest does not enter: the // fingerprint already covers the manifest. It is the empty string when no pull request carries a fingerprint. The checker // keys its verdict by it. func FingerprintSet(pulls []seat.CandidatesByGroupValuePullsItem) string { lines := map[string]bool{} for _, p := range pulls { for module, m := range p.Fps { if m.Fp2 != "" { lines[module+"="+m.Fp2] = true } else { lines[module+"=none:"+m.None2] = true } } } if len(lines) == 0 { return "" } all := make([]string, 0, len(lines)) for l := range lines { all = append(all, l) } sort.Strings(all) sum := sha256.Sum256([]byte(strings.Join(all, "\n"))) return hex.EncodeToString(sum[:])[:12] } // CandidatesKey is the key, in the state candidates, of a group: `` escaped (keys.EscapeName), the branch name the // group's pull requests share (a slash is allowed); a lone pull request is a group of one under its branch name. func CandidatesKey(group string) (string, error) { if group == "" { return "", fmt.Errorf("meshcatalogclient: the group name is empty") } return seat.CandidatesByGroupKey(keys.EscapeName(group)), nil } // ParseCandidatesKey is the inverse of CandidatesKey. func ParseCandidatesKey(key string) (string, error) { part, ok := seat.ParseCandidatesByGroupKey(key) if !ok { return "", fmt.Errorf("meshcatalogclient: %q is not a candidates key", key) } group, err := keys.UnescapeName(part) if err != nil { return "", fmt.Errorf("meshcatalogclient: %q is not a candidates key: %w", key, err) } return group, nil } var tryGroupOK = regexp.MustCompile(`^try-[0-9]{1,20}$`) // IsTryGroup says a candidates key is the group of a whole try, try-, which is also the try's id: the indexer makes the // candidate of a try's group from its previews, and the checker judges it by its try, never as a pull request's check. func IsTryGroup(key string) bool { return tryGroupOK.MatchString(key) } // NoneNoPreview is why a candidate's touched module has no fingerprint of the new form yet: its merge preview is not // computed. The indexer writes it; a reader that waits for a try's candidate takes one that still says it as not yet the // candidate of the heads previewed. const NoneNoPreview = "no merge preview is computed"