Skip to content

Implement CodePipelineEvent #247

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

Merged
merged 23 commits into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c4bec4a
[BREAKING CHANGE] Clean up CodePipeline Job Implementation
whithajess Sep 17, 2019
fddd182
Fix original typo missed when moving to job
whithajess Oct 28, 2019
737f84d
Implement CodePipeline Events
whithajess Oct 2, 2019
a075ecb
Fix up detail types
whithajess Oct 30, 2019
ba8d413
Allow for floats
whithajess Oct 30, 2019
c8c151e
Merge branch 'master' into feature/code-pipeline-events
whithajess Nov 14, 2019
f727954
Merge branch 'master' into feature/code-pipeline-events
whithajess Nov 25, 2019
1d5a7ac
Merge remote-tracking branch 'upstream/master' into feature/code-pipe…
whithajess Jul 19, 2021
30d8e3d
Add "CloudWatch" to maintain backwards compatability
whithajess Jul 19, 2021
a482ebd
go fmt
whithajess Jul 19, 2021
2ea403c
Change tests to CloudWatch rename also
whithajess Jul 20, 2021
8265443
Remove Job
whithajess Jul 20, 2021
ac59d30
Fix explicit type constants
whithajess Jul 20, 2021
4c5a353
Fix merge issue
whithajess Jul 20, 2021
f74aa0f
Add missed CloudWatch change
whithajess Jul 20, 2021
029afc2
Merge branch 'master' into feature/code-pipeline-events
bmoffatt Jul 26, 2021
43c3a02
Codereview fixes
whithajess Aug 11, 2021
46581ac
Change tests to match the ExecutionId -> ID change
whithajess Aug 11, 2021
35e25d2
Again missed the test change to match with int64
whithajess Aug 11, 2021
00c1a76
String to Int change to match code change
whithajess Aug 11, 2021
25e4786
Merge branch 'master' into feature/code-pipeline-events
whithajess Aug 11, 2021
22b1ea5
Merge branch 'master' into feature/code-pipeline-events
bmoffatt Sep 17, 2021
f08396f
Merge branch 'main' into feature/code-pipeline-events
bmoffatt Sep 17, 2021
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
106 changes: 106 additions & 0 deletions events/codepipeline.go
Original file line number Diff line number Diff line change
@@ -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 CodePipelineEvent struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest changing the name to CodePipelineCloudWatchEvent to disambiguate from CodePipelineJobEvent

// 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"`
}
4 changes: 2 additions & 2 deletions events/codepipeline_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
38 changes: 38 additions & 0 deletions events/codepipeline_job_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
package events

import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/aws/aws-lambda-go/events/test"
"github.com/stretchr/testify/assert"
)

func TestCodePipeLineJobEventMarshaling(t *testing.T) {

// 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)
}

// de-serialize into CognitoEvent
var inputEvent CodePipelineJobEvent
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}

// 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, CodePipelineJobEvent{})
}
111 changes: 86 additions & 25 deletions events/codepipeline_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,99 @@
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
package events

import (
"encoding/json"
"github.com/stretchr/testify/require"
"io/ioutil"
"testing"

"github.com/aws/aws-lambda-go/events/test"
"github.com/stretchr/testify/assert"
"time"
)

func TestCodePipeLineEventMarshaling(t *testing.T) {

// read json from file
inputJSON, err := ioutil.ReadFile("./testdata/codepipline-event.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
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",
},
},
},
}

// de-serialize into CognitoEvent
var inputEvent CodePipelineEvent
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
for _, testcase := range tests {
data, err := ioutil.ReadFile(testcase.input)
require.NoError(t, err)

// serialize to json
outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}
var actual CodePipelineEvent
require.NoError(t, json.Unmarshal(data, &actual))

assert.JSONEq(t, string(inputJSON), string(outputJSON))
}

func TestCodePipelineEventMarshalingMalformedJson(t *testing.T) {
test.TestMalformedJson(t, CodePipelineEvent{})
require.Equal(t, testcase.expect, actual)
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
18 changes: 18 additions & 0 deletions events/testdata/codepipeline-execution-stage-change-event.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
18 changes: 18 additions & 0 deletions events/testdata/codepipeline-execution-state-change-event.json
Original file line number Diff line number Diff line change
@@ -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"
}
}