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