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
|
// 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 reqwest;
use self::reqwest::header::{Authorization, Basic};
use af83;
use pull_request::CommitRef;
#[derive(Debug, PartialEq, Eq)]
pub enum JobStatus {
Success,
Failure,
Pending,
Unknown,
}
pub struct Job {
display_name: String,
result: JobStatus,
}
impl Job {
fn new(payload: String) -> Job {
let mut job = json::parse(payload.as_ref()).unwrap();
Job {
display_name: job["displayName"].take_string().unwrap(),
result: result_from_job(job["result"].take_string()),
}
}
}
pub fn update_commit_status(commit_ref) {
let jobs = get_jobs();
for job_url in jobs {
let payload = request_job(job_url);
// Does `displayName` match
if job_for_commit(payload, commit_ref) {
// spawn thread
let status = result_from_job(payload);
}
}
}
pub fn auth_credentials() -> Basic {
Basic {
username: "username".to_string(),
password: Some("token".to_string()),
}
}
pub fn get_jobs(repo_name: String) {//-> Vec<String> {
let client = reqwest::Client::new();
let credentials = auth_credentials();
let mut res = client.get("http://jenkins.example.com/job/changes-branches/18/api/json")
.header(Authorization(credentials))
.send()
.unwrap();
println!("{}", res.status());
}
// 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 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".to_string());
assert_eq!(
jobs,
[
"http://jenkins.example.com/job/changes-branches/18/",
"http://jenkins.example.com/job/changes-branches/17/"
]
);
}
#[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 {
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 {
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
);
}
}
|