From 533d5f35b5bec5af2bd7af5af1f35147586a3d86 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Wed, 4 Nov 2026 08:50:51 +0000 Subject: [PATCH] fix(issues): a skipped scan still catches up the deferred issues (#826) --- .../src/pipeline/issue_catchup.rs | 50 +++++++++++++++---- .../src/pipeline/issue_creation.rs | 29 +++++++++++ compliance-agent/src/pipeline/orchestrator.rs | 2 + docs/guide/issues.md | 2 +- 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/compliance-agent/src/pipeline/issue_catchup.rs b/compliance-agent/src/pipeline/issue_catchup.rs index 156b944..9ac22c1 100644 --- a/compliance-agent/src/pipeline/issue_catchup.rs +++ b/compliance-agent/src/pipeline/issue_catchup.rs @@ -9,6 +9,10 @@ //! most [`MAX_NEW_ISSUES`] per scan, highest severity and oldest first. The //! rest wait for the next scans. Gitea only: it matches against the listing //! the scan made anyway; the other trackers would need a search per finding. +//! +//! A scheduled scan that change detection skips (no new commits) still runs +//! the catch-up when findings are [`waiting`] for an issue, so a backlog +//! does not wait for the next push. use std::collections::{BTreeMap, HashSet}; @@ -119,26 +123,50 @@ pub(super) async fn catch_up( Ok(run(sink, &candidates, index, mode).await) } -/// The target's open medium+ findings without an issue, as stored. -async fn load_candidates(sink: &IssueSink<'_>) -> Result, AgentError> { +/// The target's open medium+ findings without an issue, as a query. +fn candidate_filter(repo_id: &str) -> mongodb::bson::Document { let severities: Vec = ISSUE_SEVERITIES.iter().map(Severity::to_string).collect(); let statuses: Vec = ISSUE_STATUSES.iter().map(ToString::to_string).collect(); // `tracker_issue_url: null` matches a missing field too. + doc! { + "repo_id": repo_id, + "tracker_issue_url": null, + "severity": { "$in": severities }, + "status": { "$in": statuses }, + } +} + +/// The target's open medium+ findings without an issue, as stored. +async fn load_candidates(sink: &IssueSink<'_>) -> Result, AgentError> { let findings = sink .db .findings() - .find(doc! { - "repo_id": sink.repo_id, - "tracker_issue_url": null, - "severity": { "$in": severities }, - "status": { "$in": statuses }, - }) + .find(candidate_filter(sink.repo_id)) .await? .try_collect() .await?; Ok(findings) } +/// Whether target `repo_id` has findings waiting for an issue: one query, +/// so a skipped scan with nothing to catch up never lists the tracker. +/// +/// # Errors +/// +/// The findings cannot be read. +pub(super) async fn waiting( + db: &crate::database::Database, + repo_id: &str, +) -> Result { + let one = db + .findings() + .clone_with_type::() + .find_one(candidate_filter(repo_id)) + .projection(doc! { "_id": 1 }) + .await?; + Ok(one.is_some()) +} + /// The findings that want an issue and have none, highest severity first, /// then oldest first. fn select(findings: Vec) -> Vec { @@ -515,6 +543,8 @@ mod tests { } }; + assert!(waiting(&db, "target-1").await.unwrap()); + // A dry run changes nothing. let dry = catch_up(&sink, &index, IssueCatchup::DryRun).await.unwrap(); assert_eq!((dry.created.total(), dry.linked.total()), (1, 1)); @@ -554,10 +584,12 @@ mod tests { .expect("the link is recorded for the status sync"); assert_eq!(record.external_id, "7"); - // The next scan finds nothing left to do. + // The next scan finds nothing left to do, and a skipped one would + // not even list the tracker. let again = catch_up(&sink, &index, IssueCatchup::On).await.unwrap(); assert_eq!((again.created.total(), again.linked.total()), (0, 0)); assert_eq!(posts(&seen), 1); + assert!(!waiting(&db, "target-1").await.unwrap()); pool.drop_tenant("issue-catchup").await.ok(); } } diff --git a/compliance-agent/src/pipeline/issue_creation.rs b/compliance-agent/src/pipeline/issue_creation.rs index efd510e..0576229 100644 --- a/compliance-agent/src/pipeline/issue_creation.rs +++ b/compliance-agent/src/pipeline/issue_creation.rs @@ -160,6 +160,35 @@ impl PipelineOrchestrator { } } +impl PipelineOrchestrator { + /// The issue catch-up for a scan that change detection skipped: without + /// it, findings deferred by [`crate::pipeline::issue_catchup::MAX_NEW_ISSUES`] + /// wait for the next push. Runs only with the catch-up on (or in dry + /// run), on a Gitea tracker, and when findings are waiting for an issue, + /// so an idle repository costs one query and no tracker request. A + /// failure is logged; the skipped scan stays skipped. + pub(super) async fn catch_up_unchanged(&self, repo: &RepoView, repo_id: &str) { + if self.config.issue_catchup == IssueCatchup::Off { + return; + } + let Some(TrackerDispatch::Gitea(_)) = self.build_tracker(repo) else { + return; + }; + match crate::pipeline::issue_catchup::waiting(&self.db, repo_id).await { + Ok(true) => {} + Ok(false) => return, + Err(e) => { + tracing::warn!("[{repo_id}] Issue catch-up check failed: {e}"); + return; + } + } + tracing::info!("[{repo_id}] Unchanged repository: issue catch-up only"); + if let Err(e) = self.create_tracker_issues(repo, repo_id, &[]).await { + tracing::warn!("[{repo_id}] Issue catch-up failed: {e}"); + } + } +} + /// What opening, reopening and closing a target's CVE issues needs: the /// tracker configuration, where tracker calls may connect, and the tenant's /// database. Shared by the scans and the CVE monitor. diff --git a/compliance-agent/src/pipeline/orchestrator.rs b/compliance-agent/src/pipeline/orchestrator.rs index 56b40c2..74c34ea 100644 --- a/compliance-agent/src/pipeline/orchestrator.rs +++ b/compliance-agent/src/pipeline/orchestrator.rs @@ -190,6 +190,8 @@ impl PipelineOrchestrator { ), ) .await; + // Findings deferred by the catch-up's cap still get their issues. + self.catch_up_unchanged(repo, &repo_id).await; return Ok(0); } diff --git a/docs/guide/issues.md b/docs/guide/issues.md index b97630c..d86da11 100644 --- a/docs/guide/issues.md +++ b/docs/guide/issues.md @@ -15,7 +15,7 @@ This means every actionable finding gets tracked in the same system your develop ### Catch-up for findings without an issue (Gitea) -A scan opens issues only for the findings it finds new, so a finding whose issue could not be opened at the time (Gitea unreachable, the issue refused) stays without one. With `ISSUE_CATCHUP=on`, every scan of a Gitea target then also goes through its open, triaged and to-review findings of severity medium or above that have no issue: one whose issue exists (found by the fingerprint in its body, then by title; a grouped finding by fingerprint only) is linked to it, the others get a new issue. At most 20 new issues per target and scan, highest severity and oldest first; the rest follow in the next scans. `ISSUE_CATCHUP=dry_run` only logs how many issues it would open and link, per severity, and changes nothing. The default is `off`; GitHub, GitLab and Jira are not caught up. +A scan opens issues only for the findings it finds new, so a finding whose issue could not be opened at the time (Gitea unreachable, the issue refused) stays without one. With `ISSUE_CATCHUP=on`, every scan of a Gitea target then also goes through its open, triaged and to-review findings of severity medium or above that have no issue: one whose issue exists (found by the fingerprint in its body, then by title; a grouped finding by fingerprint only) is linked to it, the others get a new issue. At most 20 new issues per target and scan, highest severity and oldest first; the rest follow in the next scans. A scheduled scan that is skipped because the repository has no new commits still runs the catch-up when findings are waiting for an issue, so the rest follow every scan interval, not only after the next push. `ISSUE_CATCHUP=dry_run` only logs how many issues it would open and link, per severity, and changes nothing. The default is `off`; GitHub, GitLab and Jira are not caught up. ### CVE issues (managed repositories)