Skip to content

Commit

Permalink
Remove normalize subcommand (#924)
Browse files Browse the repository at this point in the history
* Remove unused `normalize` subcommand

* Revert to go 1.18

* Fix syntax

* Go 1.21 again

* Remove ioutil usage

* Fix
  • Loading branch information
ofalvai authored Mar 22, 2024
1 parent 0e87d89 commit 2c558aa
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 602 deletions.
373 changes: 0 additions & 373 deletions bitrise/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"github.com/bitrise-io/bitrise/configs"
"github.com/bitrise-io/bitrise/log"
"github.com/bitrise-io/bitrise/models"
"github.com/bitrise-io/bitrise/tools"
envmanModels "github.com/bitrise-io/envman/models"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/command/git"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/pointers"
Expand Down Expand Up @@ -212,23 +210,6 @@ func SetBuildFailedEnv(failed bool) error {
return os.Setenv("BITRISE_BUILD_STATUS", statusStr)
}

// SaveConfigToFile ...
func SaveConfigToFile(pth string, bitriseConf models.BitriseDataModel) error {
contBytes, err := generateYAML(bitriseConf)
if err != nil {
return err
}
return fileutil.WriteBytesToFile(pth, contBytes)
}

func generateYAML(v interface{}) ([]byte, error) {
bytes, err := yaml.Marshal(v)
if err != nil {
return []byte{}, err
}
return bytes, nil
}

func normalizeValidateFillMissingDefaults(bitriseData *models.BitriseDataModel) ([]string, error) {
if err := bitriseData.Normalize(); err != nil {
return []string{}, err
Expand Down Expand Up @@ -328,357 +309,3 @@ func ReadSpecStep(pth string) (stepmanModels.StepModel, error) {

return stepModel, nil
}

func getInputByKey(inputs []envmanModels.EnvironmentItemModel, key string) (envmanModels.EnvironmentItemModel, error) {
for _, input := range inputs {
aKey, _, err := input.GetKeyValuePair()
if err != nil {
return envmanModels.EnvironmentItemModel{}, err
}
if aKey == key {
return input, nil
}
}
return envmanModels.EnvironmentItemModel{}, fmt.Errorf("No Environmnet found for key (%s)", key)
}

func isStringSliceWithSameElements(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}

m := make(map[string]bool, len(s1))
for _, s := range s1 {
m[s] = true
}

for _, s := range s2 {
v, found := m[s]
if !found || !v {
return false
}
delete(m, s)
}
return len(m) == 0
}

func isDependecyEqual(d1, d2 stepmanModels.DependencyModel) bool {
return (d1.Manager == d2.Manager && d1.Name == d2.Name)
}

func containsDependecy(m map[stepmanModels.DependencyModel]bool, d1 stepmanModels.DependencyModel) bool {
for d2 := range m {
if isDependecyEqual(d1, d2) {
return true
}
}
return false
}

func isDependencySliceWithSameElements(s1, s2 []stepmanModels.DependencyModel) bool {
if len(s1) != len(s2) {
return false
}

m := make(map[stepmanModels.DependencyModel]bool, len(s1))
for _, s := range s1 {
m[s] = true
}

for _, d := range s2 {
if containsDependecy(m, d) == false {
return false
}
delete(m, d)
}
return len(m) == 0
}

func removeStepDefaultsAndFillStepOutputs(stepListItem *models.StepListItemModel, defaultStepLibSource string) error {
// Create stepIDData
compositeStepIDStr, workflowStep, err := models.GetStepIDStepDataPair(*stepListItem)
if err != nil {
return err
}
stepIDData, err := models.CreateStepIDDataFromString(compositeStepIDStr, defaultStepLibSource)
if err != nil {
return err
}

// Activate step - get step.yml
tempStepCloneDirPath, err := pathutil.NormalizedOSTempDirPath("step_clone")
if err != nil {
return err
}
tempStepYMLDirPath, err := pathutil.NormalizedOSTempDirPath("step_yml")
if err != nil {
return err
}
tempStepYMLFilePath := filepath.Join(tempStepYMLDirPath, "step.yml")

if stepIDData.SteplibSource == "path" {
stepAbsLocalPth, err := pathutil.AbsPath(stepIDData.IDorURI)
if err != nil {
return err
}
if err := command.CopyFile(filepath.Join(stepAbsLocalPth, "step.yml"), tempStepYMLFilePath); err != nil {
return err
}
} else if stepIDData.SteplibSource == "git" {
repo, err := git.New(tempStepCloneDirPath)
if err != nil {
return err
}

var cloneCmd *command.Model
if stepIDData.Version == "" {
cloneCmd = repo.Clone(stepIDData.IDorURI, "--depth=1")
} else {
cloneCmd = repo.CloneTagOrBranch(stepIDData.IDorURI, stepIDData.Version, "--depth=1")
}
if err := cloneCmd.Run(); err != nil {
return err
}

if err := command.CopyFile(filepath.Join(tempStepCloneDirPath, "step.yml"), tempStepYMLFilePath); err != nil {
return err
}
} else if stepIDData.SteplibSource == "_" {
// Steplib independent steps are completly defined in workflow
tempStepYMLFilePath = ""
} else if stepIDData.SteplibSource != "" {
if err := tools.StepmanSetup(stepIDData.SteplibSource); err != nil {
return err
}
if err := tools.StepmanActivate(stepIDData.SteplibSource, stepIDData.IDorURI, stepIDData.Version, tempStepCloneDirPath, tempStepYMLFilePath); err != nil {
return err
}
} else {
return errors.New("Failed to fill step ouputs: unknown SteplibSource")
}

// Fill outputs
if tempStepYMLFilePath != "" {
specStep, err := ReadSpecStep(tempStepYMLFilePath)
if err != nil {
return err
}

if workflowStep.Title != nil && specStep.Title != nil && *workflowStep.Title == *specStep.Title {
workflowStep.Title = nil
}
if workflowStep.Description != nil && specStep.Description != nil && *workflowStep.Description == *specStep.Description {
workflowStep.Description = nil
}
if workflowStep.Summary != nil && specStep.Summary != nil && *workflowStep.Summary == *specStep.Summary {
workflowStep.Summary = nil
}
if workflowStep.Website != nil && specStep.Website != nil && *workflowStep.Website == *specStep.Website {
workflowStep.Website = nil
}
if workflowStep.SourceCodeURL != nil && specStep.SourceCodeURL != nil && *workflowStep.SourceCodeURL == *specStep.SourceCodeURL {
workflowStep.SourceCodeURL = nil
}
if workflowStep.SupportURL != nil && specStep.SupportURL != nil && *workflowStep.SupportURL == *specStep.SupportURL {
workflowStep.SupportURL = nil
}
workflowStep.PublishedAt = nil
if workflowStep.Source != nil && specStep.Source != nil {
if workflowStep.Source.Git == specStep.Source.Git {
workflowStep.Source.Git = ""
}
if workflowStep.Source.Commit == specStep.Source.Commit {
workflowStep.Source.Commit = ""
}
}
if isStringSliceWithSameElements(workflowStep.HostOsTags, specStep.HostOsTags) {
workflowStep.HostOsTags = []string{}
}
if isStringSliceWithSameElements(workflowStep.ProjectTypeTags, specStep.ProjectTypeTags) {
workflowStep.ProjectTypeTags = []string{}
}
if isStringSliceWithSameElements(workflowStep.TypeTags, specStep.TypeTags) {
workflowStep.TypeTags = []string{}
}
if isDependencySliceWithSameElements(workflowStep.Dependencies, specStep.Dependencies) {
workflowStep.Dependencies = []stepmanModels.DependencyModel{}
}
if workflowStep.IsRequiresAdminUser != nil && specStep.IsRequiresAdminUser != nil && *workflowStep.IsRequiresAdminUser == *specStep.IsRequiresAdminUser {
workflowStep.IsRequiresAdminUser = nil
}
if workflowStep.IsAlwaysRun != nil && specStep.IsAlwaysRun != nil && *workflowStep.IsAlwaysRun == *specStep.IsAlwaysRun {
workflowStep.IsAlwaysRun = nil
}
if workflowStep.IsSkippable != nil && specStep.IsSkippable != nil && *workflowStep.IsSkippable == *specStep.IsSkippable {
workflowStep.IsSkippable = nil
}
if workflowStep.RunIf != nil && specStep.RunIf != nil && *workflowStep.RunIf == *specStep.RunIf {
workflowStep.RunIf = nil
}

inputs := []envmanModels.EnvironmentItemModel{}
for _, input := range workflowStep.Inputs {
sameValue := false

wfKey, wfValue, err := input.GetKeyValuePair()
if err != nil {
return err
}

wfOptions, err := input.GetOptions()
if err != nil {
return err
}

sInput, err := getInputByKey(specStep.Inputs, wfKey)
if err != nil {
return err
}

_, sValue, err := sInput.GetKeyValuePair()
if err != nil {
return err
}

if wfValue == sValue {
sameValue = true
}

sOptions, err := sInput.GetOptions()
if err != nil {
return err
}

hasOptions := false

if wfOptions.IsSensitive != nil && sOptions.IsSensitive != nil && *wfOptions.IsSensitive == *sOptions.IsSensitive {
wfOptions.IsSensitive = nil
} else {
hasOptions = true
}

if wfOptions.IsExpand != nil && sOptions.IsExpand != nil && *wfOptions.IsExpand == *sOptions.IsExpand {
wfOptions.IsExpand = nil
} else {
hasOptions = true
}

if wfOptions.SkipIfEmpty != nil && sOptions.SkipIfEmpty != nil && *wfOptions.SkipIfEmpty == *sOptions.SkipIfEmpty {
wfOptions.SkipIfEmpty = nil
} else {
hasOptions = true
}

if wfOptions.Title != nil && sOptions.Title != nil && *wfOptions.Title == *sOptions.Title {
wfOptions.Title = nil
} else {
hasOptions = true
}

if wfOptions.Description != nil && sOptions.Description != nil && *wfOptions.Description == *sOptions.Description {
wfOptions.Description = nil
} else {
hasOptions = true
}

if wfOptions.Summary != nil && sOptions.Summary != nil && *wfOptions.Summary == *sOptions.Summary {
wfOptions.Summary = nil
} else {
hasOptions = true
}

if wfOptions.Category != nil && sOptions.Category != nil && *wfOptions.Category == *sOptions.Category {
wfOptions.Category = nil
} else {
hasOptions = true
}

if isStringSliceWithSameElements(wfOptions.ValueOptions, sOptions.ValueOptions) {
wfOptions.ValueOptions = []string{}
} else {
hasOptions = true
}

if wfOptions.IsRequired != nil && sOptions.IsRequired != nil && *wfOptions.IsRequired == *sOptions.IsRequired {
wfOptions.IsRequired = nil
} else {
hasOptions = true
}

if wfOptions.IsDontChangeValue != nil && sOptions.IsDontChangeValue != nil && *wfOptions.IsDontChangeValue == *sOptions.IsDontChangeValue {
wfOptions.IsDontChangeValue = nil
} else {
hasOptions = true
}

if wfOptions.IsTemplate != nil && sOptions.IsTemplate != nil && *wfOptions.IsTemplate == *sOptions.IsTemplate {
wfOptions.IsTemplate = nil
} else {
hasOptions = true
}

if !hasOptions && sameValue {
// default env
} else {
if hasOptions {
input[envmanModels.OptionsKey] = wfOptions
} else {
delete(input, envmanModels.OptionsKey)
}

inputs = append(inputs, input)
}
}

workflowStep.Inputs = inputs

// We need only key-value and title from spec outputs
outputs := []envmanModels.EnvironmentItemModel{}
for _, output := range specStep.Outputs {
sKey, sValue, err := output.GetKeyValuePair()
if err != nil {
return err
}

sOptions, err := output.GetOptions()
if err != nil {
return err
}

newOutput := envmanModels.EnvironmentItemModel{
sKey: sValue,
envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
Title: sOptions.Title,
},
}

outputs = append(outputs, newOutput)
}

workflowStep.Outputs = outputs

(*stepListItem)[compositeStepIDStr] = workflowStep
}

// Cleanup
if err := command.RemoveDir(tempStepCloneDirPath); err != nil {
return errors.New(fmt.Sprint("Failed to remove step clone dir: ", err))
}
if err := command.RemoveDir(tempStepYMLDirPath); err != nil {
return errors.New(fmt.Sprint("Failed to remove step clone dir: ", err))
}

return nil
}

// RemoveConfigRedundantFieldsAndFillStepOutputs ...
func RemoveConfigRedundantFieldsAndFillStepOutputs(config *models.BitriseDataModel) error {
for _, workflow := range config.Workflows {
for _, stepListItem := range workflow.Steps {
if err := removeStepDefaultsAndFillStepOutputs(&stepListItem, config.DefaultStepLibSource); err != nil {
return err
}
}
}
return config.RemoveRedundantFields()
}
Loading

0 comments on commit 2c558aa

Please sign in to comment.