Files
archie/internal/infrastructure/controlplanestore/intake_routes.go
T

93 lines
3.0 KiB
Go

package controlplanestore
import (
"context"
"encoding/json"
"fmt"
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
)
// IntakeRoutesKind is the resource holding which workflow an issue runs by
// its labels.
const IntakeRoutesKind = "intake-routes"
func intakeRoutesDefinition() Definition {
return Definition{
Kind: IntakeRoutesKind,
Title: "Issue routing",
Document: workflow.IntakeRoutes{},
ApplyMode: "live",
Seed: func(config.Config) any { return workflow.ShippedIntakeRoutes() },
Validate: func(input []byte) error {
return validateAs(input, func(routes workflow.IntakeRoutes) error { return routes.Validate() })
},
}
}
// checkIntakeRoutes refuses routes naming a workflow the writing org has not
// defined.
func (s *Server) checkIntakeRoutes(ctx context.Context, orgID string, value []byte) error {
var routes workflow.IntakeRoutes
if err := json.Unmarshal(value, &routes); err != nil {
return err
}
stored, err := s.resource(ctx, orgID, WorkflowDefinitionsKind)
if err != nil {
return err
}
return s.checkRoutesDefined(routes, stored.Value)
}
// checkRoutesDefinedBy refuses a workflow definitions write that removes a
// workflow the org's intake routes still name, which would park every issue
// matching that route at pin time.
func (s *Server) checkRoutesDefinedBy(ctx context.Context, orgID string, definitions []byte) error {
stored, err := s.resource(ctx, orgID, IntakeRoutesKind)
if err != nil {
return err
}
var routes workflow.IntakeRoutes
if err := json.Unmarshal(stored.Value, &routes); err != nil {
return err
}
if len(routes.Rules) == 0 && routes.Default == "" {
return nil
}
return s.checkRoutesDefined(routes, definitions)
}
func (s *Server) checkRoutesDefined(routes workflow.IntakeRoutes, definitions []byte) error {
collection, err := workflow.DecodeDefinitionCollection(definitions, s.steps)
if err != nil {
return err
}
available := make(map[string]struct{}, len(collection.Definitions))
for _, definition := range collection.Definitions {
available[definition.ID] = struct{}{}
}
if err := routes.CheckDefined(available); err != nil {
return fmt.Errorf("%w: %w", ErrValidation, err)
}
return nil
}
// IntakeRoutes returns where the caller's org sends an issue by its labels.
func (c *WorkflowDefinitionsClient) IntakeRoutes(ctx context.Context) (workflow.IntakeRoutes, error) {
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: IntakeRoutesKind})
if err != nil {
c.ApplyStatus.Report(ctx, IntakeRoutesKind, 0, err)
return workflow.IntakeRoutes{}, controlplanerpc.ClientError(err)
}
var routes workflow.IntakeRoutes
err = json.Unmarshal(response.Resource.ValueJson, &routes)
if err == nil {
err = routes.Validate()
}
c.ApplyStatus.Report(ctx, IntakeRoutesKind, response.Resource.Version, err)
return routes, err
}