Skip to content

Commit

Permalink
feat: [ASSMT-650]: show not supported stages in console (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielGz authored Nov 4, 2024
1 parent 7d543cb commit 77d97bf
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func migrateSpinnakerApplication() error {
}

payload := map[string][]map[string]interface{}{"pipelines": pipelines}

stages, _ := getSupportedStages()
if stages != nil {
checkUnsupportedStages(payload, stages)
}
_, err = createSpinnakerPipelines(payload)
return err
}
81 changes: 81 additions & 0 deletions pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"errors"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"

log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -126,10 +128,68 @@ func migrateSpinnakerPipelines() error {
}

payload := map[string][]map[string]interface{}{"pipelines": pipelines}

stages, _ := getSupportedStages()
if stages != nil {
checkUnsupportedStages(payload, stages)
}

_, err = createSpinnakerPipelines(payload)
return err
}

func checkUnsupportedStages(payload map[string][]map[string]interface{}, supportedStages []string) {
unsupportedStagesMap := make(map[string][]string)

// Convert supported stages to lowercase for case-insensitive comparison
supportedMap := make(map[string]bool)
for _, stage := range supportedStages {
supportedMap[strings.ToLower(stage)] = true
}

// Regular expression to remove non-alphanumeric characters
re := regexp.MustCompile(`[^a-zA-Z0-9]+`)

// Iterate over each pipeline
for _, pipeline := range payload["pipelines"] {
unsupportedStages := []string{}

// Get stages from the pipeline
if stages, ok := pipeline["stages"].([]interface{}); ok {
for _, stage := range stages {
// Ensure stage is a map and retrieve the type
if stageMap, ok := stage.(map[string]interface{}); ok {
if stageType, ok := stageMap["type"].(string); ok {
// Remove special characters and convert to lowercase
cleanStageType := strings.ToLower(re.ReplaceAllString(stageType, ""))

// Check if the cleaned stage type is unsupported
if !supportedMap[cleanStageType] {
unsupportedStages = append(unsupportedStages, stageType) // Add original type for clarity in logs
}
}
}
}
}

// Add to results if there are unsupported stages
if len(unsupportedStages) > 0 {
if name, ok := pipeline["name"].(string); ok {
unsupportedStagesMap[name] = unsupportedStages
}
}
}

// Log unsupported stages
if len(unsupportedStagesMap) > 0 {
for pipelineID, stages := range unsupportedStagesMap {
log.Warnf("Pipeline ID: %s, Unsupported Stages: %v", pipelineID, stages)
}
} else {
log.Info("All stages in all pipelines are supported.")
}
}

func fetchDependentPipelines(pipelines []map[string]interface{}, err error, authMethod string) ([]map[string]interface{}, error) {
var pipelinesToRemove []int
for _, pipeline := range pipelines {
Expand Down Expand Up @@ -391,6 +451,27 @@ func getSinglePipeline(authMethod string, name string) ([]byte, error) {
return GetWithAuth(migrationReq.SpinnakerHost, "applications/"+migrationReq.SpinnakerAppName+"/pipelineConfigs/"+name, authMethod, migrationReq.Auth64, migrationReq.Cert, migrationReq.Key, migrationReq.AllowInsecureReq)
}

func getSupportedStages() ([]string, error) {
url := GetUrl(migrationReq.Environment, MigratorService, "spinnaker/pipelines/stages", migrationReq.Account)
resp, err := Get(url, migrationReq.Auth)
if err != nil {
log.Warnf("failed to fetch supported stages: %v", err)
return nil, err
}
byteData, err := json.Marshal(resp.Resource)
if err != nil {
log.Warnf("failed to parse supported stages: %v", err)
}

var stages []string
if err := json.Unmarshal(byteData, &stages); err != nil {
log.Warnf("failed to parse supported stages: %v", err)
return nil, err
}

return stages, nil
}

func createSpinnakerPipelines(pipelines interface{}) (reqId string, err error) {
queryParams := map[string]string{
ProjectIdentifier: migrationReq.ProjectIdentifier,
Expand Down

0 comments on commit 77d97bf

Please sign in to comment.