From 55806a52a071e27bfa2b912e92af6bcd824a4a91 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Feb 2026 00:08:41 +0000 Subject: [PATCH 1/2] Initial plan From a655e64b6f32f9c27bc819cda8f393c798337cf2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Feb 2026 00:14:47 +0000 Subject: [PATCH 2/2] Fix inconsistent error handling in UnmarshalTarget and add tests Co-authored-by: gildas <56485+gildas@users.noreply.github.com> --- cmd/pipeline/pipeline_test.go | 69 +++++++++++++++++++++++++++++++++++ cmd/pipeline/target.go | 26 +++++++++---- 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/cmd/pipeline/pipeline_test.go b/cmd/pipeline/pipeline_test.go index aeb7fb7..8bdaa41 100644 --- a/cmd/pipeline/pipeline_test.go +++ b/cmd/pipeline/pipeline_test.go @@ -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") +} diff --git a/cmd/pipeline/target.go b/cmd/pipeline/target.go index 7829540..83a9ba3 100644 --- a/cmd/pipeline/target.go +++ b/cmd/pipeline/target.go @@ -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