feat(issues): closing or reopening a Prüfwerk issue in Gitea updates the finding (#953) #960

Merged
sharang merged 1 commits from feat/953-gitea-back-sync into main 2026-11-10 16:22:30 +00:00
Owner

Part of epic #955. Until now issues only synced one way, from Prüfwerk to Gitea. This PR carries a person's close or reopen of a Prüfwerk issue in Gitea back to the finding.

Behaviour

The managed source's org webhook now handles issues events with action closed or reopened (git_source/events.rs, pipeline/issue_backsync.rs).

  • Matching: the repository's managed target is found by source_repo (base URL + Gitea repo id). Then the TrackerIssue with external_id == number that belongs to that target: a CVE issue by its cve:<target>: key, a finding issue by the finding's repo_id. Issue numbers repeat across repositories, so the number alone is not enough. An unknown issue is ignored ("issue not opened by Prüfwerk"). An issue event never creates a target.
  • Closed in Gitea: an undecided finding (ISSUE_STATUSES: open, triaged, needs_review) becomes resolved. Its history gets a new entry: StatusChange.tracker = closed_in_tracker, by = "pruefwerk-tracker", by_name = <Gitea login>. No emails are sent. A finding a person already decided in Prüfwerk (for example an accepted risk) keeps that decision; only the issue record is updated. TrackerIssue.status = closed, and TrackerIssue.closed_in_tracker = <login> records who closed it.
  • Reopened in Gitea: a decided finding goes back to open, the same status the scan's "found again" uses. TrackerIssue.status = open, and closed_in_tracker is cleared. If a finding the scan had resolved (no longer found) is reopened in Gitea, the next scan resolves it again. That comes from scan evidence, not a loop.
  • Next scan: nothing new was needed here. finding_store::save_new reopens any resolved finding its scanner reports again (FoundAgain). The status sync then reopens the issue with the existing "Bei der Prüfung erneut gefunden" comment. The issue_sync test covers this after a back-synced close.
  • Both updates are conditional, like the scan's own status changes, so a decision made at the same time is not overwritten.
  • Dashboard: the finding history shows "Im Ticket geschlossen von login" / "Closed in the ticket by login" (and "wieder geöffnet" / "Reopened"). The login also appears as who made the change.

Loop protection

  1. Bot sender ignored: is_own_event(sender, author). Prüfwerk opens its issues as the source's bot user and closes and reopens them as that user. So an event whose sender is the issue's author (issue.user.login, case-insensitive) was caused by Prüfwerk and is answered 200 straight away. The outbound code does not know the bot login, and the bot token cannot read it: GET /api/v1/user needs read:user, and the token only has read:organization, read:repository and write:issue. The issue author stands in for it. Tested in events.rs and end to end.
  2. The back-sync never calls Gitea. The issue record is written before the finding, so IssueAction::done returns early for the outbound sync: no comment, no second close or reopen. Tested in issue_sync.rs.
  3. Token changed to a different bot user: the issue author is the old bot, so a close by the new bot is not recognised as Prüfwerk's own. For that case the "closed" back-sync only sets closed_in_tracker on a record that is not already closed (Prüfwerk records its own close right after the PATCH). The finding is not touched either, because Prüfwerk only closes issues of decided findings. Known edge: an issue Prüfwerk adopted by title that a person had opened. If that same person closes it, the close is taken for the bot's and ignored. That is a missed back-sync, not a loop.

Webhook subscription for existing sources

Prüfwerk does not create the org/system webhook. Operations registers it from webhook_url/webhook_secret (see docs/guide/managed-git*.md), and the bot has no admin rights, so Prüfwerk cannot add the event itself.

  • New sources: the platform GET/PUT /api/v1/platform/tenants/{id}/git-source response now has "webhook_events": ["repository", "push", "issues"], and the docs (DE/EN) list Issues as a required event.
  • Existing sources: one operator step per instance. Tick Issues on the existing webhook in Gitea, or with an admin token: PATCH /api/v1/orgs/{org}/hooks/{id} (for a system hook PATCH /api/v1/admin/hooks/{id}) with {"events": ["repository", "push", "issues"]}. Until that is done no issue deliveries arrive, and nothing breaks.
  • Issue deliveries go through the same automation_allowed gate as push and repository. For a read-only or unentitled tenant they are recorded as Refused, not applied.

CVE issues

When a person closes a CVE issue in Gitea, no VEX status is set, because VEX needs a justification. Only TrackerIssue.status = closed and closed_in_tracker = <login> are recorded. Until now, the next scan reopened a closed issue whose CVE was still present ("Bei der Prüfung erneut gefunden"), which would undo the person's close at once. Now:

  • while the scan still finds the CVE unchanged, an issue with closed_in_tracker stays closed and nothing is sent;
  • once a complete scan no longer finds the CVE, the marker is cleared (no Gitea call; the issue is already closed). If the CVE comes back later, the issue is reopened as before;
  • a VEX statement withdrawn while the marker is set also leaves the issue closed;
  • reopening in Gitea clears the marker.
    Changed in two places in sync_cve_issues, plus a lifecycle test.

How tested

  • cargo fmt --all --check; clippy -D warnings on compliance-core, compliance-agent (all targets) and compliance-dashboard (server and web).
  • Unit tests: cargo test -p compliance-agent --lib with filters issue / git_source / events / webhook / cve_issues (48 passed), compliance-core finding, compliance-dashboard finding_detail.
  • Mongo tests run locally against mongod 7.0.45, the build CI uses: issue_backsync::closing_and_reopening_in_gitea_moves_the_finding, the extended issue_sync::an_issue_follows_its_findings_status (no outbound call after a back-synced close; "erneut gefunden" reopen after FoundAgain), and the extended cve_issues::severe_cves_open_issues_that_close_when_the_dependency_is_fixed. These lib Mongo tests skip in CI's --lib job (no Mongo there), like the existing ones in those modules.
  • In CI (agent-integration job): tests/git_source.rs::an_issue_closed_or_reopened_in_gitea_moves_its_finding runs end to end through the signed webhook. It covers a bot close ignored (200, no change), a person's close (202, finding resolved with the tracker history entry, record closed with the login, delivery recorded), a reopen (finding open, marker cleared) and webhook_events in the platform response. Also re-run locally: resolve_findings, group_decisions, group_findings, manual_findings, vex_lifecycle.

Open points

  • The Tickets page and the CVE alert UI do not show "ticket closed by X" yet. closed_in_tracker is stored and ready for that; those files are owned by #946/#949.
  • Moving a closed CVE into Heute's "KI/Prüfen" bucket belongs to #950.
  • There is no fallback if the Issues event is not subscribed: deliveries simply do not arrive, and nothing detects that.

Closes #953

🤖 Generated with Claude Code

Part of epic #955. Until now issues only synced one way, from Prüfwerk to Gitea. This PR carries a person's close or reopen of a Prüfwerk issue in Gitea back to the finding. ## Behaviour The managed source's org webhook now handles `issues` events with action `closed` or `reopened` (`git_source/events.rs`, `pipeline/issue_backsync.rs`). - **Matching:** the repository's managed target is found by `source_repo` (base URL + Gitea repo id). Then the `TrackerIssue` with `external_id == number` that belongs to that target: a CVE issue by its `cve:<target>:` key, a finding issue by the finding's `repo_id`. Issue numbers repeat across repositories, so the number alone is not enough. An unknown issue is ignored ("issue not opened by Prüfwerk"). An issue event never creates a target. - **Closed in Gitea:** an undecided finding (`ISSUE_STATUSES`: open, triaged, needs_review) becomes `resolved`. Its history gets a new entry: `StatusChange.tracker = closed_in_tracker`, `by = "pruefwerk-tracker"`, `by_name = <Gitea login>`. No emails are sent. A finding a person already decided in Prüfwerk (for example an accepted risk) keeps that decision; only the issue record is updated. `TrackerIssue.status = closed`, and `TrackerIssue.closed_in_tracker = <login>` records who closed it. - **Reopened in Gitea:** a decided finding goes back to `open`, the same status the scan's "found again" uses. `TrackerIssue.status = open`, and `closed_in_tracker` is cleared. If a finding the scan had resolved (no longer found) is reopened in Gitea, the next scan resolves it again. That comes from scan evidence, not a loop. - **Next scan:** nothing new was needed here. `finding_store::save_new` reopens any `resolved` finding its scanner reports again (`FoundAgain`). The status sync then reopens the issue with the existing "Bei der Prüfung erneut gefunden" comment. The issue_sync test covers this after a back-synced close. - Both updates are conditional, like the scan's own status changes, so a decision made at the same time is not overwritten. - **Dashboard:** the finding history shows "Im Ticket geschlossen von *login*" / "Closed in the ticket by *login*" (and "wieder geöffnet" / "Reopened"). The login also appears as who made the change. ## Loop protection 1. **Bot sender ignored:** `is_own_event(sender, author)`. Prüfwerk opens its issues as the source's bot user and closes and reopens them as that user. So an event whose sender is the issue's author (`issue.user.login`, case-insensitive) was caused by Prüfwerk and is answered `200` straight away. The outbound code does not know the bot login, and the bot token cannot read it: `GET /api/v1/user` needs `read:user`, and the token only has `read:organization`, `read:repository` and `write:issue`. The issue author stands in for it. Tested in `events.rs` and end to end. 2. **The back-sync never calls Gitea.** The issue record is written before the finding, so `IssueAction::done` returns early for the outbound sync: no comment, no second close or reopen. Tested in `issue_sync.rs`. 3. **Token changed to a different bot user:** the issue author is the old bot, so a close by the new bot is not recognised as Prüfwerk's own. For that case the "closed" back-sync only sets `closed_in_tracker` on a record that is not already closed (Prüfwerk records its own close right after the PATCH). The finding is not touched either, because Prüfwerk only closes issues of decided findings. Known edge: an issue Prüfwerk *adopted by title* that a person had opened. If that same person closes it, the close is taken for the bot's and ignored. That is a missed back-sync, not a loop. ## Webhook subscription for existing sources Prüfwerk does not create the org/system webhook. Operations registers it from `webhook_url`/`webhook_secret` (see `docs/guide/managed-git*.md`), and the bot has no admin rights, so Prüfwerk cannot add the event itself. - **New sources:** the platform `GET`/`PUT /api/v1/platform/tenants/{id}/git-source` response now has `"webhook_events": ["repository", "push", "issues"]`, and the docs (DE/EN) list **Issues** as a required event. - **Existing sources: one operator step per instance.** Tick **Issues** on the existing webhook in Gitea, or with an admin token: `PATCH /api/v1/orgs/{org}/hooks/{id}` (for a system hook `PATCH /api/v1/admin/hooks/{id}`) with `{"events": ["repository", "push", "issues"]}`. Until that is done no issue deliveries arrive, and nothing breaks. - Issue deliveries go through the same `automation_allowed` gate as push and repository. For a read-only or unentitled tenant they are recorded as Refused, not applied. ## CVE issues When a person closes a CVE issue in Gitea, **no VEX status is set**, because VEX needs a justification. Only `TrackerIssue.status = closed` and `closed_in_tracker = <login>` are recorded. Until now, the next scan reopened a closed issue whose CVE was still present ("Bei der Prüfung erneut gefunden"), which would undo the person's close at once. Now: - while the scan still finds the CVE unchanged, an issue with `closed_in_tracker` stays closed and nothing is sent; - once a **complete** scan no longer finds the CVE, the marker is cleared (no Gitea call; the issue is already closed). If the CVE comes back later, the issue is reopened as before; - a VEX statement withdrawn while the marker is set also leaves the issue closed; - reopening in Gitea clears the marker. Changed in two places in `sync_cve_issues`, plus a lifecycle test. ## How tested - `cargo fmt --all --check`; clippy `-D warnings` on `compliance-core`, `compliance-agent` (all targets) and `compliance-dashboard` (`server` and `web`). - Unit tests: `cargo test -p compliance-agent --lib` with filters issue / git_source / events / webhook / cve_issues (48 passed), `compliance-core` finding, `compliance-dashboard` finding_detail. - Mongo tests run locally against mongod 7.0.45, the build CI uses: `issue_backsync::closing_and_reopening_in_gitea_moves_the_finding`, the extended `issue_sync::an_issue_follows_its_findings_status` (no outbound call after a back-synced close; "erneut gefunden" reopen after FoundAgain), and the extended `cve_issues::severe_cves_open_issues_that_close_when_the_dependency_is_fixed`. **These lib Mongo tests skip in CI's `--lib` job** (no Mongo there), like the existing ones in those modules. - **In CI** (agent-integration job): `tests/git_source.rs::an_issue_closed_or_reopened_in_gitea_moves_its_finding` runs end to end through the signed webhook. It covers a bot close ignored (200, no change), a person's close (202, finding resolved with the tracker history entry, record closed with the login, delivery recorded), a reopen (finding open, marker cleared) and `webhook_events` in the platform response. Also re-run locally: `resolve_findings`, `group_decisions`, `group_findings`, `manual_findings`, `vex_lifecycle`. ## Open points - The Tickets page and the CVE alert UI do not show "ticket closed by X" yet. `closed_in_tracker` is stored and ready for that; those files are owned by #946/#949. - Moving a closed CVE into Heute's "KI/Prüfen" bucket belongs to #950. - There is no fallback if the Issues event is not subscribed: deliveries simply do not arrive, and nothing detects that. Closes #953 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang added 1 commit 2026-11-10 16:01:25 +00:00
feat(issues): closing or reopening a Prüfwerk issue in Gitea updates the finding (#953)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 3m58s
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 20m26s
a3bd43593f
The managed Git source's org webhook now handles `issues` events. A
person closing an issue Prüfwerk opened resolves the undecided finding,
recorded as "closed in the ticket" with the Gitea login; reopening it
makes the finding open again. A decision already made in Prüfwerk is
kept. Events whose sender is the issue's author (Prüfwerk's bot user,
which opened it) are ignored, and the back-sync never calls Gitea, so
nothing loops; the issue record is updated first, so the outbound sync
has nothing left to comment or close.

CVE issues closed in Gitea only record the close and who did it, no VEX
status. The scan leaves such an issue closed while the CVE is still
found, and forgets the close once a complete scan no longer finds it, so
a CVE that comes back reopens the issue as before.

The platform API names the events the webhook needs (`webhook_events`);
Prüfwerk cannot change the webhook itself, so existing webhooks need
the Issues event added in Gitea. The finding history shows
"Im Ticket geschlossen von <login>".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang force-pushed feat/953-gitea-back-sync from 8c13fbaced to a3bd43593f 2026-11-10 16:01:25 +00:00 Compare
sharang merged commit f1123e1adb into main 2026-11-10 16:22:30 +00:00
Sign in to join this conversation.