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
#![deny(missing_docs)]
extern crate reqwest;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use std::thread;
use std::time::Duration;
use std::env;
use std::str::FromStr;
use reqwest::{RedirectPolicy, StatusCode};
use reqwest::header::UserAgent;
mod error;
mod matrix;
pub use error::Error;
pub use matrix::{Matrix, MatrixElement};
const USER_AGENT: &'static str = concat!("travis-after-all/", env!("CARGO_PKG_VERSION"));
const TRAVIS_JOB_NUMBER : &'static str = "TRAVIS_JOB_NUMBER";
const TRAVIS_BUILD_ID: &'static str = "TRAVIS_BUILD_ID";
const TRAVIS_API_URL: &'static str = "https://api.travis-ci.org";
const POLLING_INTERVAL: &'static str = "LEADER_POLLING_INTERVAL";
fn env_var(varname: &str) -> Result<String, Error> {
env::var(varname)
.map_err(|_| Error::from_string(format!("Missing environment variable: {}", varname)))
}
fn is_leader(job: &str) -> bool {
job.ends_with('1')
}
pub struct Build {
travis_api_url: String,
build_id: String,
job_number: String,
polling_interval: u64,
}
impl Build {
pub fn from_env() -> Result<Build, Error> {
let build_id = try!(env_var(TRAVIS_BUILD_ID));
let job_number = try!(env_var(TRAVIS_JOB_NUMBER));
let polling_interval = match env_var(POLLING_INTERVAL) {
Err(_) => 5,
Ok(val) => try!(FromStr::from_str(&val))
};
Ok(Build {
travis_api_url: TRAVIS_API_URL.into(),
build_id: build_id,
job_number: job_number,
polling_interval: polling_interval,
})
}
pub fn is_leader(&self) -> bool {
is_leader(&self.job_number)
}
pub fn build_matrix(&self) -> Result<Matrix, Error> {
let url = format!("{}/builds/{}", self.travis_api_url, self.build_id);
let mut client = reqwest::Client::new().unwrap();
client.redirect(RedirectPolicy::limited(5));
let mut res = client.get(&url)
.header(UserAgent(USER_AGENT.to_string()))
.send()
.unwrap();
if *res.status() == StatusCode::NotFound {
return Err(Error::BuildNotFound);
}
res.json().map_err(|e| From::from(e))
}
pub fn wait_for_others(&self) -> Result<(), Error> {
if !self.is_leader() {
return Err(Error::NotLeader)
}
let dur = Duration::new(self.polling_interval, 0);
loop {
let matrix = try!(self.build_matrix());
if matrix.others_finished() {
break;
}
thread::sleep(dur);
}
let matrix = try!(self.build_matrix());
match matrix.others_succeeded() {
true => Ok(()),
false => Err(Error::FailedBuilds)
}
}
}