diff --git a/events/codepipeline_cloudwatch.go b/events/codepipeline_cloudwatch.go index 41340c4e..24d4a342 100644 --- a/events/codepipeline_cloudwatch.go +++ b/events/codepipeline_cloudwatch.go @@ -1,6 +1,7 @@ package events import ( + "encoding/json" "time" ) @@ -79,8 +80,9 @@ type CodePipelineEventBridgeEvent = CodePipelineCloudWatchEvent type CodePipelineEventDetail struct { Pipeline string `json:"pipeline"` - // From live testing this is always int64 not string as documented - Version int64 `json:"version"` + // Version is the pipeline version number. AWS may send this as an integer (e.g. 1) + // or a float (e.g. 2.0). Use Version.Int64() or Version.Float64() to extract the value. + Version json.Number `json:"version"` ExecutionID string `json:"execution-id"` @@ -104,8 +106,8 @@ type CodePipelineEventDetailType struct { Provider string `json:"provider"` - // From published EventBridge schema registry this is always int64 not string as documented - Version int64 `json:"version"` + // Version is a string per the AWS EventBridge schema registry and confirmed by the service team. + Version string `json:"version"` } type CodePipelineEventDetailExecutionResult struct { diff --git a/events/codepipeline_cloudwatch_test.go b/events/codepipeline_cloudwatch_test.go index 13321980..fbd7002c 100644 --- a/events/codepipeline_cloudwatch_test.go +++ b/events/codepipeline_cloudwatch_test.go @@ -29,7 +29,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) { }, Detail: CodePipelineEventDetail{ Pipeline: "myPipeline", - Version: 1, + Version: "1", ExecutionID: "01234567-0123-0123-0123-012345678901", Stage: "Prod", Action: "myAction", @@ -39,7 +39,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) { Owner: "AWS", Category: "Deploy", Provider: "CodeDeploy", - Version: 1, + Version: "1", }, }, }, @@ -59,7 +59,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) { }, Detail: CodePipelineEventDetail{ Pipeline: "myPipeline", - Version: 1, + Version: "1", ExecutionID: "01234567-0123-0123-0123-012345678901", State: "STARTED", }, @@ -80,12 +80,44 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) { }, Detail: CodePipelineEventDetail{ Pipeline: "myPipeline", - Version: 1, + Version: "1", ExecutionID: "01234567-0123-0123-0123-012345678901", State: "STARTED", }, }, }, + { + // AWS may send detail.version as a float (e.g. 2.0) rather than an integer. + // Regression test for https://github.com/aws/aws-lambda-go/issues/552 + input: "testdata/codepipeline-action-execution-stage-change-event-float-version.json", + expect: CodePipelineCloudWatchEvent{ + Version: "0", + ID: "CWE-event-id", + DetailType: "CodePipeline Action Execution State Change", + Source: "aws.codepipeline", + AccountID: "123456789012", + Time: time.Date(2017, 04, 22, 3, 31, 47, 0, time.UTC), + Region: "us-east-1", + Resources: []string{ + "arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline", + }, + Detail: CodePipelineEventDetail{ + Pipeline: "myPipeline", + Version: "2.0", + ExecutionID: "01234567-0123-0123-0123-012345678901", + Stage: "Prod", + Action: "myAction", + State: "STARTED", + Region: "us-west-2", + Type: CodePipelineEventDetailType{ + Owner: "AWS", + Category: "Deploy", + Provider: "CodeDeploy", + Version: "1", + }, + }, + }, + }, } for _, testcase := range tests { diff --git a/events/testdata/codepipeline-action-execution-stage-change-event-float-version.json b/events/testdata/codepipeline-action-execution-stage-change-event-float-version.json new file mode 100644 index 00000000..4a60fb65 --- /dev/null +++ b/events/testdata/codepipeline-action-execution-stage-change-event-float-version.json @@ -0,0 +1,27 @@ +{ + "version": "0", + "id": "CWE-event-id", + "detail-type": "CodePipeline Action Execution State Change", + "source": "aws.codepipeline", + "account": "123456789012", + "time": "2017-04-22T03:31:47Z", + "region": "us-east-1", + "resources": [ + "arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline" + ], + "detail": { + "pipeline": "myPipeline", + "version": 2.0, + "execution-id": "01234567-0123-0123-0123-012345678901", + "stage": "Prod", + "action": "myAction", + "state": "STARTED", + "region":"us-west-2", + "type": { + "owner": "AWS", + "category": "Deploy", + "provider": "CodeDeploy", + "version": "1" + } + } +} diff --git a/events/testdata/codepipeline-action-execution-stage-change-event.json b/events/testdata/codepipeline-action-execution-stage-change-event.json index 826fa77a..9d41990f 100644 --- a/events/testdata/codepipeline-action-execution-stage-change-event.json +++ b/events/testdata/codepipeline-action-execution-stage-change-event.json @@ -21,7 +21,7 @@ "owner": "AWS", "category": "Deploy", "provider": "CodeDeploy", - "version": 1 + "version": "1" } } } \ No newline at end of file