50 lines
2.2 KiB
Go
50 lines
2.2 KiB
Go
package check
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/clients/meshdeliveryclient"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdelivery"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdeliverychecker"
|
|
)
|
|
|
|
// The checker's owed state holds the one act it owes, the status mesh/check on a pull request at a head: the gate's owed act
|
|
// with the context mesh/check. Its value has the shape of the gate's (the seat mesh-delivery's owed), so its rules and its
|
|
// codec are the mesh-delivery client's; only the checker reads it, so the checker's own narrowing to its one act lives here.
|
|
|
|
// CheckerOwedAct is the act the checker owes.
|
|
const CheckerOwedAct = meshdeliveryclient.OwedStatus + "=" + meshdeliveryclient.StatusContextCheck
|
|
|
|
// CheckerOwedKey is the key, in the checker's owed state, of the status the checker owes on a pull request at a head.
|
|
func CheckerOwedKey(repository string, number int, head string) (string, error) {
|
|
return meshdeliveryclient.OwedKey(repository, number, head, CheckerOwedAct)
|
|
}
|
|
|
|
func checkCheckerOwed(o meshdeliverychecker.OwedByRepositoryNumberHeadActValue) error {
|
|
if o.Act != meshdeliveryclient.OwedStatus || o.Context != meshdeliveryclient.StatusContextCheck {
|
|
return fmt.Errorf("the checker owes only a status with the context %s, not the act %q with the context %q", meshdeliveryclient.StatusContextCheck, o.Act, o.Context)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeCheckerOwed is o as the gate's owed value, refused for any act but the checker's status.
|
|
func EncodeCheckerOwed(o meshdeliverychecker.OwedByRepositoryNumberHeadActValue) ([]byte, error) {
|
|
if err := checkCheckerOwed(o); err != nil {
|
|
return nil, err
|
|
}
|
|
return meshdeliveryclient.EncodeOwed(meshdelivery.OwedByRepositoryNumberHeadActValue(o))
|
|
}
|
|
|
|
// DecodeCheckerOwed reads an owed act of the checker's state.
|
|
func DecodeCheckerOwed(data []byte) (meshdeliverychecker.OwedByRepositoryNumberHeadActValue, error) {
|
|
g, err := meshdeliveryclient.DecodeOwed(data)
|
|
if err != nil {
|
|
return meshdeliverychecker.OwedByRepositoryNumberHeadActValue{}, err
|
|
}
|
|
o := meshdeliverychecker.OwedByRepositoryNumberHeadActValue(g)
|
|
if err := checkCheckerOwed(o); err != nil {
|
|
return meshdeliverychecker.OwedByRepositoryNumberHeadActValue{}, err
|
|
}
|
|
return o, nil
|
|
}
|