From 8e5bd6ac19dc2d7c2f738d3fd3b364aeffc126fc Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Thu, 19 Sep 2024 17:35:23 -0400 Subject: [PATCH] Add pr contracts command to bulk apply for regression testing --- cmd/command/pr/pr.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ pkg/pr/types.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/cmd/command/pr/pr.go b/cmd/command/pr/pr.go index 47e4d2a4..0bb4693b 100644 --- a/cmd/command/pr/pr.go +++ b/cmd/command/pr/pr.go @@ -97,6 +97,18 @@ func (p *Plural) prCommands() []cli.Command { }, }, }, + { + Name: "contracts", + Action: handlePrContracts, + Usage: "Runs a set of contract tests for your pr automations", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "file", + Usage: "the contract file to run", + Required: true, + }, + }, + }, } } @@ -126,6 +138,42 @@ func handleTestPrAutomation(c *cli.Context) error { return pr.Apply(template) } +func handlePrContracts(c *cli.Context) error { + contracts, err := pr.BuildContracts(c.String("file")) + if err != nil { + return err + } + + if contracts.Spec.Templates != nil { + tplCopy := contracts.Spec.Templates + if err := utils.CopyFile(tplCopy.From, tplCopy.To); err != nil { + return err + } + } + + if contracts.Spec.Workdir != "" { + if err := os.Chdir(contracts.Spec.Workdir); err != nil { + return err + } + } + + for _, contract := range contracts.Spec.Automations { + template, err := pr.BuildCRD(contract.File, contract.Context) + if err != nil { + return err + } + if contract.ExternalDir != "" { + template.Spec.Creates.ExternalDir = contract.ExternalDir + } + + if err := pr.Apply(template); err != nil { + return err + } + } + + return nil +} + func (p *Plural) handleCreatePrAutomation(c *cli.Context) error { if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { return err diff --git a/pkg/pr/types.go b/pkg/pr/types.go index 733c3600..dfcb8362 100644 --- a/pkg/pr/types.go +++ b/pkg/pr/types.go @@ -52,6 +52,31 @@ type RegexReplacement struct { Templated bool `json:"templated"` } +type PrContracts struct { + ApiVersion string `json:"apiVersion"` + Kind string `json:"kind"` + Metadata map[string]interface{} `json:"metadata"` + Context map[string]interface{} `json:"context"` + Spec PrContractsSpec `json:"spec"` +} + +type PrContractsSpec struct { + Templates *TemplateCopy `json:"templates"` + Workdir string `json:"workdir,omitempty"` + Automations []AutomationContract `json:"automations"` +} + +type TemplateCopy struct { + From string `json:"from"` + To string `json:"to"` +} + +type AutomationContract struct { + File string `json:"file"` + ExternalDir string `json:"externalDir,omitempty"` + Context string `json:"context"` +} + func Build(path string) (*PrTemplate, error) { pr := &PrTemplate{} data, err := os.ReadFile(path) @@ -65,3 +90,17 @@ func Build(path string) (*PrTemplate, error) { return pr, nil } + +func BuildContracts(path string) (*PrContracts, error) { + pr := &PrContracts{} + data, err := os.ReadFile(path) + if err != nil { + return pr, err + } + + if err := yaml.Unmarshal(data, pr); err != nil { + return pr, err + } + + return pr, err +}