From c4bec4a19bca6c976705e4ac24c1cade896fe1ba Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 17 Sep 2019 13:54:39 +1200 Subject: [PATCH 01/16] [BREAKING CHANGE] Clean up CodePipeline Job Implementation * Incorrectly used "CodePipelineEvent" --- events/codepipeline_job.go | 4 ++-- .../{codepipeline_test.go => codepipeline_job_test.go} | 10 +++++----- ...depipline-event.json => codepipline-job-event.json} | 0 3 files changed, 7 insertions(+), 7 deletions(-) rename events/{codepipeline_test.go => codepipeline_job_test.go} (70%) rename events/testdata/{codepipline-event.json => codepipline-job-event.json} (100%) diff --git a/events/codepipeline_job.go b/events/codepipeline_job.go index 68f96344..dd3b8236 100644 --- a/events/codepipeline_job.go +++ b/events/codepipeline_job.go @@ -2,8 +2,8 @@ package events -// CodePipelineEvent contains data from an event sent from AWS Codepipeline -type CodePipelineEvent struct { +// CodePipelineJobEvent contains data from an event sent from AWS CodePipeline +type CodePipelineJobEvent struct { CodePipelineJob CodePipelineJob `json:"CodePipeline.job"` } diff --git a/events/codepipeline_test.go b/events/codepipeline_job_test.go similarity index 70% rename from events/codepipeline_test.go rename to events/codepipeline_job_test.go index cf7dfed4..8aa8eafd 100644 --- a/events/codepipeline_test.go +++ b/events/codepipeline_job_test.go @@ -10,16 +10,16 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCodePipeLineEventMarshaling(t *testing.T) { +func TestCodePipeLineJobEventMarshaling(t *testing.T) { // read json from file - inputJSON, err := ioutil.ReadFile("./testdata/codepipline-event.json") + inputJSON, err := ioutil.ReadFile("./testdata/codepipline-job-event.json") if err != nil { t.Errorf("could not open test file. details: %v", err) } // de-serialize into CognitoEvent - var inputEvent CodePipelineEvent + var inputEvent CodePipelineJobEvent if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { t.Errorf("could not unmarshal event. details: %v", err) } @@ -33,6 +33,6 @@ func TestCodePipeLineEventMarshaling(t *testing.T) { assert.JSONEq(t, string(inputJSON), string(outputJSON)) } -func TestCodePipelineEventMarshalingMalformedJson(t *testing.T) { - test.TestMalformedJson(t, CodePipelineEvent{}) +func TestCodePipelineJobEventMarshalingMalformedJson(t *testing.T) { + test.TestMalformedJson(t, CodePipelineJobEvent{}) } diff --git a/events/testdata/codepipline-event.json b/events/testdata/codepipline-job-event.json similarity index 100% rename from events/testdata/codepipline-event.json rename to events/testdata/codepipline-job-event.json From fddd182498db4b1cbb49e050acd810b9f673dc74 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 29 Oct 2019 12:25:23 +1300 Subject: [PATCH 02/16] Fix original typo missed when moving to job --- events/codepipeline_job_test.go | 2 +- .../{codepipline-job-event.json => codepipeline-job-event.json} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename events/testdata/{codepipline-job-event.json => codepipeline-job-event.json} (100%) diff --git a/events/codepipeline_job_test.go b/events/codepipeline_job_test.go index 8aa8eafd..b99c4938 100644 --- a/events/codepipeline_job_test.go +++ b/events/codepipeline_job_test.go @@ -13,7 +13,7 @@ import ( func TestCodePipeLineJobEventMarshaling(t *testing.T) { // read json from file - inputJSON, err := ioutil.ReadFile("./testdata/codepipline-job-event.json") + inputJSON, err := ioutil.ReadFile("./testdata/codepipeline-job-event.json") if err != nil { t.Errorf("could not open test file. details: %v", err) } diff --git a/events/testdata/codepipline-job-event.json b/events/testdata/codepipeline-job-event.json similarity index 100% rename from events/testdata/codepipline-job-event.json rename to events/testdata/codepipeline-job-event.json From 737f84d38b7779fc9145341eaef03976af90405e Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Wed, 2 Oct 2019 17:50:34 +1300 Subject: [PATCH 03/16] Implement CodePipeline Events * Reference docs: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#codepipeline_event_type * Test json from Reference Docs * Fix data issues from live testing vs real events --- events/codepipeline.go | 106 ++++++++++++++++++ events/codepipeline_test.go | 99 ++++++++++++++++ ...e-action-execution-stage-change-event.json | 27 +++++ ...pipeline-execution-stage-change-event.json | 18 +++ ...pipeline-execution-state-change-event.json | 18 +++ 5 files changed, 268 insertions(+) create mode 100644 events/codepipeline.go create mode 100644 events/codepipeline_test.go create mode 100644 events/testdata/codepipeline-action-execution-stage-change-event.json create mode 100644 events/testdata/codepipeline-execution-stage-change-event.json create mode 100644 events/testdata/codepipeline-execution-state-change-event.json diff --git a/events/codepipeline.go b/events/codepipeline.go new file mode 100644 index 00000000..9da639d3 --- /dev/null +++ b/events/codepipeline.go @@ -0,0 +1,106 @@ +package events + +import ( + "time" +) + +const ( + CodePipelineEventSource = "aws.codepipeline" + CodePipelineExecutionEventDetailType = "CodePipeline Pipeline Execution State Change" + CodePipelineActionEventDetailType = "CodePipeline Pipeline Action State Change" + CodePipelineStageEventDetailType = "CodePipeline Pipeline Stage State Change" +) + +type CodePipelineStageState string + +const ( + CodePipelineStageStateStarted CodePipelineStageState = "STARTED" + CodePipelineStageStateSucceeded = "SUCCEEDED" + CodePipelineStageStateResumed = "RESUMED" + CodePipelineStageStateFailed = "FAILED" + CodePipelineStageStateCanceled = "CANCELED" +) + +type CodePipelineState string + +const ( + CodePipelineStateStarted CodePipelineState = "STARTED" + CodePipelineStateSucceeded = "SUCCEEDED" + CodePipelineStateResumed = "RESUMED" + CodePipelineStateFailed = "FAILED" + CodePipelineStateCanceled = "CANCELED" + CodePipelineStateSuperseded = "SUPERSEDED" +) + +type CodePipelineActionState string + +const ( + CodePipelineActionStateStarted CodePipelineActionState = "STARTED" + CodePipelineActionStateSucceeded = "SUCCEEDED" + CodePipelineActionStateFailed = "FAILED" + CodePipelineActionStateCanceled = "CANCELED" +) + +// CodePipelineEvent is documented at: +// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#codepipeline_event_type +type CodePipelineEvent struct { + // Version is the version of the event's schema. + Version string `json:"version"` + + // ID is the GUID of this event. + ID string `json:"id"` + + // DetailType informs the schema of the Detail field. For deployment state-change + // events, the value should be equal to CodePipelineDeploymentEventDetailType. + // For instance state-change events, the value should be equal to + // CodePipelineInstanceEventDetailType. + DetailType string `json:"detail-type"` + + // Source should be equal to CodePipelineEventSource. + Source string `json:"source"` + + // AccountID is the id of the AWS account from which the event originated. + AccountID string `json:"account"` + + // Time is the event's timestamp. + Time time.Time `json:"time"` + + // Region is the AWS region from which the event originated. + Region string `json:"region"` + + // Resources is a list of ARNs of CodePipeline applications and deployment + // groups that this event pertains to. + Resources []string `json:"resources"` + + // Detail contains information specific to a deployment event. + Detail CodePipelineEventDetail `json:"detail"` +} + +type CodePipelineEventDetail struct { + Pipeline string `json:"pipeline"` + + // From live testing this is always int64 not string as documented + Version int64 `json:"version"` + + ExecutionId string `json:"execution-id"` + + Stage string `json:"stage"` + + Action string `json:"action"` + + State CodePipelineState `json:"state"` + + Region string `json:"region"` + + Type CodePipelineEventDetailType `json:"type"` +} + +type CodePipelineEventDetailType struct { + Owner string `json:"owner"` + + Category string `json:"category"` + + Provider string `json:"provider"` + + Version string `json:"version"` +} diff --git a/events/codepipeline_test.go b/events/codepipeline_test.go new file mode 100644 index 00000000..ed111100 --- /dev/null +++ b/events/codepipeline_test.go @@ -0,0 +1,99 @@ +package events + +import ( + "encoding/json" + "github.com/stretchr/testify/require" + "io/ioutil" + "testing" + "time" +) + +func TestUnmarshalCodePipelineEvent(t *testing.T) { + tests := []struct { + input string + expect CodePipelineEvent + }{ + { + input: "testdata/codepipeline-action-execution-stage-change-event.json", + expect: CodePipelineEvent{ + 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: 1, + 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", + }, + }, + }, + }, + { + input: "testdata/codepipeline-execution-stage-change-event.json", + expect: CodePipelineEvent{ + Version: "0", + ID: "CWE-event-id", + DetailType: "CodePipeline Stage 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: 1, + ExecutionId: "01234567-0123-0123-0123-012345678901", + State: "STARTED", + }, + }, + }, + { + input: "testdata/codepipeline-execution-state-change-event.json", + expect: CodePipelineEvent{ + Version: "0", + ID: "CWE-event-id", + DetailType: "CodePipeline Pipeline 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: 1, + ExecutionId: "01234567-0123-0123-0123-012345678901", + State: "STARTED", + }, + }, + }, + } + + for _, testcase := range tests { + data, err := ioutil.ReadFile(testcase.input) + require.NoError(t, err) + + var actual CodePipelineEvent + require.NoError(t, json.Unmarshal(data, &actual)) + + require.Equal(t, testcase.expect, actual) + } +} diff --git a/events/testdata/codepipeline-action-execution-stage-change-event.json b/events/testdata/codepipeline-action-execution-stage-change-event.json new file mode 100644 index 00000000..9d41990f --- /dev/null +++ b/events/testdata/codepipeline-action-execution-stage-change-event.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": 1, + "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" + } + } +} \ No newline at end of file diff --git a/events/testdata/codepipeline-execution-stage-change-event.json b/events/testdata/codepipeline-execution-stage-change-event.json new file mode 100644 index 00000000..6a76b4c3 --- /dev/null +++ b/events/testdata/codepipeline-execution-stage-change-event.json @@ -0,0 +1,18 @@ +{ + "version": "0", + "id": "CWE-event-id", + "detail-type": "CodePipeline Stage 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": 1, + "state": "STARTED", + "execution-id": "01234567-0123-0123-0123-012345678901" + } +} \ No newline at end of file diff --git a/events/testdata/codepipeline-execution-state-change-event.json b/events/testdata/codepipeline-execution-state-change-event.json new file mode 100644 index 00000000..32c2bcb4 --- /dev/null +++ b/events/testdata/codepipeline-execution-state-change-event.json @@ -0,0 +1,18 @@ +{ + "version": "0", + "id": "CWE-event-id", + "detail-type": "CodePipeline Pipeline 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": 1, + "state": "STARTED", + "execution-id": "01234567-0123-0123-0123-012345678901" + } +} \ No newline at end of file From a075ecbedf8fc47672d1d3b68b342dadfbebfe47 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Wed, 30 Oct 2019 14:53:33 +1300 Subject: [PATCH 04/16] Fix up detail types --- events/codepipeline.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/events/codepipeline.go b/events/codepipeline.go index 9da639d3..d3174d86 100644 --- a/events/codepipeline.go +++ b/events/codepipeline.go @@ -7,8 +7,8 @@ import ( const ( CodePipelineEventSource = "aws.codepipeline" CodePipelineExecutionEventDetailType = "CodePipeline Pipeline Execution State Change" - CodePipelineActionEventDetailType = "CodePipeline Pipeline Action State Change" - CodePipelineStageEventDetailType = "CodePipeline Pipeline Stage State Change" + CodePipelineActionEventDetailType = "CodePipeline Action Execution State Change" + CodePipelineStageEventDetailType = "CodePipeline Stage Execution State Change" ) type CodePipelineStageState string From ba8d4131ff69fd3b0f58bd9795326bd265a5d417 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Wed, 30 Oct 2019 15:31:42 +1300 Subject: [PATCH 05/16] Allow for floats --- events/codepipeline.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/codepipeline.go b/events/codepipeline.go index d3174d86..7646f8b9 100644 --- a/events/codepipeline.go +++ b/events/codepipeline.go @@ -80,7 +80,7 @@ type CodePipelineEventDetail struct { Pipeline string `json:"pipeline"` // From live testing this is always int64 not string as documented - Version int64 `json:"version"` + Version float64 `json:"version"` ExecutionId string `json:"execution-id"` From 30d8e3d77e4462336e87c1ccb10207f5ca9f0445 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 20 Jul 2021 11:51:13 +1200 Subject: [PATCH 06/16] Add "CloudWatch" to maintain backwards compatability --- events/codepipeline.go | 108 ++---------------------------- events/codepipeline_cloudwatch.go | 106 +++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 104 deletions(-) create mode 100644 events/codepipeline_cloudwatch.go diff --git a/events/codepipeline.go b/events/codepipeline.go index 7646f8b9..239020bc 100644 --- a/events/codepipeline.go +++ b/events/codepipeline.go @@ -1,106 +1,6 @@ package events -import ( - "time" -) - -const ( - CodePipelineEventSource = "aws.codepipeline" - CodePipelineExecutionEventDetailType = "CodePipeline Pipeline Execution State Change" - CodePipelineActionEventDetailType = "CodePipeline Action Execution State Change" - CodePipelineStageEventDetailType = "CodePipeline Stage Execution State Change" -) - -type CodePipelineStageState string - -const ( - CodePipelineStageStateStarted CodePipelineStageState = "STARTED" - CodePipelineStageStateSucceeded = "SUCCEEDED" - CodePipelineStageStateResumed = "RESUMED" - CodePipelineStageStateFailed = "FAILED" - CodePipelineStageStateCanceled = "CANCELED" -) - -type CodePipelineState string - -const ( - CodePipelineStateStarted CodePipelineState = "STARTED" - CodePipelineStateSucceeded = "SUCCEEDED" - CodePipelineStateResumed = "RESUMED" - CodePipelineStateFailed = "FAILED" - CodePipelineStateCanceled = "CANCELED" - CodePipelineStateSuperseded = "SUPERSEDED" -) - -type CodePipelineActionState string - -const ( - CodePipelineActionStateStarted CodePipelineActionState = "STARTED" - CodePipelineActionStateSucceeded = "SUCCEEDED" - CodePipelineActionStateFailed = "FAILED" - CodePipelineActionStateCanceled = "CANCELED" -) - -// CodePipelineEvent is documented at: -// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#codepipeline_event_type -type CodePipelineEvent struct { - // Version is the version of the event's schema. - Version string `json:"version"` - - // ID is the GUID of this event. - ID string `json:"id"` - - // DetailType informs the schema of the Detail field. For deployment state-change - // events, the value should be equal to CodePipelineDeploymentEventDetailType. - // For instance state-change events, the value should be equal to - // CodePipelineInstanceEventDetailType. - DetailType string `json:"detail-type"` - - // Source should be equal to CodePipelineEventSource. - Source string `json:"source"` - - // AccountID is the id of the AWS account from which the event originated. - AccountID string `json:"account"` - - // Time is the event's timestamp. - Time time.Time `json:"time"` - - // Region is the AWS region from which the event originated. - Region string `json:"region"` - - // Resources is a list of ARNs of CodePipeline applications and deployment - // groups that this event pertains to. - Resources []string `json:"resources"` - - // Detail contains information specific to a deployment event. - Detail CodePipelineEventDetail `json:"detail"` -} - -type CodePipelineEventDetail struct { - Pipeline string `json:"pipeline"` - - // From live testing this is always int64 not string as documented - Version float64 `json:"version"` - - ExecutionId string `json:"execution-id"` - - Stage string `json:"stage"` - - Action string `json:"action"` - - State CodePipelineState `json:"state"` - - Region string `json:"region"` - - Type CodePipelineEventDetailType `json:"type"` -} - -type CodePipelineEventDetailType struct { - Owner string `json:"owner"` - - Category string `json:"category"` - - Provider string `json:"provider"` - - Version string `json:"version"` -} +// CodePipelineJob has been incorrectly assigned as CodePipelineEvent +// - https://github.com/aws/aws-lambda-go/issues/244 +// This maintains backwards compatability until a v2 release +type CodePipelineEvent = CodePipelineJobEvent \ No newline at end of file diff --git a/events/codepipeline_cloudwatch.go b/events/codepipeline_cloudwatch.go new file mode 100644 index 00000000..382062ab --- /dev/null +++ b/events/codepipeline_cloudwatch.go @@ -0,0 +1,106 @@ +package events + +import ( + "time" +) + +const ( + CodePipelineEventSource = "aws.codepipeline" + CodePipelineExecutionEventDetailType = "CodePipeline Pipeline Execution State Change" + CodePipelineActionEventDetailType = "CodePipeline Action Execution State Change" + CodePipelineStageEventDetailType = "CodePipeline Stage Execution State Change" +) + +type CodePipelineStageState string + +const ( + CodePipelineStageStateStarted CodePipelineStageState = "STARTED" + CodePipelineStageStateSucceeded = "SUCCEEDED" + CodePipelineStageStateResumed = "RESUMED" + CodePipelineStageStateFailed = "FAILED" + CodePipelineStageStateCanceled = "CANCELED" +) + +type CodePipelineState string + +const ( + CodePipelineStateStarted CodePipelineState = "STARTED" + CodePipelineStateSucceeded = "SUCCEEDED" + CodePipelineStateResumed = "RESUMED" + CodePipelineStateFailed = "FAILED" + CodePipelineStateCanceled = "CANCELED" + CodePipelineStateSuperseded = "SUPERSEDED" +) + +type CodePipelineActionState string + +const ( + CodePipelineActionStateStarted CodePipelineActionState = "STARTED" + CodePipelineActionStateSucceeded = "SUCCEEDED" + CodePipelineActionStateFailed = "FAILED" + CodePipelineActionStateCanceled = "CANCELED" +) + +// CodePipelineEvent is documented at: +// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#codepipeline_event_type +type CodePipelineCloudWatchEvent struct { + // Version is the version of the event's schema. + Version string `json:"version"` + + // ID is the GUID of this event. + ID string `json:"id"` + + // DetailType informs the schema of the Detail field. For deployment state-change + // events, the value should be equal to CodePipelineDeploymentEventDetailType. + // For instance state-change events, the value should be equal to + // CodePipelineInstanceEventDetailType. + DetailType string `json:"detail-type"` + + // Source should be equal to CodePipelineEventSource. + Source string `json:"source"` + + // AccountID is the id of the AWS account from which the event originated. + AccountID string `json:"account"` + + // Time is the event's timestamp. + Time time.Time `json:"time"` + + // Region is the AWS region from which the event originated. + Region string `json:"region"` + + // Resources is a list of ARNs of CodePipeline applications and deployment + // groups that this event pertains to. + Resources []string `json:"resources"` + + // Detail contains information specific to a deployment event. + Detail CodePipelineEventDetail `json:"detail"` +} + +type CodePipelineEventDetail struct { + Pipeline string `json:"pipeline"` + + // From live testing this is always int64 not string as documented + Version float64 `json:"version"` + + ExecutionId string `json:"execution-id"` + + Stage string `json:"stage"` + + Action string `json:"action"` + + State CodePipelineState `json:"state"` + + Region string `json:"region"` + + Type CodePipelineEventDetailType `json:"type"` +} + +type CodePipelineEventDetailType struct { + Owner string `json:"owner"` + + Category string `json:"category"` + + Provider string `json:"provider"` + + Version string `json:"version"` +} From a482ebdf836890739c3d000f41fc37c3044f67b5 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 20 Jul 2021 11:53:22 +1200 Subject: [PATCH 07/16] go fmt --- events/codepipeline.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/codepipeline.go b/events/codepipeline.go index 239020bc..2134cbb5 100644 --- a/events/codepipeline.go +++ b/events/codepipeline.go @@ -3,4 +3,4 @@ package events // CodePipelineJob has been incorrectly assigned as CodePipelineEvent // - https://github.com/aws/aws-lambda-go/issues/244 // This maintains backwards compatability until a v2 release -type CodePipelineEvent = CodePipelineJobEvent \ No newline at end of file +type CodePipelineEvent = CodePipelineJobEvent From 2ea403cbc1facc421d77dd89bb5d5f371130039b Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 20 Jul 2021 12:03:47 +1200 Subject: [PATCH 08/16] Change tests to CloudWatch rename also --- events/codepipeline_cloudwatch_test.go | 99 ++++++++++++++++++++++ events/codepipeline_test.go | 111 ++++++------------------- 2 files changed, 124 insertions(+), 86 deletions(-) create mode 100644 events/codepipeline_cloudwatch_test.go diff --git a/events/codepipeline_cloudwatch_test.go b/events/codepipeline_cloudwatch_test.go new file mode 100644 index 00000000..265d552e --- /dev/null +++ b/events/codepipeline_cloudwatch_test.go @@ -0,0 +1,99 @@ +package events + +import ( + "encoding/json" + "github.com/stretchr/testify/require" + "io/ioutil" + "testing" + "time" +) + +func TestUnmarshalCodePipelineEvent(t *testing.T) { + tests := []struct { + input string + expect CodePipelineCloudWatchEvent + }{ + { + input: "testdata/codepipeline-action-execution-stage-change-event.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: 1, + 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", + }, + }, + }, + }, + { + input: "testdata/codepipeline-execution-stage-change-event.json", + expect: CodePipelineCloudWatchEvent{ + Version: "0", + ID: "CWE-event-id", + DetailType: "CodePipeline Stage 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: 1, + ExecutionId: "01234567-0123-0123-0123-012345678901", + State: "STARTED", + }, + }, + }, + { + input: "testdata/codepipeline-execution-state-change-event.json", + expect: CodePipelineCloudWatchEvent{ + Version: "0", + ID: "CWE-event-id", + DetailType: "CodePipeline Pipeline 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: 1, + ExecutionId: "01234567-0123-0123-0123-012345678901", + State: "STARTED", + }, + }, + }, + } + + for _, testcase := range tests { + data, err := ioutil.ReadFile(testcase.input) + require.NoError(t, err) + + var actual CodePipelineEvent + require.NoError(t, json.Unmarshal(data, &actual)) + + require.Equal(t, testcase.expect, actual) + } +} diff --git a/events/codepipeline_test.go b/events/codepipeline_test.go index ed111100..77795010 100644 --- a/events/codepipeline_test.go +++ b/events/codepipeline_test.go @@ -1,99 +1,38 @@ +// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. package events import ( "encoding/json" - "github.com/stretchr/testify/require" "io/ioutil" "testing" - "time" + + "github.com/aws/aws-lambda-go/events/test" + "github.com/stretchr/testify/assert" ) -func TestUnmarshalCodePipelineEvent(t *testing.T) { - tests := []struct { - input string - expect CodePipelineEvent - }{ - { - input: "testdata/codepipeline-action-execution-stage-change-event.json", - expect: CodePipelineEvent{ - 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: 1, - 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", - }, - }, - }, - }, - { - input: "testdata/codepipeline-execution-stage-change-event.json", - expect: CodePipelineEvent{ - Version: "0", - ID: "CWE-event-id", - DetailType: "CodePipeline Stage 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: 1, - ExecutionId: "01234567-0123-0123-0123-012345678901", - State: "STARTED", - }, - }, - }, - { - input: "testdata/codepipeline-execution-state-change-event.json", - expect: CodePipelineEvent{ - Version: "0", - ID: "CWE-event-id", - DetailType: "CodePipeline Pipeline 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: 1, - ExecutionId: "01234567-0123-0123-0123-012345678901", - State: "STARTED", - }, - }, - }, - } +func TestCodePipeLineJobEventMarshaling(t *testing.T) { - for _, testcase := range tests { - data, err := ioutil.ReadFile(testcase.input) - require.NoError(t, err) + // read json from file + inputJSON, err := ioutil.ReadFile("./testdata/codepipeline-job-event.json") + if err != nil { + t.Errorf("could not open test file. details: %v", err) + } - var actual CodePipelineEvent - require.NoError(t, json.Unmarshal(data, &actual)) + // de-serialize into CognitoEvent + var inputEvent CodePipelineEvent + if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { + t.Errorf("could not unmarshal event. details: %v", err) + } - require.Equal(t, testcase.expect, actual) + // serialize to json + outputJSON, err := json.Marshal(inputEvent) + if err != nil { + t.Errorf("could not marshal event. details: %v", err) } + + assert.JSONEq(t, string(inputJSON), string(outputJSON)) +} + +func TestCodePipelineJobEventMarshalingMalformedJson(t *testing.T) { + test.TestMalformedJson(t, CodePipelineEvent{}) } From 8265443095c600d57e7846c890b948ac49d74693 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 20 Jul 2021 12:06:49 +1200 Subject: [PATCH 09/16] Remove Job --- events/codepipeline_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/events/codepipeline_test.go b/events/codepipeline_test.go index 77795010..6a31a2f7 100644 --- a/events/codepipeline_test.go +++ b/events/codepipeline_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCodePipeLineJobEventMarshaling(t *testing.T) { +func TestCodePipeLineEventMarshaling(t *testing.T) { // read json from file inputJSON, err := ioutil.ReadFile("./testdata/codepipeline-job-event.json") @@ -33,6 +33,6 @@ func TestCodePipeLineJobEventMarshaling(t *testing.T) { assert.JSONEq(t, string(inputJSON), string(outputJSON)) } -func TestCodePipelineJobEventMarshalingMalformedJson(t *testing.T) { +func TestCodePipelineEventMarshalingMalformedJson(t *testing.T) { test.TestMalformedJson(t, CodePipelineEvent{}) } From ac59d3064b90c07b72c0171a2cf13513f41895d3 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 20 Jul 2021 12:12:25 +1200 Subject: [PATCH 10/16] Fix explicit type constants --- events/codepipeline_cloudwatch.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/events/codepipeline_cloudwatch.go b/events/codepipeline_cloudwatch.go index 382062ab..3b7e1585 100644 --- a/events/codepipeline_cloudwatch.go +++ b/events/codepipeline_cloudwatch.go @@ -15,30 +15,30 @@ type CodePipelineStageState string const ( CodePipelineStageStateStarted CodePipelineStageState = "STARTED" - CodePipelineStageStateSucceeded = "SUCCEEDED" - CodePipelineStageStateResumed = "RESUMED" - CodePipelineStageStateFailed = "FAILED" - CodePipelineStageStateCanceled = "CANCELED" + CodePipelineStageStateSucceeded CodePipelineStageState = "SUCCEEDED" + CodePipelineStageStateResumed CodePipelineStageState = "RESUMED" + CodePipelineStageStateFailed CodePipelineStageState = "FAILED" + CodePipelineStageStateCanceled CodePipelineStageState = "CANCELED" ) type CodePipelineState string const ( CodePipelineStateStarted CodePipelineState = "STARTED" - CodePipelineStateSucceeded = "SUCCEEDED" - CodePipelineStateResumed = "RESUMED" - CodePipelineStateFailed = "FAILED" - CodePipelineStateCanceled = "CANCELED" - CodePipelineStateSuperseded = "SUPERSEDED" + CodePipelineStateSucceeded CodePipelineState = "SUCCEEDED" + CodePipelineStateResumed CodePipelineState = "RESUMED" + CodePipelineStateFailed CodePipelineState = "FAILED" + CodePipelineStateCanceled CodePipelineState = "CANCELED" + CodePipelineStateSuperseded CodePipelineState = "SUPERSEDED" ) type CodePipelineActionState string const ( CodePipelineActionStateStarted CodePipelineActionState = "STARTED" - CodePipelineActionStateSucceeded = "SUCCEEDED" - CodePipelineActionStateFailed = "FAILED" - CodePipelineActionStateCanceled = "CANCELED" + CodePipelineActionStateSucceeded CodePipelineActionState = "SUCCEEDED" + CodePipelineActionStateFailed CodePipelineActionState = "FAILED" + CodePipelineActionStateCanceled CodePipelineActionState = "CANCELED" ) // CodePipelineEvent is documented at: From 4c5a353a719f1276fc6084dd988e98d39144774b Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 20 Jul 2021 12:14:32 +1200 Subject: [PATCH 11/16] Fix merge issue --- events/codepipeline_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/codepipeline_test.go b/events/codepipeline_test.go index 6a31a2f7..063ea0b3 100644 --- a/events/codepipeline_test.go +++ b/events/codepipeline_test.go @@ -18,7 +18,7 @@ func TestCodePipeLineEventMarshaling(t *testing.T) { t.Errorf("could not open test file. details: %v", err) } - // de-serialize into CognitoEvent + // de-serialize into CodePipelineEvent var inputEvent CodePipelineEvent if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { t.Errorf("could not unmarshal event. details: %v", err) From f74aa0f11a55c4628c9bc44ba70929ce7900e3b7 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Tue, 20 Jul 2021 12:30:20 +1200 Subject: [PATCH 12/16] Add missed CloudWatch change --- events/codepipeline_cloudwatch_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/codepipeline_cloudwatch_test.go b/events/codepipeline_cloudwatch_test.go index 265d552e..7bd98c5c 100644 --- a/events/codepipeline_cloudwatch_test.go +++ b/events/codepipeline_cloudwatch_test.go @@ -91,7 +91,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) { data, err := ioutil.ReadFile(testcase.input) require.NoError(t, err) - var actual CodePipelineEvent + var actual CodePipelineCloudWatchEvent require.NoError(t, json.Unmarshal(data, &actual)) require.Equal(t, testcase.expect, actual) From 43c3a027bb2bb23e589e1b3f868588fcebd376ad Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Wed, 11 Aug 2021 12:45:55 +1200 Subject: [PATCH 13/16] Codereview fixes --- events/codepipeline_cloudwatch.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/events/codepipeline_cloudwatch.go b/events/codepipeline_cloudwatch.go index 3b7e1585..e51fea12 100644 --- a/events/codepipeline_cloudwatch.go +++ b/events/codepipeline_cloudwatch.go @@ -80,9 +80,9 @@ type CodePipelineEventDetail struct { Pipeline string `json:"pipeline"` // From live testing this is always int64 not string as documented - Version float64 `json:"version"` + Version int64 `json:"version"` - ExecutionId string `json:"execution-id"` + ExecutionID string `json:"execution-id"` Stage string `json:"stage"` @@ -102,5 +102,6 @@ type CodePipelineEventDetailType struct { Provider string `json:"provider"` - Version string `json:"version"` + // From published EventBridge schema registry this is always int64 not string as documented + Version int64 `json:"version"` } From 46581aca112a94a24d02f176f81828fb6a0e8704 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Wed, 11 Aug 2021 12:47:53 +1200 Subject: [PATCH 14/16] Change tests to match the ExecutionId -> ID change --- events/codepipeline_cloudwatch_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/events/codepipeline_cloudwatch_test.go b/events/codepipeline_cloudwatch_test.go index 7bd98c5c..8572ba07 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, - ExecutionId: "01234567-0123-0123-0123-012345678901", + ExecutionID: "01234567-0123-0123-0123-012345678901", Stage: "Prod", Action: "myAction", State: "STARTED", @@ -59,7 +59,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) { Detail: CodePipelineEventDetail{ Pipeline: "myPipeline", Version: 1, - ExecutionId: "01234567-0123-0123-0123-012345678901", + ExecutionID: "01234567-0123-0123-0123-012345678901", State: "STARTED", }, }, @@ -80,7 +80,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) { Detail: CodePipelineEventDetail{ Pipeline: "myPipeline", Version: 1, - ExecutionId: "01234567-0123-0123-0123-012345678901", + ExecutionID: "01234567-0123-0123-0123-012345678901", State: "STARTED", }, }, From 35e25d2830c37006a47e7b6b1b56c3a8e57682e5 Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Wed, 11 Aug 2021 12:51:58 +1200 Subject: [PATCH 15/16] Again missed the test change to match with int64 --- events/codepipeline_cloudwatch_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/codepipeline_cloudwatch_test.go b/events/codepipeline_cloudwatch_test.go index 8572ba07..e927e705 100644 --- a/events/codepipeline_cloudwatch_test.go +++ b/events/codepipeline_cloudwatch_test.go @@ -38,7 +38,7 @@ func TestUnmarshalCodePipelineEvent(t *testing.T) { Owner: "AWS", Category: "Deploy", Provider: "CodeDeploy", - Version: "1", + Version: 1, }, }, }, From 00c1a76e9b8b6fab8c99ee627b57673a9eab69bf Mon Sep 17 00:00:00 2001 From: Jesse Whitham Date: Wed, 11 Aug 2021 13:52:50 +1200 Subject: [PATCH 16/16] String to Int change to match code change --- .../codepipeline-action-execution-stage-change-event.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/testdata/codepipeline-action-execution-stage-change-event.json b/events/testdata/codepipeline-action-execution-stage-change-event.json index 9d41990f..826fa77a 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