fix(agent): git source pagination ends on an empty page only (#439)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 7m21s
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 7m21s
A Gitea with MAX_RESPONSE_ITEMS below 50 returns short pages; taking the first short page for the last one would have made the sync archive every repository after it. Same for the label listing. A token renewal skips git artifacts without credentials instead of failing, and the guides say the source is readable through the API (no UI yet). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
This commit is contained in:
1 parent
1a9af09bc4
commit
aa90fab2a0
6 files changed
+59
-14
No files matched your search
@@ -128,11 +128,14 @@ impl RepoLister for SourceClient {
|
||||
)));
|
||||
}
|
||||
let batch: Vec<GiteaRepo> = 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<Mutex<Vec<String>>>) -> String {
|
||||
async fn fake_gitea(total: i64, cap: i64, seen: Arc<Mutex<Vec<String>>>) -> 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::<i64>().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!({
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -70,11 +70,12 @@ impl GiteaTracker {
|
||||
let batch: Vec<GiteaLabel> = 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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user