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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// maybe wait a few seconds to be sure a Jenkins job was created. This happens at the caller.
// make request to [branch]-branches
// if it comes back successfully with a `builds` hash
// request all URLs in `builds`
// if its `displayName` matches [branch]-commitsha{5}
// check `result` ('SUCCESS', 'FAILURE', nonexistent)
// update GitHub commit status
// if pending
// start a thread that checks every 30 seconds for the `result` and update GitHub commit status
// if time spent > 20 minutes
// set GH commit status to error (timeout)
// if `result` is successful or failed, update status and stop
// set GH status to error (no job found)
// fn update_github_status(commit_ref)
// fn get_jobs(repo_name)
// fn af83 job name from commit_ref (separate af83 module)
// fn update_github_commit_status(status, message) (lives in GitHub module)
// fn request_job(url)
// fn result_from_job(payload)
extern crate json;
extern crate mockito;
extern crate reqwest;
extern crate url;
use std::error::Error;
use std::thread;
use std::thread::sleep;
use std::time::{Duration, Instant};
use self::reqwest::header::{Authorization, Basic};
use self::url::Url;
use af83;
use github;
use pull_request::CommitRef;
#[cfg(not(test))]
const API_URL: &'static str = "http://jenkins.example.com";
#[cfg(test)]
const API_URL: &'static str = mockito::SERVER_URL;
#[derive(Debug, PartialEq, Eq)]
pub enum JobStatus {
Success,
Failure,
Pending,
Unknown,
}
impl JobStatus {
fn commit_status(&self) -> github::CommitStatus {
match *self {
JobStatus::Success => github::CommitStatus::Success,
JobStatus::Failure => github::CommitStatus::Failure,
JobStatus::Pending => github::CommitStatus::Pending,
JobStatus::Unknown => github::CommitStatus::Error,
}
}
}
pub struct Job {
display_name: String,
result: JobStatus,
}
impl Job {
fn new(payload: String) -> Result<Job, Box<Error>> {
let mut job = json::parse(payload.as_ref())?;
Ok(
Job {
display_name: job["displayName"].take_string().unwrap_or_default(),
result: result_from_job(job["result"].take_string()),
}
)
}
}
pub fn find_and_track_build_and_update_status(commit_ref: CommitRef)
-> Result<(), Box<Error>> {
let jobs = get_jobs(commit_ref.repo.as_ref())?;
let t20_minutes = 60 * 20;
for job_url in jobs {
let mut job = request_job(job_url.as_ref())?;
// Does `displayName` match
if job_for_commit(&job, &commit_ref) {
thread::spawn(move || {
// Start timer
let now = Instant::now();
let commit_status = job.result.commit_status();
github::update_commit_status(
&commit_ref,
&commit_status,
job_url.clone(),
None,
"continuous-integration/jenkins".to_string()
);
while job.result == JobStatus::Pending {
// loop
// if timer > 20 minutes
// call github::update_commit_status with timeout error
// return
// wait 30 seconds
// call request_job again
// if the status is different
// call github::update_commit_status
// stop
if now.elapsed().as_secs() == t20_minutes {
github::update_commit_status(
&commit_ref,
&github::CommitStatus::Error,
job_url.clone(),
Some("The status checker timed out.".to_string()),
"continuous-integration/jenkins".to_string()
);
return
}
sleep(Duration::from_secs(30));
let updated_job = request_job(job_url.as_ref()).unwrap();
if job.result != updated_job.result {
github::update_commit_status(
&commit_ref,
&job.result.commit_status(),
job_url.clone(),
None,
"continuous-integration/jenkins".to_string()
);
return
}
job = updated_job;
}
});
return Ok(())
}
}
Ok(())
}
pub fn auth_credentials() -> Basic {
Basic {
username: "username".to_string(),
password: Some("token".to_string()),
}
}
pub fn get_jobs(repo_name: &str) -> Result<Vec<String>, Box<Error>> {
let client = reqwest::Client::new();
let credentials = auth_credentials();
let mut response = client.get(&format!("{}/job/{}-branches/api/json", API_URL, repo_name))
.header(Authorization(credentials))
.send()?;
let body = response.text()?;
let jobs = json::parse(body.as_ref())?;
Ok(
jobs["builds"].members()
.map(|job| {
job["url"].clone().take_string().unwrap_or_default()
})
.collect::<Vec<String>>()
)
}
pub fn request_job(url: &str) -> Result<Job, Box<Error>> {
let url = Url::parse(url.as_ref())?;
let client = reqwest::Client::new();
let credentials = auth_credentials();
let mut response = client.get(&format!("{}{}/api/json", API_URL, url.path()))
.header(Authorization(credentials))
.send()?;
let body = response.text()?;
let mut job = json::parse(body.as_ref())?;
Ok(
Job {
display_name: job["displayName"].take_string().unwrap_or_default(),
result: result_from_job(job["result"].take_string()),
}
)
}
// Does the `commit_ref` correspond to the job?
pub fn job_for_commit(job: &Job, commit_ref: &CommitRef) -> bool {
job.display_name == af83::job_name(&commit_ref)
}
pub fn result_from_job(status: Option<String>) -> JobStatus {
match status {
None => JobStatus::Pending,
Some(s) => {
match s.as_ref() {
"SUCCESS" => JobStatus::Success,
"FAILURE" => JobStatus::Failure,
_ => JobStatus::Unknown,
}
}
}
}
#[cfg(test)]
mod tests {
use self::mockito::mock;
use super::*;
#[test]
fn job_new_creates_a_job_from_payload() {
let payload = r#"{
"displayName": "3296-fix-typo-700d0",
"result": "SUCCESS"
}"#.to_string();
let job = Job::new(payload);
assert_eq!(job.display_name, "3296-fix-typo-700d0");
assert_eq!(job.result, JobStatus::Success);
}
#[test]
fn get_jobs_queries_jobs_from_jenkins_api() {
let _mock = mock("GET", "/job/changes-branches/api/json")
.with_status(200)
.with_header("content-type", "application/json;charset=utf-8")
.with_body(r#"
{
"displayName": "changes-branches",
"builds": [
{
"_class": "hudson.model.FreeStyleBuild",
"number": 18,
"url": "http://jenkins.example.com/job/changes-branches/18/"
},
{
"_class": "hudson.model.FreeStyleBuild",
"number": 17,
"url": "http://jenkins.example.com/job/changes-branches/17/"
}
]
}
"#)
.create();
let jobs = get_jobs("changes");
assert_eq!(
jobs,
[
"http://jenkins.example.com/job/changes-branches/18/",
"http://jenkins.example.com/job/changes-branches/17/"
]
);
}
#[test]
fn request_job_queries_a_job_from_the_jenkins_api() {
let _mock = mock("GET", "/job/changes-branches/15/api/json")
.with_status(200)
.with_header("content-type", "application/json;charset=utf-8")
.with_body(r#"
{
"displayName": "2388-delete-the-codes-391af",
"result": "SUCCESS"
}
"#)
.create();
let job = request_job("http://jenkins.example.com/job/changes-branches/15");
let expected = Job {
display_name: "2388-delete-the-codes-391af".to_string(),
result: JobStatus::Success,
};
assert_eq!(job.display_name, expected.display_name);
assert_eq!(job.result, expected.result);
}
#[test]
fn job_for_commit_returns_true_when_commit_matches_job() {
let job = Job {
display_name: "1753-fix-everything-b4a28".to_string(),
result: JobStatus::Pending,
};
let commit_ref = CommitRef {
owner: "uso".to_string(),
repo: "vivid-system".to_string(),
sha: "b4a286e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c".to_string(),
branch: "1753-fix-everything".to_string(),
};
assert_eq!(job_for_commit(&job, &commit_ref), true);
}
#[test]
fn job_for_commit_returns_false_when_commit_doesnt_match_job() {
let job = Job {
display_name: "5234-eliminate-widgetmacallit-5a28c".to_string(),
result: JobStatus::Success,
};
let commit_ref = CommitRef {
owner: "uso".to_string(),
repo: "vivid-system".to_string(),
sha: "b4a286e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c".to_string(),
branch: "1753-fix-everything".to_string(),
};
assert_eq!(job_for_commit(&job, &commit_ref), false);
}
#[test]
fn result_from_job_is_success() {
assert_eq!(
result_from_job(Some("SUCCESS".to_string())),
JobStatus::Success
);
}
#[test]
fn result_from_job_is_failure() {
assert_eq!(
result_from_job(Some("FAILURE".to_string())),
JobStatus::Failure
);
}
#[test]
fn result_from_job_is_pending() {
assert_eq!(
result_from_job(None),
JobStatus::Pending
);
}
}
|