Skip to content

Commit

Permalink
Add pr contracts command to bulk apply for regression testing
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino committed Sep 19, 2024
1 parent 7099425 commit 8e5bd6a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cmd/command/pr/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
}
}

Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions pkg/pr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit 8e5bd6a

Please sign in to comment.