1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::custom_serde::{codebuild_time, CodeBuildNumber};
use crate::encodings::{MinuteDuration, SecondDuration};
use chrono::{DateTime, Utc};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;

pub type CodeBuildPhaseStatus = String;

pub type CodeBuildPhaseType = String;

/// `CodeBuildEvent` is documented at:
/// https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html#sample-build-notifications-ref
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildEvent {
    /// AccountID is the id of the AWS account from which the event originated.
    #[serde(default)]
    #[serde(rename = "account")]
    pub account_id: Option<String>,
    /// Region is the AWS region from which the event originated.
    #[serde(default)]
    pub region: Option<String>,
    /// DetailType informs the schema of the Detail field. For build state-change
    /// events, the value will be CodeBuildStateChangeDetailType. For phase-change
    /// events, it will be CodeBuildPhaseChangeDetailType.
    #[serde(default)]
    #[serde(rename = "detail-type")]
    pub detail_type: Option<String>,
    /// Source should be equal to CodeBuildEventSource.
    #[serde(default)]
    pub source: Option<String>,
    /// Version is the version of the event's schema.
    #[serde(default)]
    pub version: Option<String>,
    /// Time is the event's timestamp.
    pub time: DateTime<Utc>,
    /// ID is the GUID of this event.
    #[serde(default)]
    pub id: Option<String>,
    /// Resources is a list of ARNs of CodeBuild builds that this event pertains to.
    pub resources: Vec<String>,
    /// Detail contains information specific to a build state-change or
    /// build phase-change event.
    pub detail: CodeBuildEventDetail,
}

/// `CodeBuildEventDetail` represents the all details related to the code build event
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildEventDetail {
    #[serde(rename = "build-status")]
    pub build_status: Option<CodeBuildPhaseStatus>,
    #[serde(default)]
    #[serde(rename = "project-name")]
    pub project_name: Option<String>,
    #[serde(default)]
    #[serde(rename = "build-id")]
    pub build_id: Option<String>,
    #[serde(rename = "additional-information")]
    pub additional_information: CodeBuildEventAdditionalInformation,
    #[serde(rename = "current-phase")]
    pub current_phase: Option<CodeBuildPhaseType>,
    #[serde(default)]
    #[serde(rename = "current-phase-context")]
    pub current_phase_context: Option<String>,
    #[serde(default)]
    pub version: Option<String>,
    #[serde(rename = "completed-phase-status")]
    pub completed_phase_status: Option<CodeBuildPhaseStatus>,
    #[serde(rename = "completed-phase")]
    pub completed_phase: Option<CodeBuildPhaseType>,
    #[serde(default)]
    #[serde(rename = "completed-phase-context")]
    pub completed_phase_context: Option<String>,
    #[serde(rename = "completed-phase-duration-seconds")]
    pub completed_phase_duration: Option<SecondDuration>,
    #[serde(rename = "completed-phase-start")]
    #[serde(default)]
    #[serde(with = "codebuild_time::optional_time")]
    pub completed_phase_start: Option<CodeBuildTime>,
    #[serde(rename = "completed-phase-end")]
    #[serde(default)]
    #[serde(with = "codebuild_time::optional_time")]
    pub completed_phase_end: Option<CodeBuildTime>,
}

/// `CodeBuildEventAdditionalInformation` represents additional information to the code build event
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildEventAdditionalInformation {
    pub artifact: CodeBuildArtifact,
    pub environment: CodeBuildEnvironment,
    #[serde(rename = "timeout-in-minutes")]
    pub timeout: MinuteDuration,
    #[serde(rename = "build-complete")]
    pub build_complete: bool,
    #[serde(rename = "build-number")]
    pub build_number: Option<CodeBuildNumber>,
    #[serde(default)]
    pub initiator: Option<String>,
    #[serde(rename = "build-start-time")]
    #[serde(with = "codebuild_time::str_time")]
    pub build_start_time: CodeBuildTime,
    pub source: CodeBuildSource,
    #[serde(default)]
    #[serde(rename = "source-version")]
    pub source_version: Option<String>,
    pub logs: CodeBuildLogs,
    pub phases: Vec<CodeBuildPhase>,
}

/// `CodeBuildArtifact` represents the artifact provided to build
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildArtifact {
    #[serde(default)]
    #[serde(rename = "md5sum")]
    pub md5_sum: Option<String>,
    #[serde(default)]
    #[serde(rename = "sha256sum")]
    pub sha256_sum: Option<String>,
    #[serde(default)]
    pub location: Option<String>,
}

/// `CodeBuildEnvironment` represents the environment for a build
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildEnvironment {
    #[serde(default)]
    pub image: Option<String>,
    #[serde(rename = "privileged-mode")]
    pub privileged_mode: bool,
    #[serde(default)]
    #[serde(rename = "compute-type")]
    pub compute_type: Option<String>,
    #[serde(default)]
    pub type_: Option<String>,
    #[serde(rename = "environment-variables")]
    pub environment_variables: Vec<CodeBuildEnvironmentVariable>,
}

/// `CodeBuildEnvironmentVariable` encapsulate environment variables for the code build
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildEnvironmentVariable {
    /// Name is the name of the environment variable.
    #[serde(default)]
    pub name: Option<String>,
    /// Type is PLAINTEXT or PARAMETER_STORE.
    #[serde(default)]
    pub type_: Option<String>,
    /// Value is the value of the environment variable.
    #[serde(default)]
    pub value: Option<String>,
}

/// `CodeBuildSource` represent the code source will be build
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildSource {
    #[serde(default)]
    pub location: Option<String>,
    #[serde(default)]
    pub type_: Option<String>,
}

/// `CodeBuildLogs` gives the log details of a code build
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildLogs {
    #[serde(default)]
    #[serde(rename = "group-name")]
    pub group_name: Option<String>,
    #[serde(default)]
    #[serde(rename = "stream-name")]
    pub stream_name: Option<String>,
    #[serde(default)]
    #[serde(rename = "deep-link")]
    pub deep_link: Option<String>,
}

/// `CodeBuildPhase` represents the phase of a build and its details
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeBuildPhase<T1 = Value>
where
    T1: DeserializeOwned,
    T1: Serialize,
{
    #[serde(bound = "")]
    #[serde(rename = "phase-context")]
    pub phase_context: Option<Vec<T1>>,
    #[serde(rename = "start-time")]
    #[serde(with = "codebuild_time::str_time")]
    pub start_time: CodeBuildTime,
    #[serde(rename = "end-time")]
    #[serde(default)]
    #[serde(with = "codebuild_time::optional_time")]
    pub end_time: Option<CodeBuildTime>,
    #[serde(rename = "duration-in-seconds")]
    pub duration: Option<SecondDuration>,
    #[serde(rename = "phase-type")]
    pub phase_type: CodeBuildPhaseType,
    #[serde(rename = "phase-status")]
    pub phase_status: Option<CodeBuildPhaseStatus>,
}

pub type CodeBuildTime = DateTime<Utc>;

#[cfg(test)]
mod test {
    use super::*;

    use serde_json;

    #[test]
    #[cfg(feature = "codebuild")]
    fn example_codebuild_phase_change() {
        let data = include_bytes!("../../fixtures/example-codebuild-phase-change.json");
        let parsed: CodeBuildEvent = serde_json::from_slice(data).unwrap();
        let output: String = serde_json::to_string(&parsed).unwrap();
        let reparsed: CodeBuildEvent = serde_json::from_slice(output.as_bytes()).unwrap();
        assert_eq!(parsed, reparsed);
    }

    #[test]
    #[cfg(feature = "codebuild")]
    fn example_codebuild_state_change() {
        let data = include_bytes!("../../fixtures/example-codebuild-state-change.json");
        let parsed: CodeBuildEvent = serde_json::from_slice(data).unwrap();
        let output: String = serde_json::to_string(&parsed).unwrap();
        let reparsed: CodeBuildEvent = serde_json::from_slice(output.as_bytes()).unwrap();
        assert_eq!(parsed, reparsed);
    }
}