Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom status report names #1013

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ type StepListStepItemModel map[string]stepmanModels.StepModel
type StepListItemModel map[string]interface{}

type PipelineModel 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"`
Triggers Triggers `json:"triggers,omitempty" yaml:"triggers,omitempty"`
Stages []StageListItemModel `json:"stages,omitempty" yaml:"stages,omitempty"`
Workflows GraphPipelineWorkflowListItemModel `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"`
Triggers Triggers `json:"triggers,omitempty" yaml:"triggers,omitempty"`
StatusReportName string `json:"status_report_name,omitempty" yaml:"status_report_name,omitempty"`
Stages []StageListItemModel `json:"stages,omitempty" yaml:"stages,omitempty"`
Workflows GraphPipelineWorkflowListItemModel `json:"workflows,omitempty" yaml:"workflows,omitempty"`
}

type StageListItemModel map[string]StageModel
Expand Down Expand Up @@ -105,15 +106,16 @@ func (d *GraphPipelineAlwaysRunMode) UnmarshalYAML(unmarshal func(interface{}) e
type WorkflowListItemModel map[string]WorkflowModel

type WorkflowModel 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"`
Triggers Triggers `json:"triggers,omitempty" yaml:"triggers,omitempty"`
BeforeRun []string `json:"before_run,omitempty" yaml:"before_run,omitempty"`
AfterRun []string `json:"after_run,omitempty" yaml:"after_run,omitempty"`
Environments []envmanModels.EnvironmentItemModel `json:"envs,omitempty" yaml:"envs,omitempty"`
Steps []StepListItemModel `json:"steps,omitempty" yaml:"steps,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty" yaml:"meta,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"`
Triggers Triggers `json:"triggers,omitempty" yaml:"triggers,omitempty"`
StatusReportName string `json:"status_report_name,omitempty" yaml:"status_report_name,omitempty"`
BeforeRun []string `json:"before_run,omitempty" yaml:"before_run,omitempty"`
AfterRun []string `json:"after_run,omitempty" yaml:"after_run,omitempty"`
Environments []envmanModels.EnvironmentItemModel `json:"envs,omitempty" yaml:"envs,omitempty"`
Steps []StepListItemModel `json:"steps,omitempty" yaml:"steps,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty" yaml:"meta,omitempty"`
}

type DockerCredentials struct {
Expand All @@ -131,10 +133,11 @@ type Container struct {
}

type AppModel 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"`
Environments []envmanModels.EnvironmentItemModel `json:"envs,omitempty" yaml:"envs,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"`
StatusReportName string `json:"status_report_name,omitempty" yaml:"status_report_name,omitempty"`
Environments []envmanModels.EnvironmentItemModel `json:"envs,omitempty" yaml:"envs,omitempty"`
}

type BitriseDataModel struct {
Expand Down
22 changes: 20 additions & 2 deletions models/models_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ func (workflow *WorkflowModel) Validate() error {
return err
}
}

return validateStatusReportName(workflow.StatusReportName)
}

const statusReportNameRegex = `^[a-zA-Z0-9,./():\-_ <>[\]|]*$`

func validateStatusReportName(statusReportName string) error {
if len(statusReportName) > 100 {
return fmt.Errorf("status_report_name (%s) is too long, max length is 100 characters", statusReportName)
}

re := regexp.MustCompile(statusReportNameRegex)
if !re.MatchString(statusReportName) {
return fmt.Errorf("status_report_name (%s) contains invalid characters, should match the '%s' regex", statusReportName, statusReportNameRegex)
}
return nil
}

Expand All @@ -383,7 +398,7 @@ func (app *AppModel) Validate() error {
return err
}
}
return nil
return validateStatusReportName(app.StatusReportName)
}

func (config *BitriseDataModel) Validate() ([]string, error) {
Expand Down Expand Up @@ -535,6 +550,10 @@ func validatePipelines(config *BitriseDataModel) ([]string, error) {
return pipelineWarnings, err
}

if err := validateStatusReportName(pipeline.StatusReportName); err != nil {
return pipelineWarnings, err
}

hasStages := len(pipeline.Stages) > 0
hasWorkflows := len(pipeline.Workflows) > 0

Expand All @@ -552,7 +571,6 @@ func validatePipelines(config *BitriseDataModel) ([]string, error) {
if hasWorkflows {
return pipelineWarnings, validateDAGPipeline(pipelineID, &pipeline, config)
}

}

return pipelineWarnings, nil
Expand Down
122 changes: 122 additions & 0 deletions models/models_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,36 @@ func TestValidateConfig(t *testing.T) {
require.Equal(t, 0, len(warnings))
}

t.Log("validate app status report name - max length")
{
bitriseData := BitriseDataModel{
FormatVersion: "1.4.0",
}

bitriseData.App.StatusReportName = strings.Repeat("a", 100)
_, err := bitriseData.Validate()
require.NoError(t, err)

bitriseData.App.StatusReportName += "a"
_, err = bitriseData.Validate()
require.EqualError(t, err, "status_report_name ("+bitriseData.App.StatusReportName+") is too long, max length is 100 characters")
}

t.Log("validate app status report name - allowed characters")
{
bitriseData := BitriseDataModel{
FormatVersion: "1.4.0",
}

bitriseData.App.StatusReportName = "aA0,./():-_< >[]|"
_, err := bitriseData.Validate()
require.NoError(t, err)

bitriseData.App.StatusReportName += "*"
_, err = bitriseData.Validate()
require.EqualError(t, err, "status_report_name ("+bitriseData.App.StatusReportName+") contains invalid characters, should match the '"+statusReportNameRegex+"' regex")
}

t.Log("Invalid bitriseData - pipeline ID empty")
{
bitriseData := BitriseDataModel{
Expand Down Expand Up @@ -536,6 +566,76 @@ func TestValidateConfig(t *testing.T) {
require.Equal(t, 1, len(warnings))
require.Equal(t, "invalid workflow ID (wf/id): doesn't conform to: [A-Za-z0-9-_.]", warnings[0])
}

t.Log("validate pipeline status report name - max length")
{
bitriseData := BitriseDataModel{
FormatVersion: "1.4.0",
Pipelines: map[string]PipelineModel{
"pipeline1": PipelineModel{
Stages: []StageListItemModel{
StageListItemModel{"stage1": StageModel{}},
},
},
},
Stages: map[string]StageModel{
"stage1": StageModel{
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow1": StageWorkflowModel{}},
},
},
},
Workflows: map[string]WorkflowModel{
"workflow1": WorkflowModel{},
},
}
pipeline := bitriseData.Pipelines["pipeline1"]

pipeline.StatusReportName = strings.Repeat("a", 100)
bitriseData.Pipelines["pipeline1"] = pipeline
_, err := bitriseData.Validate()
require.NoError(t, err)

pipeline.StatusReportName += "a"
bitriseData.Pipelines["pipeline1"] = pipeline
_, err = bitriseData.Validate()
require.EqualError(t, err, "status_report_name ("+pipeline.StatusReportName+") is too long, max length is 100 characters")
}

t.Log("validate pipeline status report name - allowed characters")
{
bitriseData := BitriseDataModel{
FormatVersion: "1.4.0",
Pipelines: map[string]PipelineModel{
"pipeline1": PipelineModel{
Stages: []StageListItemModel{
StageListItemModel{"stage1": StageModel{}},
},
},
},
Stages: map[string]StageModel{
"stage1": StageModel{
Workflows: []StageWorkflowListItemModel{
StageWorkflowListItemModel{"workflow1": StageWorkflowModel{}},
},
},
},
Workflows: map[string]WorkflowModel{
"workflow1": WorkflowModel{},
},
}
pipeline := bitriseData.Pipelines["pipeline1"]

pipeline.StatusReportName = "aA0,./():-_< >[]|"
bitriseData.Pipelines["pipeline1"] = pipeline
_, err := bitriseData.Validate()
require.NoError(t, err)

pipeline.StatusReportName += "*"
bitriseData.Pipelines["pipeline1"] = pipeline
_, err = bitriseData.Validate()
require.EqualError(t, err, "status_report_name ("+pipeline.StatusReportName+") contains invalid characters, should match the '"+statusReportNameRegex+"' regex")
}
}

func TestValidateConfig_Containers(t *testing.T) {
Expand Down Expand Up @@ -886,6 +986,28 @@ workflows:
require.NoError(t, err)
require.Equal(t, 1, len(warnings))
}

t.Log("validate workflow status report name - max length")
{
workflow := WorkflowModel{}

workflow.StatusReportName = strings.Repeat("a", 100)
require.NoError(t, workflow.Validate())

workflow.StatusReportName += "a"
require.EqualError(t, workflow.Validate(), "status_report_name ("+workflow.StatusReportName+") is too long, max length is 100 characters")
}

t.Log("validate workflow status report name - allowed characters")
{
workflow := WorkflowModel{}

workflow.StatusReportName = "aA0,./():-_< >[]|"
require.NoError(t, workflow.Validate())

workflow.StatusReportName += "*"
require.EqualError(t, workflow.Validate(), "status_report_name ("+workflow.StatusReportName+") contains invalid characters, should match the '"+statusReportNameRegex+"' regex")
}
}

// Trigger map
Expand Down