1package events
2
3import (
4	"encoding/json"
5	"time"
6)
7
8const (
9	CodeBuildEventSource           = "aws.codebuild"
10	CodeBuildStateChangeDetailType = "CodeBuild Build State Change"
11	CodeBuildPhaseChangeDetailType = "CodeBuild Build Phase Change"
12)
13
14// CodeBuildPhaseStatus represents the status of code build phase (i.e. failed, in progress)
15type CodeBuildPhaseStatus string
16
17const (
18	CodeBuildPhaseStatusFailed     CodeBuildPhaseStatus = "FAILED"
19	CodeBuildPhaseStatusFault      CodeBuildPhaseStatus = "FAULT"
20	CodeBuildPhaseStatusInProgress CodeBuildPhaseStatus = "IN_PROGRESS"
21	CodeBuildPhaseStatusQueued     CodeBuildPhaseStatus = "QUEUED"
22	CodeBuildPhaseStatusStopped    CodeBuildPhaseStatus = "STOPPED"
23	CodeBuildPhaseStatusSucceeded  CodeBuildPhaseStatus = "SUCCEEDED"
24	CodeBuildPhaseStatusTimedOut   CodeBuildPhaseStatus = "TIMED_OUT"
25)
26
27// CodeBuildPhaseType represents the type of the code build phase (i.e. submitted, install)
28type CodeBuildPhaseType string
29
30const (
31	CodeBuildPhaseTypeSubmitted       CodeBuildPhaseType = "SUBMITTED"
32	CodeBuildPhaseTypeQueued          CodeBuildPhaseType = "QUEUED"
33	CodeBuildPhaseTypeProvisioning    CodeBuildPhaseType = "PROVISIONING"
34	CodeBuildPhaseTypeDownloadSource  CodeBuildPhaseType = "DOWNLOAD_SOURCE"
35	CodeBuildPhaseTypeInstall         CodeBuildPhaseType = "INSTALL"
36	CodeBuildPhaseTypePreBuild        CodeBuildPhaseType = "PRE_BUILD"
37	CodeBuildPhaseTypeBuild           CodeBuildPhaseType = "BUILD"
38	CodeBuildPhaseTypePostBuild       CodeBuildPhaseType = "POST_BUILD"
39	CodeBuildPhaseTypeUploadArtifacts CodeBuildPhaseType = "UPLOAD_ARTIFACTS"
40	CodeBuildPhaseTypeFinalizing      CodeBuildPhaseType = "FINALIZING"
41	CodeBuildPhaseTypeCompleted       CodeBuildPhaseType = "COMPLETED"
42)
43
44// CodeBuildEvent is documented at:
45// https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html#sample-build-notifications-ref
46type CodeBuildEvent struct {
47	// AccountID is the id of the AWS account from which the event originated.
48	AccountID string `json:"account"`
49
50	// Region is the AWS region from which the event originated.
51	Region string `json:"region"`
52
53	// DetailType informs the schema of the Detail field. For build state-change
54	// events, the value will be CodeBuildStateChangeDetailType. For phase-change
55	// events, it will be CodeBuildPhaseChangeDetailType.
56	DetailType string `json:"detail-type"`
57
58	// Source should be equal to CodeBuildEventSource.
59	Source string `json:"source"`
60
61	// Version is the version of the event's schema.
62	Version string `json:"version"`
63
64	// Time is the event's timestamp.
65	Time time.Time `json:"time"`
66
67	// ID is the GUID of this event.
68	ID string `json:"id"`
69
70	// Resources is a list of ARNs of CodeBuild builds that this event pertains to.
71	Resources []string `json:"resources"`
72
73	// Detail contains information specific to a build state-change or
74	// build phase-change event.
75	Detail CodeBuildEventDetail `json:"detail"`
76}
77
78// CodeBuildEventDetail represents the all details related to the code build event
79type CodeBuildEventDetail struct {
80	BuildStatus           CodeBuildPhaseStatus                `json:"build-status"`
81	ProjectName           string                              `json:"project-name"`
82	BuildID               string                              `json:"build-id"`
83	AdditionalInformation CodeBuildEventAdditionalInformation `json:"additional-information"`
84	CurrentPhase          CodeBuildPhaseType                  `json:"current-phase"`
85	CurrentPhaseContext   string                              `json:"current-phase-context"`
86	Version               string                              `json:"version"`
87
88	CompletedPhaseStatus   CodeBuildPhaseStatus `json:"completed-phase-status"`
89	CompletedPhase         CodeBuildPhaseType   `json:"completed-phase"`
90	CompletedPhaseContext  string               `json:"completed-phase-context"`
91	CompletedPhaseDuration DurationSeconds      `json:"completed-phase-duration-seconds"`
92	CompletedPhaseStart    CodeBuildTime        `json:"completed-phase-start"`
93	CompletedPhaseEnd      CodeBuildTime        `json:"completed-phase-end"`
94}
95
96//CodeBuildEventAdditionalInformation represents additional information to the code build event
97type CodeBuildEventAdditionalInformation struct {
98	Artifact CodeBuildArtifact `json:"artifact"`
99
100	Environment CodeBuildEnvironment `json:"environment"`
101
102	Timeout DurationMinutes `json:"timeout-in-minutes"`
103
104	BuildComplete bool `json:"build-complete"`
105
106	Initiator string `json:"initiator"`
107
108	BuildStartTime CodeBuildTime `json:"build-start-time"`
109
110	Source CodeBuildSource `json:"source"`
111
112	Logs CodeBuildLogs `json:"logs"`
113
114	Phases []CodeBuildPhase `json:"phases"`
115}
116
117// CodeBuildArtifact represents the artifact provided to build
118type CodeBuildArtifact struct {
119	MD5Sum    string `json:"md5sum"`
120	SHA256Sum string `json:"sha256sum"`
121	Location  string `json:"location"`
122}
123
124// CodeBuildEnvironment represents the environment for a build
125type CodeBuildEnvironment struct {
126	Image                string                         `json:"image"`
127	PrivilegedMode       bool                           `json:"privileged-mode"`
128	ComputeType          string                         `json:"compute-type"`
129	Type                 string                         `json:"type"`
130	EnvironmentVariables []CodeBuildEnvironmentVariable `json:"environment-variables"`
131}
132
133// CodeBuildEnvironmentVariable encapsulate environment variables for the code build
134type CodeBuildEnvironmentVariable struct {
135	// Name is the name of the environment variable.
136	Name string `json:"name"`
137
138	// Type is PLAINTEXT or PARAMETER_STORE.
139	Type string `json:"type"`
140
141	// Value is the value of the environment variable.
142	Value string `json:"value"`
143}
144
145// CodeBuildSource represent the code source will be build
146type CodeBuildSource struct {
147	Location string `json:"location"`
148	Type     string `json:"type"`
149}
150
151// CodeBuildLogs gives the log details of a code build
152type CodeBuildLogs struct {
153	GroupName  string `json:"group-name"`
154	StreamName string `json:"stream-name"`
155	DeepLink   string `json:"deep-link"`
156}
157
158// CodeBuildPhase represents the phase of a build and its details
159type CodeBuildPhase struct {
160	PhaseContext []interface{} `json:"phase-context"`
161
162	StartTime CodeBuildTime `json:"start-time"`
163
164	EndTime CodeBuildTime `json:"end-time"`
165
166	Duration DurationSeconds `json:"duration-in-seconds"`
167
168	PhaseType CodeBuildPhaseType `json:"phase-type"`
169
170	PhaseStatus CodeBuildPhaseStatus `json:"phase-status"`
171}
172
173// CodeBuildTime represents the time of the build
174type CodeBuildTime time.Time
175
176const codeBuildTimeFormat = "Jan 2, 2006 3:04:05 PM"
177
178// MarshalJSON converts a given CodeBuildTime to json
179func (t CodeBuildTime) MarshalJSON() ([]byte, error) {
180	return json.Marshal(time.Time(t).Format(codeBuildTimeFormat))
181}
182
183// UnmarshalJSON converts a given json to a CodeBuildTime
184func (t *CodeBuildTime) UnmarshalJSON(data []byte) error {
185	var s string
186	if err := json.Unmarshal(data, &s); err != nil {
187		return err
188	}
189
190	ts, err := time.Parse(codeBuildTimeFormat, s)
191	if err != nil {
192		return err
193	}
194
195	*t = CodeBuildTime(ts)
196	return nil
197}
198