Skip to content
Closed
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
69 changes: 69 additions & 0 deletions cmd/pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,72 @@ func (suite *PipelineSuite) TestPipelineStateWithoutResult() {
suite.Assert().Equal("IN_PROGRESS", p.State.Name)
suite.Assert().Nil(p.State.Result)
}

func (suite *PipelineSuite) TestPipelineWithInvalidTargetType() {
payload := []byte(`{
"type": "pipeline",
"uuid": "{a1b2c3d4-e5f6-7890-abcd-ef1234567890}",
"build_number": 1,
"state": {
"type": "pipeline_state_in_progress",
"name": "IN_PROGRESS"
},
"target": {
"type": "invalid_target_type",
"ref_type": "branch",
"ref_name": "develop"
},
"created_on": "2024-01-15T10:30:00.000000+00:00",
"duration_in_seconds": 0,
"creator": {
"type": "user",
"display_name": "Test User"
},
"repository": {
"type": "repository",
"name": "test-repo",
"full_name": "workspace/test-repo"
},
"links": {}
}`)
var p pipeline.Pipeline
err := json.Unmarshal(payload, &p)
suite.Require().Error(err)
suite.Assert().Contains(err.Error(), "Invalid Type")
suite.Assert().Contains(err.Error(), "invalid_target_type")
suite.Assert().Contains(err.Error(), "pipeline_ref_target")
suite.Assert().Contains(err.Error(), "pipeline_pullrequest_target")
}

func (suite *PipelineSuite) TestPipelineWithMissingTargetType() {
payload := []byte(`{
"type": "pipeline",
"uuid": "{a1b2c3d4-e5f6-7890-abcd-ef1234567890}",
"build_number": 1,
"state": {
"type": "pipeline_state_in_progress",
"name": "IN_PROGRESS"
},
"target": {
"ref_type": "branch",
"ref_name": "develop"
},
"created_on": "2024-01-15T10:30:00.000000+00:00",
"duration_in_seconds": 0,
"creator": {
"type": "user",
"display_name": "Test User"
},
"repository": {
"type": "repository",
"name": "test-repo",
"full_name": "workspace/test-repo"
},
"links": {}
}`)
var p pipeline.Pipeline
err := json.Unmarshal(payload, &p)
suite.Require().Error(err)
suite.Assert().Contains(err.Error(), "Argument")
suite.Assert().Contains(err.Error(), "missing")
}
26 changes: 19 additions & 7 deletions cmd/pipeline/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,28 @@ var targetRegistry = core.TypeRegistry{}
func UnmarshalTarget(payload []byte) (Target, error) {
target, err := targetRegistry.UnmarshalJSON(payload)
if err != nil {
if strings.HasPrefix(err.Error(), "Missing JSON Property") {
errStr := err.Error()

if strings.HasPrefix(errStr, "Missing JSON Property") {
return nil, errors.JSONUnmarshalError.Wrap(errors.ArgumentMissing.With("type"))
}
if strings.HasPrefix(err.Error(), "Unsupported Type") {
keys := make([]string, 0, len(targetRegistry))
for key := range targetRegistry {
keys = append(keys, key)
}
return nil, errors.JSONUnmarshalError.Wrap(errors.InvalidType.With(strings.TrimSuffix(strings.TrimPrefix(err.Error(), `Unsupported Type "`), `"`), strings.Join(keys, ", ")))

// Check for "Unsupported Type" error and extract the type name
if strings.HasPrefix(errStr, "Unsupported Type ") {
// Extract the type name from the error message: 'Unsupported Type "typename"'
const prefix = "Unsupported Type "
remainder := errStr[len(prefix):]
// Remove surrounding quotes
typeName := strings.Trim(remainder, `"`)

// Get list of supported types
supportedTypes := targetRegistry.SupportedTypes()

return nil, errors.JSONUnmarshalError.Wrap(
errors.InvalidType.With(typeName, strings.Join(supportedTypes, ", ")),
)
}

return nil, err
}
return target.(Target), nil
Expand Down