diff --git a/compliance-agent/src/git_source/gitea.rs b/compliance-agent/src/git_source/gitea.rs index 3a7ac35..17b560e 100644 --- a/compliance-agent/src/git_source/gitea.rs +++ b/compliance-agent/src/git_source/gitea.rs @@ -128,11 +128,14 @@ impl RepoLister for SourceClient { ))); } let batch: Vec = resp.json().await?; - let last = batch.len() < PAGE_SIZE; - repos.extend(batch); - if last { + // Only an empty page ends the list: an instance may cap the page + // size below `PAGE_SIZE` (`[api] MAX_RESPONSE_ITEMS`), and taking + // a short page for the last one would archive every repository + // after it. + if batch.is_empty() { return Ok(repos); } + repos.extend(batch); } Err(AgentError::Other(format!( "'{org}' lists more than {} repositories; sync stopped", @@ -164,7 +167,7 @@ mod tests { } /// A fake Gitea serving `total` repositories of org `kunde`, paginated. - async fn fake_gitea(total: i64, seen: Arc>>) -> String { + async fn fake_gitea(total: i64, cap: i64, seen: Arc>>) -> String { let app = Router::new().route( "/api/v1/orgs/{org}/repos", get( @@ -179,7 +182,7 @@ mod tests { headers["authorization"].to_str().unwrap() )); let page: i64 = q["page"].parse().unwrap(); - let limit: i64 = q["limit"].parse().unwrap(); + let limit: i64 = q["limit"].parse::().unwrap().min(cap); let from = (page - 1) * limit + 1; let to = (page * limit).min(total); let items: Vec<_> = (from..=to) @@ -199,7 +202,7 @@ mod tests { #[tokio::test] async fn lists_every_page_with_the_bot_token() { let seen = Arc::new(Mutex::new(Vec::new())); - let base = fake_gitea(120, seen.clone()).await; + let base = fake_gitea(120, 50, seen.clone()).await; let client = SourceClient::new( &base, SecretString::from("bot-tok"), @@ -216,6 +219,7 @@ mod tests { "kunde page=1 auth=token bot-tok", "kunde page=2 auth=token bot-tok", "kunde page=3 auth=token bot-tok", + "kunde page=4 auth=token bot-tok", ] ); } @@ -223,13 +227,23 @@ mod tests { #[tokio::test] async fn an_exact_multiple_of_the_page_size_ends_on_an_empty_page() { let seen = Arc::new(Mutex::new(Vec::new())); - let base = fake_gitea(50, seen.clone()).await; + let base = fake_gitea(50, 50, seen.clone()).await; let client = SourceClient::new(&base, SecretString::from("t"), NetGuard::allow_private()).unwrap(); assert_eq!(client.list_org_repos("kunde").await.unwrap().len(), 50); assert_eq!(seen.lock().unwrap().len(), 2); } + #[tokio::test] + async fn a_capped_page_size_is_not_taken_for_the_end() { + // An instance with MAX_RESPONSE_ITEMS=30 answers 30 per page. + let seen = Arc::new(Mutex::new(Vec::new())); + let base = fake_gitea(70, 30, seen.clone()).await; + let client = + SourceClient::new(&base, SecretString::from("t"), NetGuard::allow_private()).unwrap(); + assert_eq!(client.list_org_repos("kunde").await.unwrap().len(), 70); + } + #[test] fn owner_falls_back_to_the_full_name() { let r: GiteaRepo = serde_json::from_value(serde_json::json!({ diff --git a/compliance-agent/src/git_source/mod.rs b/compliance-agent/src/git_source/mod.rs index b3f59ba..c207127 100644 --- a/compliance-agent/src/git_source/mod.rs +++ b/compliance-agent/src/git_source/mod.rs @@ -634,7 +634,11 @@ pub async fn retoken_targets( "updated_at": mongodb::bson::DateTime::now(), } }, ) - .array_filters(vec![doc! { "git.kind": "git_repo" }]) + // Only git artifacts that have credentials: setting a field inside a + // `null` auth would fail the whole update. + .array_filters(vec![ + doc! { "git.kind": "git_repo", "git.auth": { "$type": "object" } }, + ]) .await?; Ok(res.modified_count) } diff --git a/compliance-agent/src/trackers/gitea.rs b/compliance-agent/src/trackers/gitea.rs index 1ad1a78..8e2b6f2 100644 --- a/compliance-agent/src/trackers/gitea.rs +++ b/compliance-agent/src/trackers/gitea.rs @@ -70,11 +70,12 @@ impl GiteaTracker { let batch: Vec = resp.json().await.map_err(|e| { CoreError::IssueTracker(format!("Failed to parse Gitea labels: {e}")) })?; - let last = batch.len() < LABEL_PAGE_SIZE; - labels.extend(batch); - if last { + // Only an empty page ends the list (the instance may cap the + // page size below ours). + if batch.is_empty() { break; } + labels.extend(batch); } Ok(labels) } diff --git a/compliance-agent/tests/git_source.rs b/compliance-agent/tests/git_source.rs index 3a344b3..691700a 100644 --- a/compliance-agent/tests/git_source.rs +++ b/compliance-agent/tests/git_source.rs @@ -352,6 +352,31 @@ async fn platform_api_webhook_and_sync() { ); assert_eq!(h.managed_count(11).await + h.managed_count(13).await, 2); + // ── a new bot token reaches the managed targets ───────────────────── + let (status, _) = h + .put_source(serde_json::json!({ + "base_url": "https://kunde.code.breakpilot.com", "org": "kunde", + "bot_token": "bot-pat-2", "enabled": false + })) + .await; + assert_eq!(status, 200); + let target = + h.db.onboarded_targets() + .find_one(doc! { "source_repo.repo_id": 11_i64 }) + .await + .unwrap() + .unwrap(); + let tracker_token = target.scan_config.issue_tracker.unwrap().token.unwrap(); + let git_token = target.artifacts[0] + .auth + .as_ref() + .unwrap() + .secret + .clone() + .unwrap(); + assert_eq!(h.agent.secrets.open(&tracker_token).unwrap(), "bot-pat-2"); + assert_eq!(h.agent.secrets.open(&git_token).unwrap(), "bot-pat-2"); + // ── delete: the source goes, its targets are archived ─────────────── let resp: serde_json::Value = h .client diff --git a/docs/guide/managed-git-en.md b/docs/guide/managed-git-en.md index 6d2a4af..4e76f2b 100644 --- a/docs/guide/managed-git-en.md +++ b/docs/guide/managed-git-en.md @@ -23,7 +23,8 @@ of an organisation in that instance automatically: with its targets, in case a webhook delivery was lost. BreakPilot sets the connection up during provisioning; there is nothing for you -to configure. IT administrators can see it in the workspace (read-only). +to configure. IT administrators can read it through the API +(`GET /api/v1/git-source`); a view in the interface will follow. ::: tip Deleted a target? A target that belongs to a repository of the connected organisation is created diff --git a/docs/guide/managed-git.md b/docs/guide/managed-git.md index 7e701cf..3fe7da4 100644 --- a/docs/guide/managed-git.md +++ b/docs/guide/managed-git.md @@ -24,8 +24,8 @@ Repository-Organisation dieser Instanz automatisch prüfen: Organisation mit den Prüfobjekten ab, falls ein Webhook verloren ging. Die Anbindung richtet BreakPilot bei der Bereitstellung ein; Sie müssen nichts -konfigurieren. IT-Administratoren sehen die Anbindung im Arbeitsbereich -(nur lesend). +konfigurieren. IT-Administratoren können sie über die API lesen +(`GET /api/v1/git-source`); eine Ansicht in der Oberfläche folgt. ::: tip Prüfobjekt gelöscht? Ein Prüfobjekt, das zu einem Repository der angebundenen Organisation gehört,