diff --git a/compliance-agent/src/trackers/gitea.rs b/compliance-agent/src/trackers/gitea.rs index f5a96f0..a7b2a22 100644 --- a/compliance-agent/src/trackers/gitea.rs +++ b/compliance-agent/src/trackers/gitea.rs @@ -114,7 +114,9 @@ impl GiteaTracker { /// Every page of a list endpoint, `path` being the URL without `page` and /// `limit`. Only an empty page ends the list: an instance may cap the page - /// size below ours (`[api] MAX_RESPONSE_ITEMS`). + /// size below ours (`[api] MAX_RESPONSE_ITEMS`). Gitea answers some lists + /// past their end with `null` instead of `[]` (an issue's timeline does), + /// which ends the list the same way. /// /// # Errors /// @@ -133,10 +135,11 @@ impl GiteaTracker { let url = self.api_url(&format!( "{path}{separator}page={page}&limit={LIST_PAGE_SIZE}" )); - let batch: Vec = self.send_json(what, self.http.get(&url)).await?; - if batch.is_empty() { + // `Option` takes the `null` page; serde reads it as `None`. + let batch: Option> = self.send_json(what, self.http.get(&url)).await?; + let Some(batch) = batch.filter(|b| !b.is_empty()) else { return Ok(all); - } + }; all.extend(batch); } Err(CoreError::IssueTracker(format!( @@ -800,6 +803,75 @@ mod tests { assert_eq!(seen.lock().unwrap().len(), 4); } + /// A fake Gitea whose issue timeline has one page of `entries`, then + /// answers `null` as Gitea does past the end of a timeline. + async fn fake_timeline(entries: serde_json::Value) -> String { + let app = Router::new() + .route( + "/api/v1/repos/{owner}/{repo}/issues/{number}/timeline", + get( + |axum::extract::Query(params): axum::extract::Query< + HashMap, + >, + State(entries): State| async move { + if params["page"] == "1" { + Json(entries) + } else { + Json(serde_json::Value::Null) + } + }, + ), + ) + .with_state(entries); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + format!("http://{addr}") + } + + #[tokio::test] + async fn a_null_timeline_page_ends_the_list() { + let base = fake_timeline(serde_json::json!([])).await; + let tracker = + GiteaTracker::new(base, SecretString::from("t"), NetGuard::allow_private()).unwrap(); + let entries: Vec = tracker + .list_all("issue timeline", "/repos/o/r/issues/1/timeline", 3) + .await + .unwrap(); + assert!(entries.is_empty()); + + let base = fake_timeline(serde_json::json!([{ "id": 1 }, { "id": 2 }])).await; + let tracker = + GiteaTracker::new(base, SecretString::from("t"), NetGuard::allow_private()).unwrap(); + let entries: Vec = tracker + .list_all("issue timeline", "/repos/o/r/issues/1/timeline", 3) + .await + .unwrap(); + assert_eq!(entries.len(), 2, "the page before the null one counts"); + } + + #[tokio::test] + async fn a_null_first_timeline_page_is_an_empty_timeline() { + let app = Router::new().route( + "/api/v1/repos/{owner}/{repo}/issues/{number}/timeline", + get(|| async { Json(serde_json::Value::Null) }), + ); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + let tracker = GiteaTracker::new( + format!("http://{addr}"), + SecretString::from("t"), + NetGuard::allow_private(), + ) + .unwrap(); + let entries: Vec = tracker + .list_all("issue timeline", "/repos/o/r/issues/1/timeline", 3) + .await + .unwrap(); + assert!(entries.is_empty()); + } + #[tokio::test] async fn a_failed_listing_is_an_error_not_an_empty_list() { let app = Router::new().route(