Skip to content

Commit

Permalink
[CI-2392] Use new model for stage workflow (#915)
Browse files Browse the repository at this point in the history
* Use new model for stage workflow

* Rename functions
  • Loading branch information
tothszabi authored Feb 16, 2024
1 parent 094dbb3 commit 83e3e40
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 50 deletions.
22 changes: 15 additions & 7 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ type StageListItemModel map[string]StageModel

// StageModel ...
type StageModel struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
ShouldAlwaysRun bool `json:"should_always_run,omitempty" yaml:"should_always_run,omitempty"`
AbortOnFail bool `json:"abort_on_fail,omitempty" yaml:"abort_on_fail,omitempty"`
RunIf string `json:"run_if,omitempty" yaml:"run_if,omitempty"`
Workflows []WorkflowListItemModel `json:"workflows,omitempty" yaml:"workflows,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
ShouldAlwaysRun bool `json:"should_always_run,omitempty" yaml:"should_always_run,omitempty"`
AbortOnFail bool `json:"abort_on_fail,omitempty" yaml:"abort_on_fail,omitempty"`
RunIf string `json:"run_if,omitempty" yaml:"run_if,omitempty"`
Workflows []StageWorkflowListItemModel `json:"workflows,omitempty" yaml:"workflows,omitempty"`
}

// StageWorkflowListItemModel ...
type StageWorkflowListItemModel map[string]StageWorkflowModel

// StageWorkflowModel ...
type StageWorkflowModel struct {
RunIf string `json:"run_if,omitempty" yaml:"run_if,omitempty"`
}

// WorkflowListItemModel ...
Expand Down
18 changes: 8 additions & 10 deletions models/models_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func validatePipelines(config *BitriseDataModel) ([]string, error) {
}

for _, pipelineStage := range pipeline.Stages {
pipelineStageID, err := GetStageIDFromListItemModel(pipelineStage)
pipelineStageID, err := getStageID(pipelineStage)
if err != nil {
return pipelineWarnings, err
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func validateStages(config *BitriseDataModel) ([]string, error) {

for _, stageWorkflow := range stage.Workflows {
found := false
stageWorkflowID, err := GetWorkflowIDFromListItemModel(stageWorkflow)
stageWorkflowID, err := getWorkflowID(stageWorkflow)

if isUtilityWorkflow(stageWorkflowID) {
return stageWarnings, fmt.Errorf("workflow (%s) defined in stage (%s), is a utility workflow", stageWorkflowID, ID)
Expand Down Expand Up @@ -778,29 +778,27 @@ func MergeStepWith(step, otherStep stepmanModels.StepModel) (stepmanModels.StepM
// ----------------------------
// --- WorkflowIDData

// GetWorkflowIDFromListItemModel ...
func GetWorkflowIDFromListItemModel(workflowListItem WorkflowListItemModel) (string, error) {
func getWorkflowID(workflowListItem StageWorkflowListItemModel) (string, error) {
if len(workflowListItem) > 1 {
return "", errors.New("WorkflowListItem contains more than 1 key-value pair")
return "", errors.New("StageWorkflowListItemModel contains more than 1 key-value pair")
}
for key := range workflowListItem {
return key, nil
}
return "", errors.New("WorkflowListItem does not contain a key-value pair")
return "", errors.New("StageWorkflowListItemModel does not contain a key-value pair")
}

// ----------------------------
// --- StageIDData

// GetStageIDFromListItemModel ...
func GetStageIDFromListItemModel(stageListItem StageListItemModel) (string, error) {
func getStageID(stageListItem StageListItemModel) (string, error) {
if len(stageListItem) > 1 {
return "", errors.New("StageListItem contains more than 1 key-value pair")
return "", errors.New("StageListItemModel contains more than 1 key-value pair")
}
for key := range stageListItem {
return key, nil
}
return "", errors.New("StageListItem does not contain a key-value pair")
return "", errors.New("StageListItemModel does not contain a key-value pair")
}

// ----------------------------
Expand Down
66 changes: 33 additions & 33 deletions models/models_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ func TestValidateConfig(t *testing.T) {
},
Stages: map[string]StageModel{
"stage1": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"workflow1": WorkflowModel{}},
WorkflowListItemModel{"workflow2": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow1": StageWorkflowModel{}},
StageWorkflowListItemModel{"workflow2": StageWorkflowModel{}},
},
},
"stage2": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"workflow3": WorkflowModel{}},
WorkflowListItemModel{"workflow4": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow3": StageWorkflowModel{}},
StageWorkflowListItemModel{"workflow4": StageWorkflowModel{}},
},
},
"stage3": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"workflow5": WorkflowModel{}},
WorkflowListItemModel{"workflow6": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow5": StageWorkflowModel{}},
StageWorkflowListItemModel{"workflow6": StageWorkflowModel{}},
},
},
"stage4": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"workflow7": WorkflowModel{}},
WorkflowListItemModel{"workflow8": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow7": StageWorkflowModel{}},
StageWorkflowListItemModel{"workflow8": StageWorkflowModel{}},
},
},
},
Expand Down Expand Up @@ -105,8 +105,8 @@ func TestValidateConfig(t *testing.T) {
},
Stages: map[string]StageModel{
"stage1": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"workflow1": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow1": StageWorkflowModel{}},
},
},
},
Expand Down Expand Up @@ -163,8 +163,8 @@ func TestValidateConfig(t *testing.T) {
},
Stages: map[string]StageModel{
"stage1": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"workflow1": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow1": StageWorkflowModel{}},
},
},
},
Expand Down Expand Up @@ -197,8 +197,8 @@ func TestValidateConfig(t *testing.T) {
FormatVersion: "1.4.0",
Stages: map[string]StageModel{
"st/id": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"workflow1": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow1": StageWorkflowModel{}},
},
},
},
Expand All @@ -218,7 +218,7 @@ func TestValidateConfig(t *testing.T) {
FormatVersion: "1.4.0",
Stages: map[string]StageModel{
"stage1": StageModel{
Workflows: []WorkflowListItemModel{},
Workflows: []StageWorkflowListItemModel{},
},
},
}
Expand Down Expand Up @@ -248,8 +248,8 @@ func TestValidateConfig(t *testing.T) {
FormatVersion: "1.4.0",
Stages: map[string]StageModel{
"stage1": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"workflow2": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow2": StageWorkflowModel{}},
},
},
},
Expand All @@ -269,8 +269,8 @@ func TestValidateConfig(t *testing.T) {
FormatVersion: "12",
Stages: map[string]StageModel{
"stage1": StageModel{
Workflows: []WorkflowListItemModel{
WorkflowListItemModel{"_utility_workflow": WorkflowModel{}},
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"_utility_workflow": StageWorkflowModel{}},
},
},
},
Expand Down Expand Up @@ -634,36 +634,36 @@ func TestGetInputByKey(t *testing.T) {
// --- WorkflowIDData

func TestGetWorkflowIDFromListItemModel(t *testing.T) {
workflowData := WorkflowModel{}
workflowData := StageWorkflowModel{}

t.Log("valid workflowlist item")
{
workflowListItem := WorkflowListItemModel{
workflowListItem := StageWorkflowListItemModel{
"workflow1": workflowData,
}

id, err := GetWorkflowIDFromListItemModel(workflowListItem)
id, err := getWorkflowID(workflowListItem)
require.NoError(t, err)
require.Equal(t, "workflow1", id)
}

t.Log("invalid workflowlist item - more than 1 workflow")
{
workflowListItem := WorkflowListItemModel{
workflowListItem := StageWorkflowListItemModel{
"workflow1": workflowData,
"workflow2": workflowData,
}

id, err := GetWorkflowIDFromListItemModel(workflowListItem)
id, err := getWorkflowID(workflowListItem)
require.Error(t, err)
require.Equal(t, "", id)
}

t.Log("invalid workflowlist item - no workflow")
{
workflowListItem := WorkflowListItemModel{}
workflowListItem := StageWorkflowListItemModel{}

id, err := GetWorkflowIDFromListItemModel(workflowListItem)
id, err := getWorkflowID(workflowListItem)
require.Error(t, err)
require.Equal(t, "", id)
}
Expand All @@ -681,7 +681,7 @@ func TestGetStageIDFromListItemModel(t *testing.T) {
"stage1": stageData,
}

id, err := GetStageIDFromListItemModel(stageListItem)
id, err := getStageID(stageListItem)
require.NoError(t, err)
require.Equal(t, "stage1", id)
}
Expand All @@ -693,7 +693,7 @@ func TestGetStageIDFromListItemModel(t *testing.T) {
"stage2": stageData,
}

id, err := GetStageIDFromListItemModel(stageListItem)
id, err := getStageID(stageListItem)
require.Error(t, err)
require.Equal(t, "", id)
}
Expand All @@ -702,7 +702,7 @@ func TestGetStageIDFromListItemModel(t *testing.T) {
{
stageListItem := StageListItemModel{}

id, err := GetStageIDFromListItemModel(stageListItem)
id, err := getStageID(stageListItem)
require.Error(t, err)
require.Equal(t, "", id)
}
Expand Down

0 comments on commit 83e3e40

Please sign in to comment.