Skip to content

Commit 250c0d5

Browse files
whithajessbmoffatt
andauthored
Implement CodePipelineEvent (#247)
* [BREAKING CHANGE] Clean up CodePipeline Job Implementation * Incorrectly used "CodePipelineEvent" * Fix original typo missed when moving to job * 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 * Fix up detail types * Allow for floats * Add "CloudWatch" to maintain backwards compatability * go fmt * Change tests to CloudWatch rename also * Remove Job * Fix explicit type constants * Fix merge issue * Add missed CloudWatch change * Codereview fixes * Change tests to match the ExecutionId -> ID change * Again missed the test change to match with int64 * String to Int change to match code change Co-authored-by: Bryan Moffatt <[email protected]>
1 parent d64324e commit 250c0d5

5 files changed

+269
-0
lines changed

events/codepipeline_cloudwatch.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package events
2+
3+
import (
4+
"time"
5+
)
6+
7+
const (
8+
CodePipelineEventSource = "aws.codepipeline"
9+
CodePipelineExecutionEventDetailType = "CodePipeline Pipeline Execution State Change"
10+
CodePipelineActionEventDetailType = "CodePipeline Action Execution State Change"
11+
CodePipelineStageEventDetailType = "CodePipeline Stage Execution State Change"
12+
)
13+
14+
type CodePipelineStageState string
15+
16+
const (
17+
CodePipelineStageStateStarted CodePipelineStageState = "STARTED"
18+
CodePipelineStageStateSucceeded CodePipelineStageState = "SUCCEEDED"
19+
CodePipelineStageStateResumed CodePipelineStageState = "RESUMED"
20+
CodePipelineStageStateFailed CodePipelineStageState = "FAILED"
21+
CodePipelineStageStateCanceled CodePipelineStageState = "CANCELED"
22+
)
23+
24+
type CodePipelineState string
25+
26+
const (
27+
CodePipelineStateStarted CodePipelineState = "STARTED"
28+
CodePipelineStateSucceeded CodePipelineState = "SUCCEEDED"
29+
CodePipelineStateResumed CodePipelineState = "RESUMED"
30+
CodePipelineStateFailed CodePipelineState = "FAILED"
31+
CodePipelineStateCanceled CodePipelineState = "CANCELED"
32+
CodePipelineStateSuperseded CodePipelineState = "SUPERSEDED"
33+
)
34+
35+
type CodePipelineActionState string
36+
37+
const (
38+
CodePipelineActionStateStarted CodePipelineActionState = "STARTED"
39+
CodePipelineActionStateSucceeded CodePipelineActionState = "SUCCEEDED"
40+
CodePipelineActionStateFailed CodePipelineActionState = "FAILED"
41+
CodePipelineActionStateCanceled CodePipelineActionState = "CANCELED"
42+
)
43+
44+
// CodePipelineEvent is documented at:
45+
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#codepipeline_event_type
46+
type CodePipelineCloudWatchEvent struct {
47+
// Version is the version of the event's schema.
48+
Version string `json:"version"`
49+
50+
// ID is the GUID of this event.
51+
ID string `json:"id"`
52+
53+
// DetailType informs the schema of the Detail field. For deployment state-change
54+
// events, the value should be equal to CodePipelineDeploymentEventDetailType.
55+
// For instance state-change events, the value should be equal to
56+
// CodePipelineInstanceEventDetailType.
57+
DetailType string `json:"detail-type"`
58+
59+
// Source should be equal to CodePipelineEventSource.
60+
Source string `json:"source"`
61+
62+
// AccountID is the id of the AWS account from which the event originated.
63+
AccountID string `json:"account"`
64+
65+
// Time is the event's timestamp.
66+
Time time.Time `json:"time"`
67+
68+
// Region is the AWS region from which the event originated.
69+
Region string `json:"region"`
70+
71+
// Resources is a list of ARNs of CodePipeline applications and deployment
72+
// groups that this event pertains to.
73+
Resources []string `json:"resources"`
74+
75+
// Detail contains information specific to a deployment event.
76+
Detail CodePipelineEventDetail `json:"detail"`
77+
}
78+
79+
type CodePipelineEventDetail struct {
80+
Pipeline string `json:"pipeline"`
81+
82+
// From live testing this is always int64 not string as documented
83+
Version int64 `json:"version"`
84+
85+
ExecutionID string `json:"execution-id"`
86+
87+
Stage string `json:"stage"`
88+
89+
Action string `json:"action"`
90+
91+
State CodePipelineState `json:"state"`
92+
93+
Region string `json:"region"`
94+
95+
Type CodePipelineEventDetailType `json:"type"`
96+
}
97+
98+
type CodePipelineEventDetailType struct {
99+
Owner string `json:"owner"`
100+
101+
Category string `json:"category"`
102+
103+
Provider string `json:"provider"`
104+
105+
// From published EventBridge schema registry this is always int64 not string as documented
106+
Version int64 `json:"version"`
107+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package events
2+
3+
import (
4+
"encoding/json"
5+
"github.com/stretchr/testify/require"
6+
"io/ioutil"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestUnmarshalCodePipelineEvent(t *testing.T) {
12+
tests := []struct {
13+
input string
14+
expect CodePipelineCloudWatchEvent
15+
}{
16+
{
17+
input: "testdata/codepipeline-action-execution-stage-change-event.json",
18+
expect: CodePipelineCloudWatchEvent{
19+
Version: "0",
20+
ID: "CWE-event-id",
21+
DetailType: "CodePipeline Action Execution State Change",
22+
Source: "aws.codepipeline",
23+
AccountID: "123456789012",
24+
Time: time.Date(2017, 04, 22, 3, 31, 47, 0, time.UTC),
25+
Region: "us-east-1",
26+
Resources: []string{
27+
"arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline",
28+
},
29+
Detail: CodePipelineEventDetail{
30+
Pipeline: "myPipeline",
31+
Version: 1,
32+
ExecutionID: "01234567-0123-0123-0123-012345678901",
33+
Stage: "Prod",
34+
Action: "myAction",
35+
State: "STARTED",
36+
Region: "us-west-2",
37+
Type: CodePipelineEventDetailType{
38+
Owner: "AWS",
39+
Category: "Deploy",
40+
Provider: "CodeDeploy",
41+
Version: 1,
42+
},
43+
},
44+
},
45+
},
46+
{
47+
input: "testdata/codepipeline-execution-stage-change-event.json",
48+
expect: CodePipelineCloudWatchEvent{
49+
Version: "0",
50+
ID: "CWE-event-id",
51+
DetailType: "CodePipeline Stage Execution State Change",
52+
Source: "aws.codepipeline",
53+
AccountID: "123456789012",
54+
Time: time.Date(2017, 04, 22, 3, 31, 47, 0, time.UTC),
55+
Region: "us-east-1",
56+
Resources: []string{
57+
"arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline",
58+
},
59+
Detail: CodePipelineEventDetail{
60+
Pipeline: "myPipeline",
61+
Version: 1,
62+
ExecutionID: "01234567-0123-0123-0123-012345678901",
63+
State: "STARTED",
64+
},
65+
},
66+
},
67+
{
68+
input: "testdata/codepipeline-execution-state-change-event.json",
69+
expect: CodePipelineCloudWatchEvent{
70+
Version: "0",
71+
ID: "CWE-event-id",
72+
DetailType: "CodePipeline Pipeline Execution State Change",
73+
Source: "aws.codepipeline",
74+
AccountID: "123456789012",
75+
Time: time.Date(2017, 04, 22, 3, 31, 47, 0, time.UTC),
76+
Region: "us-east-1",
77+
Resources: []string{
78+
"arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline",
79+
},
80+
Detail: CodePipelineEventDetail{
81+
Pipeline: "myPipeline",
82+
Version: 1,
83+
ExecutionID: "01234567-0123-0123-0123-012345678901",
84+
State: "STARTED",
85+
},
86+
},
87+
},
88+
}
89+
90+
for _, testcase := range tests {
91+
data, err := ioutil.ReadFile(testcase.input)
92+
require.NoError(t, err)
93+
94+
var actual CodePipelineCloudWatchEvent
95+
require.NoError(t, json.Unmarshal(data, &actual))
96+
97+
require.Equal(t, testcase.expect, actual)
98+
}
99+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"version": "0",
3+
"id": "CWE-event-id",
4+
"detail-type": "CodePipeline Action Execution State Change",
5+
"source": "aws.codepipeline",
6+
"account": "123456789012",
7+
"time": "2017-04-22T03:31:47Z",
8+
"region": "us-east-1",
9+
"resources": [
10+
"arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline"
11+
],
12+
"detail": {
13+
"pipeline": "myPipeline",
14+
"version": 1,
15+
"execution-id": "01234567-0123-0123-0123-012345678901",
16+
"stage": "Prod",
17+
"action": "myAction",
18+
"state": "STARTED",
19+
"region":"us-west-2",
20+
"type": {
21+
"owner": "AWS",
22+
"category": "Deploy",
23+
"provider": "CodeDeploy",
24+
"version": 1
25+
}
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "0",
3+
"id": "CWE-event-id",
4+
"detail-type": "CodePipeline Stage Execution State Change",
5+
"source": "aws.codepipeline",
6+
"account": "123456789012",
7+
"time": "2017-04-22T03:31:47Z",
8+
"region": "us-east-1",
9+
"resources": [
10+
"arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline"
11+
],
12+
"detail": {
13+
"pipeline": "myPipeline",
14+
"version": 1,
15+
"state": "STARTED",
16+
"execution-id": "01234567-0123-0123-0123-012345678901"
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "0",
3+
"id": "CWE-event-id",
4+
"detail-type": "CodePipeline Pipeline Execution State Change",
5+
"source": "aws.codepipeline",
6+
"account": "123456789012",
7+
"time": "2017-04-22T03:31:47Z",
8+
"region": "us-east-1",
9+
"resources": [
10+
"arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline"
11+
],
12+
"detail": {
13+
"pipeline": "myPipeline",
14+
"version": 1,
15+
"state": "STARTED",
16+
"execution-id": "01234567-0123-0123-0123-012345678901"
17+
}
18+
}

0 commit comments

Comments
 (0)