From b04ef2cc42ba136d3b709fca47a46882d3aca65e Mon Sep 17 00:00:00 2001 From: Damien Coraboeuf Date: Mon, 13 Nov 2023 09:27:08 +0000 Subject: [PATCH] feat: setup of testing validation stamps --- cmd/promotionLevelAuto.go | 77 +++++++++++++++++++++++++------- cmd/validationStamp.go | 60 +++++++++++++++++++++++++ cmd/validationStampSetupTests.go | 58 +++--------------------- go.mod | 2 +- 4 files changed, 130 insertions(+), 67 deletions(-) diff --git a/cmd/promotionLevelAuto.go b/cmd/promotionLevelAuto.go index e1d2fc3..05fbf29 100644 --- a/cmd/promotionLevelAuto.go +++ b/cmd/promotionLevelAuto.go @@ -5,16 +5,37 @@ import ( client "ontrack-cli/client" config "ontrack-cli/config" "os" + "slices" "github.com/spf13/cobra" "gopkg.in/yaml.v2" ) type AutoPromotions struct { + // List of validations and their configuration + Validations []ValidationConfig // List of promotions Promotions []PromotionConfig } +type ValidationConfig struct { + // Name of the validation + Name string + // Optional description for the validation + Description string + // Optional data type + DataType *string + // Optional data type config + DataTypeConfig *string + // Test configuration + Tests *TestSummaryValidationConfig +} + +type TestSummaryValidationConfig struct { + // Warning if skipped tests + WarningIfSkipped bool +} + type PromotionConfig struct { // Name of the promotion Name string @@ -38,6 +59,11 @@ By default, the definition of the promotions and their auto promotion is availab This YAML file has the following structure (example): +validations: + - name: unit-tests + description: Unit tests + tests: + warningIfSkipped: false promotions: - name: BRONZE validations: @@ -87,32 +113,53 @@ promotions: } yaml.Unmarshal(buf, &root) + // Setup of validations + var createdValidations []string + for _, validation := range root.Validations { + createdValidations = append(createdValidations, validation.Name) + // TODO Generic data type + // Tests data type + if validation.Tests != nil { + err = SetupTestValidationStamp( + project, + branch, + validation.Name, + validation.Description, + validation.Tests.WarningIfSkipped, + ) + if err != nil { + return err + } + } + } + // List of validations and promotions to setup var validationStamps []string // Going over all promotions for _, promotion := range root.Promotions { if len(promotion.Validations) > 0 { - for _, validation := range promotion.Validations { - validationStamps = append(validationStamps, validation) - } + validationStamps = append(validationStamps, promotion.Validations...) } } // Creates all the validations for _, validation := range validationStamps { - // Setup the validation stamp - err := client.SetupValidationStamp( - cfg, - project, - branch, - validation, - "", - "", - "", - ) - if err != nil { - return err + // Check if not already created + if slices.Index(createdValidations, validation) < 0 { + // Setup the validation stamp + err := client.SetupValidationStamp( + cfg, + project, + branch, + validation, + "", + "", + "", + ) + if err != nil { + return err + } } } diff --git a/cmd/validationStamp.go b/cmd/validationStamp.go index 69332f6..5316d5a 100644 --- a/cmd/validationStamp.go +++ b/cmd/validationStamp.go @@ -23,6 +23,9 @@ package cmd import ( "github.com/spf13/cobra" + + client "ontrack-cli/client" + config "ontrack-cli/config" ) // validationStampCmd represents the validationStamp command @@ -52,3 +55,60 @@ func init() { // is called directly, e.g.: // validationStampCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } + +// Utility method to setup a "tests" validation stamp +func SetupTestValidationStamp( + project string, + branch string, + validation string, + description string, + warningIfSkipped bool, +) error { + + cfg, err := config.GetSelectedConfiguration() + if err != nil { + return err + } + + var data struct { + SetupTestSummaryValidationStamp struct { + Errors []struct { + Message string + } + } + } + + err = client.GraphQLCall(cfg, ` + mutation SetupTestSummaryValidationStamp( + $project: String!, + $branch: String!, + $validation: String!, + $description: String, + $warningIfSkipped: Boolean! + ) { + setupTestSummaryValidationStamp(input: { + project: $project, + branch: $branch, + validation: $validation, + description: $description, + warningIfSkipped: $warningIfSkipped + }) { + errors { + message + } + } + } + `, map[string]interface{}{ + "project": project, + "branch": branch, + "validation": validation, + "description": description, + "warningIfSkipped": warningIfSkipped, + }, &data) + + if err != nil { + return err + } + + return client.CheckDataErrors(data.SetupTestSummaryValidationStamp.Errors) +} diff --git a/cmd/validationStampSetupTests.go b/cmd/validationStampSetupTests.go index 9f0da28..b1132e1 100644 --- a/cmd/validationStampSetupTests.go +++ b/cmd/validationStampSetupTests.go @@ -22,9 +22,6 @@ THE SOFTWARE. package cmd import ( - client "ontrack-cli/client" - config "ontrack-cli/config" - "github.com/spf13/cobra" ) @@ -66,54 +63,13 @@ For example: return err } - cfg, err := config.GetSelectedConfiguration() - if err != nil { - return err - } - - var data struct { - SetupTestSummaryValidationStamp struct { - Errors []struct { - Message string - } - } - } - if err := client.GraphQLCall(cfg, ` - mutation SetupTestSummaryValidationStamp( - $project: String!, - $branch: String!, - $validation: String!, - $description: String, - $warningIfSkipped: Boolean! - ) { - setupTestSummaryValidationStamp(input: { - project: $project, - branch: $branch, - validation: $validation, - description: $description, - warningIfSkipped: $warningIfSkipped - }) { - errors { - message - } - } - } - `, map[string]interface{}{ - "project": project, - "branch": branch, - "validation": validation, - "description": description, - "warningIfSkipped": warningIfSkipped, - }, &data); err != nil { - return err - } - - if err := client.CheckDataErrors(data.SetupTestSummaryValidationStamp.Errors); err != nil { - return err - } - - // OK - return nil + return SetupTestValidationStamp( + project, + branch, + validation, + description, + warningIfSkipped, + ) }, } diff --git a/go.mod b/go.mod index 40df841..6852b54 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module ontrack-cli -go 1.15 +go 1.21.1 require ( github.com/mitchellh/go-homedir v1.1.0