Skip to content

Commit 17a8f15

Browse files
committed
Some updates to assist with testing
TRIAGEBOT_TEST_RECORD can be used to record webhook events to disk. TRIAGEBOT_TEST_DISABLE_JOBS can be used to disable automatic jobs.
1 parent 1e46b9f commit 17a8f15

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/github.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ pub struct Issue {
254254
pub number: u64,
255255
#[serde(deserialize_with = "opt_string")]
256256
pub body: String,
257-
created_at: chrono::DateTime<Utc>,
257+
pub created_at: chrono::DateTime<Utc>,
258258
pub updated_at: chrono::DateTime<Utc>,
259259
/// The SHA for a merge commit.
260260
///

src/main.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ async fn serve_req(
219219
.unwrap());
220220
}
221221
};
222+
maybe_record_test(&event, &payload);
222223

223224
match triagebot::webhook(event, payload, &ctx).await {
224225
Ok(true) => Ok(Response::new(Body::from("processed request"))),
@@ -233,6 +234,70 @@ async fn serve_req(
233234
}
234235
}
235236

237+
/// Webhook recording to help with writing server_test integration tests.
238+
fn maybe_record_test(event: &EventName, payload: &str) {
239+
if std::env::var_os("TRIAGEBOT_TEST_RECORD").is_none() {
240+
return;
241+
}
242+
let sequence_path = |name| {
243+
let path = PathBuf::from(format!("{name}.json"));
244+
if !path.exists() {
245+
return path;
246+
}
247+
let mut index = 1;
248+
loop {
249+
let path = PathBuf::from(format!("{name}.{index}.json"));
250+
if !path.exists() {
251+
return path;
252+
}
253+
index += 1;
254+
}
255+
};
256+
let payload_json: serde_json::Value = serde_json::from_str(payload).expect("valid json");
257+
let name = match event {
258+
EventName::PullRequest => {
259+
let action = payload_json["action"].as_str().unwrap();
260+
let number = payload_json["number"].as_u64().unwrap();
261+
format!("pr{number}_{action}")
262+
}
263+
EventName::PullRequestReview => {
264+
let action = payload_json["action"].as_str().unwrap();
265+
let number = payload_json["pull_request"]["number"].as_u64().unwrap();
266+
format!("pr{number}_review_{action}")
267+
}
268+
EventName::PullRequestReviewComment => {
269+
let action = payload_json["action"].as_str().unwrap();
270+
let number = payload_json["pull_request"]["number"].as_u64().unwrap();
271+
format!("pr{number}_review_comment_{action}")
272+
}
273+
EventName::IssueComment => {
274+
let action = payload_json["action"].as_str().unwrap();
275+
let number = payload_json["issue"]["number"].as_u64().unwrap();
276+
format!("issue{number}_comment_{action}")
277+
}
278+
EventName::Issue => {
279+
let action = payload_json["action"].as_str().unwrap();
280+
let number = payload_json["issue"]["number"].as_u64().unwrap();
281+
format!("issue{number}_{action}")
282+
}
283+
EventName::Push => {
284+
let after = payload_json["after"].as_str().unwrap();
285+
format!("push_{after}")
286+
}
287+
EventName::Create => {
288+
let ref_type = payload_json["ref_type"].as_str().unwrap();
289+
let git_ref = payload_json["ref"].as_str().unwrap();
290+
format!("create_{ref_type}_{git_ref}")
291+
}
292+
EventName::Other => {
293+
return;
294+
}
295+
};
296+
let path = sequence_path(name);
297+
std::fs::write(&path, payload).unwrap();
298+
log::info!("recorded JSON payload to {:?}", path);
299+
}
300+
236301
async fn run_server(addr: SocketAddr) -> anyhow::Result<()> {
237302
let pool = db::ClientPool::new();
238303
db::run_migrations(&*pool.get().await)
@@ -242,6 +307,9 @@ async fn run_server(addr: SocketAddr) -> anyhow::Result<()> {
242307
// spawning a background task that will schedule the jobs
243308
// every JOB_SCHEDULING_CADENCE_IN_SECS
244309
task::spawn(async move {
310+
if env::var_os("TRIAGEBOT_TEST_DISABLE_JOBS").is_some() {
311+
return;
312+
}
245313
loop {
246314
let res = task::spawn(async move {
247315
let pool = db::ClientPool::new();

0 commit comments

Comments
 (0)