From f51cd31da7e840edf341a5fae5b4907b673cb6f0 Mon Sep 17 00:00:00 2001 From: kert Date: Mon, 16 Nov 2026 15:45:10 -0500 Subject: [PATCH] fix(ci): keep one open issue per failed job api.diag.ci opened a new issue on every red run and never closed it. A repeat now comments on the open issue and closes older copies. A green run closes that issue. The workflow step no longer syncs the repo just to file or close. --- .gitea/workflows/bcda-vendor.yml | 23 +- .gitea/workflows/ci.yml | 46 +- .gitea/workflows/comment-pull.yml | 23 +- .gitea/workflows/deploy.yml | 186 +++++--- .gitea/workflows/harden.yml | 23 +- .gitea/workflows/infra-ci.yml | 161 +++++-- .gitea/workflows/llm-golden.yml | 23 +- .gitea/workflows/notebooks-integration.yml | 23 +- .gitea/workflows/pkg-supply-chain.yml | 18 +- .gitea/workflows/rebuild-all.yml | 23 +- .gitea/workflows/release.yml | 23 +- .gitea/workflows/zotero-sync.yml | 23 +- dev/scripts/backends/gitea.py | 67 +-- dev/scripts/backends/github.py | 28 +- src/api/clients/gitea/client.py | 13 + src/api/diag/ci.py | 493 ++++++++++++++++++--- tests/api/test_diag_ci.py | 293 ++++++++++++ 17 files changed, 1225 insertions(+), 264 deletions(-) diff --git a/.gitea/workflows/bcda-vendor.yml b/.gitea/workflows/bcda-vendor.yml index 964dd70..54e45bc 100644 --- a/.gitea/workflows/bcda-vendor.yml +++ b/.gitea/workflows/bcda-vendor.yml @@ -31,13 +31,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "BCDA Vendor" --job "bcda-vendor" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "BCDA Vendor" --job "bcda-vendor" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index aa95f7f..850da40 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -66,13 +66,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "CI" --job "test" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "CI" --job "test" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -113,13 +122,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "CI" --job "notebooks-smoke" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "CI" --job "notebooks-smoke" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/comment-pull.yml b/.gitea/workflows/comment-pull.yml index e247b8b..62826ca 100644 --- a/.gitea/workflows/comment-pull.yml +++ b/.gitea/workflows/comment-pull.yml @@ -42,13 +42,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Comment Pull" --job "comment-pull" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Comment Pull" --job "comment-pull" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 85c84fd..2fbf8f9 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -138,13 +138,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Deploy" --job "notebooks" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Deploy" --job "notebooks" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -196,13 +205,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Deploy" --job "zotero" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Deploy" --job "zotero" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -254,13 +272,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Deploy" --job "docs" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Deploy" --job "docs" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -312,13 +339,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Deploy" --job "api" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Deploy" --job "api" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -370,13 +406,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Deploy" --job "bcda" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Deploy" --job "bcda" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -428,13 +473,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Deploy" --job "llm" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Deploy" --job "llm" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -486,13 +540,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Deploy" --job "mc" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Deploy" --job "mc" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -506,11 +569,6 @@ jobs: - name: Checkout uses: https://github.com/actions/checkout@v4 - - name: Set up uv - run: curl -LsSf https://astral.sh/uv/install.sh | sh - env: - UV_INSTALL_DIR: /usr/local/bin - - name: Report # Only file an issue if at least one upstream build job failed. # Without this gate api.diag.ci runs unconditionally and files @@ -519,8 +577,24 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Deploy" --job "report" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + # This job runs on a green deploy too. Close Deploy/report only + # when nothing upstream failed. Per-image jobs close their own. + if: ${{ !contains(needs.*.result, 'failure') }} + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Deploy" --job "report" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/harden.yml b/.gitea/workflows/harden.yml index c4660d6..6c534ca 100644 --- a/.gitea/workflows/harden.yml +++ b/.gitea/workflows/harden.yml @@ -181,13 +181,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Harden" --job "build-scan-report" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Harden" --job "build-scan-report" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/infra-ci.yml b/.gitea/workflows/infra-ci.yml index 02d5e7a..bfd3f60 100644 --- a/.gitea/workflows/infra-ci.yml +++ b/.gitea/workflows/infra-ci.yml @@ -72,13 +72,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Infra CI" --job "notebooks" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Infra CI" --job "notebooks" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -105,13 +114,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Infra CI" --job "zotero" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Infra CI" --job "zotero" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -138,13 +156,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Infra CI" --job "docs" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Infra CI" --job "docs" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -171,13 +198,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Infra CI" --job "api" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Infra CI" --job "api" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -204,13 +240,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Infra CI" --job "bcda" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Infra CI" --job "bcda" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -237,13 +282,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Infra CI" --job "llm" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Infra CI" --job "llm" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ @@ -270,13 +324,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Infra CI" --job "mc" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Infra CI" --job "mc" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/llm-golden.yml b/.gitea/workflows/llm-golden.yml index 235ecfd..8ca1d47 100644 --- a/.gitea/workflows/llm-golden.yml +++ b/.gitea/workflows/llm-golden.yml @@ -54,13 +54,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "LLM Golden" --job "llm-golden" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "LLM Golden" --job "llm-golden" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/notebooks-integration.yml b/.gitea/workflows/notebooks-integration.yml index 8ce19ad..f4056e7 100644 --- a/.gitea/workflows/notebooks-integration.yml +++ b/.gitea/workflows/notebooks-integration.yml @@ -49,13 +49,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Notebooks Integration" --job "notebooks-integration" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Notebooks Integration" --job "notebooks-integration" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/pkg-supply-chain.yml b/.gitea/workflows/pkg-supply-chain.yml index d4ddfd8..7eb5c05 100644 --- a/.gitea/workflows/pkg-supply-chain.yml +++ b/.gitea/workflows/pkg-supply-chain.yml @@ -65,8 +65,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Package Supply Chain" --job "pkg-supply-chain" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Package Supply Chain" --job "pkg-supply-chain" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/rebuild-all.yml b/.gitea/workflows/rebuild-all.yml index 238f8e5..fed6e35 100644 --- a/.gitea/workflows/rebuild-all.yml +++ b/.gitea/workflows/rebuild-all.yml @@ -136,13 +136,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Rebuild All" --job "build-scan-report" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Rebuild All" --job "build-scan-report" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 4667fe8..3ab97d6 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -34,13 +34,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Release" --job "release" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Release" --job "release" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/.gitea/workflows/zotero-sync.yml b/.gitea/workflows/zotero-sync.yml index 6c652ad..9cee221 100644 --- a/.gitea/workflows/zotero-sync.yml +++ b/.gitea/workflows/zotero-sync.yml @@ -51,13 +51,22 @@ jobs: env: GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \ + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --workflow "Zotero Sync" --job "zotero-sync" \ + --run "${{ github.run_number }}" \ + --sha "${{ github.sha }}" \ + --ref "${{ github.ref }}" || true + - name: Close resolved issue + if: success() && !startsWith(github.head_ref || github.ref_name, 'renovate/') + env: + GITEA_TOKEN: ${{ secrets.DEPLOY_TOKEN }} + run: | + py=python3 + command -v python3 >/dev/null 2>&1 || { command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; } + $py src/api/diag/ci.py \ + --close \ --workflow "Zotero Sync" --job "zotero-sync" \ --run "${{ github.run_number }}" \ --sha "${{ github.sha }}" \ diff --git a/dev/scripts/backends/gitea.py b/dev/scripts/backends/gitea.py index 8c6f9a5..4b9cec0 100644 --- a/dev/scripts/backends/gitea.py +++ b/dev/scripts/backends/gitea.py @@ -117,31 +117,43 @@ def _trivy_step( # ── Failure reporting ───────────────────────────────────────────── -def _failure_step(workflow_name: str, job_name: str) -> str: - """Emit an if:failure() step that files a Gitea issue. - - Not for Renovate's branches: Renovate holds a red branch back and shows - it on its Dependency Dashboard, so an issue per failed job is noise. - """ +def _filer_run(workflow_name: str, job_name: str, *, close: bool = False) -> str: + """Shell that runs the filer. python3 is on the act image; no uv sync.""" + flag = "--close \\\n " if close else "" return f"""\ - - name: File failure issue - if: failure() && !startsWith(github.head_ref || github.ref_name, 'renovate/') - env: - GITEA_TOKEN: ${{{{ secrets.DEPLOY_TOKEN }}}} - run: | - # Jobs that never install uv (image builds, infra checks) still - # need it here — otherwise the filer dies with "uv: command not - # found" and the failure is never filed. - command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh - export PATH="$HOME/.local/bin:$PATH" - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \\ - --workflow "{workflow_name}" --job "{job_name}" \\ + py=python3 + command -v python3 >/dev/null 2>&1 || {{ command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; }} + $py src/api/diag/ci.py \\ + {flag}--workflow "{workflow_name}" --job "{job_name}" \\ --run "${{{{ github.run_number }}}}" \\ --sha "${{{{ github.sha }}}}" \\ --ref "${{{{ github.ref }}}}" || true""" +def _failure_step(workflow_name: str, job_name: str) -> str: + """Emit file-on-failure and close-on-success steps. + + Not for Renovate's branches: Renovate holds a red branch back and shows + it on its Dependency Dashboard, so an issue per failed job is noise. + One open issue per job: the failure step comments on it, the success + step closes it. + """ + gate = "!startsWith(github.head_ref || github.ref_name, 'renovate/')" + return f"""\ + - name: File failure issue + if: failure() && {gate} + env: + GITEA_TOKEN: ${{{{ secrets.DEPLOY_TOKEN }}}} + run: | +{_filer_run(workflow_name, job_name)} + - name: Close resolved issue + if: success() && {gate} + env: + GITEA_TOKEN: ${{{{ secrets.DEPLOY_TOKEN }}}} + run: | +{_filer_run(workflow_name, job_name, close=True)}""" + + # ── Gitea-specific helpers ─────────────────────────────────────── @@ -444,8 +456,6 @@ jobs: steps: {_checkout_step()} -{_setup_uv_step(uv_version)} - - name: Report # Only file an issue if at least one upstream build job failed. # Without this gate api.diag.ci runs unconditionally and files @@ -454,12 +464,15 @@ jobs: env: GITEA_TOKEN: ${{{{ secrets.DEPLOY_TOKEN }}}} run: | - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \\ - --workflow "Deploy" --job "report" \\ - --run "${{{{ github.run_number }}}}" \\ - --sha "${{{{ github.sha }}}}" \\ - --ref "${{{{ github.ref }}}}" || true +{_filer_run("Deploy", "report")} + - name: Close resolved issue + # This job runs on a green deploy too. Close Deploy/report only + # when nothing upstream failed. Per-image jobs close their own. + if: ${{{{ !contains(needs.*.result, 'failure') }}}} + env: + GITEA_TOKEN: ${{{{ secrets.DEPLOY_TOKEN }}}} + run: | +{_filer_run("Deploy", "report", close=True)} """ return (".gitea/workflows/deploy.yml", content) diff --git a/dev/scripts/backends/github.py b/dev/scripts/backends/github.py index 205db21..266da11 100644 --- a/dev/scripts/backends/github.py +++ b/dev/scripts/backends/github.py @@ -110,20 +110,34 @@ def _build_push_step( # ── Failure reporting ───────────────────────────────────────────── +def _filer_run(workflow_name: str, job_name: str, *, close: bool = False) -> str: + """Shell that runs the filer. python3 is on the runner; no uv sync.""" + flag = "--close \\\n " if close else "" + return f"""\ + py=python3 + command -v python3 >/dev/null 2>&1 || {{ command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh; export PATH="$HOME/.local/bin:$PATH"; py="uv run --no-project python"; }} + $py src/api/diag/ci.py \\ + {flag}--workflow "{workflow_name}" --job "{job_name}" \\ + --run "${{{{ github.run_number }}}}" \\ + --sha "${{{{ github.sha }}}}" \\ + --ref "${{{{ github.ref }}}}" || true""" + + def _failure_step(workflow_name: str, job_name: str) -> str: - """Emit an if:failure() step that files a Gitea issue.""" + """Emit file-on-failure and close-on-success steps.""" return f"""\ - name: File failure issue if: failure() env: GITEA_TOKEN: ${{{{ secrets.GITEA_TOKEN }}}} run: | - uv sync --no-dev --quiet 2>/dev/null || true - uv run python -m api.diag.ci \\ - --workflow "{workflow_name}" --job "{job_name}" \\ - --run "${{{{ github.run_number }}}}" \\ - --sha "${{{{ github.sha }}}}" \\ - --ref "${{{{ github.ref }}}}" || true""" +{_filer_run(workflow_name, job_name)} + - name: Close resolved issue + if: success() + env: + GITEA_TOKEN: ${{{{ secrets.GITEA_TOKEN }}}} + run: | +{_filer_run(workflow_name, job_name, close=True)}""" # ── Workflow generators ─────────────────────────────────────────── diff --git a/src/api/clients/gitea/client.py b/src/api/clients/gitea/client.py index bdec7ca..4c1247b 100644 --- a/src/api/clients/gitea/client.py +++ b/src/api/clients/gitea/client.py @@ -116,6 +116,19 @@ class GiteaClient(Client): def create_issue(self, owner: str, repo: str, body: dict) -> dict: return self.post(f"/repos/{owner}/{repo}/issues", json=body).json() + def list_issues(self, owner: str, repo: str, **params: object) -> list[dict]: + query = {k: str(v) for k, v in params.items()} + return self.get(f"/repos/{owner}/{repo}/issues", params=query).json() + + def comment_issue(self, owner: str, repo: str, index: int, body: str) -> dict: + return self.post( + f"/repos/{owner}/{repo}/issues/{index}/comments", + json={"body": body}, + ).json() + + def update_issue(self, owner: str, repo: str, index: int, body: dict) -> dict: + return self.patch(f"/repos/{owner}/{repo}/issues/{index}", json=body).json() + def list_labels(self, owner: str, repo: str) -> list[dict]: return self.get(f"/repos/{owner}/{repo}/labels", params={"limit": 50}).json() diff --git a/src/api/diag/ci.py b/src/api/diag/ci.py index 847f5ac..24d0579 100644 --- a/src/api/diag/ci.py +++ b/src/api/diag/ci.py @@ -1,14 +1,24 @@ """Backend-agnostic CI failure reporter. -Files a Gitea issue when any CI workflow step fails, regardless of -whether the CI backend is Gitea Actions or GitHub Actions. +One open Gitea issue per workflow and job. A repeated failure comments on +that issue and closes older duplicates. A later success closes it. The +previous one-issue-per-run behavior never closed anything, so a single +workflow accumulated dozens of copies. + +The HTTP path prefers :class:`api.clients.gitea.GiteaClient` and falls +back to the stdlib when the project env is not installed, so a workflow +step can run it with ``python3`` and no ``uv sync``. Usage from workflow YAML:: - uv run python -m api.diag.ci \\ + python3 src/api/diag/ci.py \\ --workflow "Deploy" --job "build-scan-report" \\ --run "42" --sha "abc1234" --ref "refs/heads/main" + python3 src/api/diag/ci.py --close \\ + --workflow "Deploy" --job "build-scan-report" \\ + --run "43" --sha "def5678" --ref "refs/heads/main" + All arguments are passed explicitly by the workflow step so the module doesn't need to know which CI system is running. """ @@ -18,10 +28,24 @@ from __future__ import annotations import argparse import logging import os +import re +import socket import sys +import tomllib +import urllib.error +import urllib.parse +import urllib.request +from pathlib import Path log = logging.getLogger(__name__) +# Body token. Dedup and auto-close key on it. Bounded so ``CI/test`` does +# not match ``CI/testing``. Legacy issues have no token; their title prefix +# ``ci/{workflow}/{job} failed`` is the other key. +_MARKER = "ci-key:" +_PAGE_LIMIT = 50 +_MAX_PAGES = 20 + def _get_token() -> str: token = os.environ.get("GITEA_TOKEN", "") @@ -39,6 +63,247 @@ def _truncate(s: str, n: int) -> str: return s if len(s) <= n else s[: n - 3] + "..." +def _marker(workflow: str, job: str) -> str: + return f"{_MARKER}{workflow}/{job}" + + +def _title_prefix(workflow: str, job: str) -> str: + return f"ci/{workflow}/{job} failed" + + +def _has_marker(body: str, workflow: str, job: str) -> bool: + token = re.escape(_marker(workflow, job)) + return re.search(rf"{token}(?![A-Za-z0-9_./-])", body or "") is not None + + +def _is_match(issue: dict, workflow: str, job: str) -> bool: + """True when *issue* is the open failure for this workflow and job.""" + title = issue.get("title") or "" + body = issue.get("body") or "" + if _has_marker(body, workflow, job): + return True + return title.startswith(_title_prefix(workflow, job)) + + +def _with_marker(body: str, workflow: str, job: str) -> str: + if _has_marker(body, workflow, job): + return body + suffix = ( + f"`{_marker(workflow, job)}` — do not edit; dedup and auto-close key on it." + ) + text = (body or "").rstrip() + return f"{text}\n\n{suffix}" if text else suffix + + +def _title(workflow: str, job: str, run: str, sha: str) -> str: + sha_short = sha[:8] if sha else "unknown" + return _truncate(f"{_title_prefix(workflow, job)} (#{run} @ {sha_short})", 120) + + +def _label_names(workflow: str) -> list[str]: + names = ["ci"] + wf_lower = workflow.lower() + for keyword in ("deploy", "harden", "ci", "infra", "release", "rebuild"): + if keyword in wf_lower: + names.append(f"pipeline:{keyword}") + break + return names + + +def _sha_short(sha: str) -> str: + return sha[:8] if sha else "unknown" + + +def _failure_note(run: str, sha: str, ref: str) -> str: + lines = [ + "Failed again.", + "", + f"- **Run:** #{run}", + f"- **Commit:** `{_sha_short(sha)}`", + f"- **Ref:** `{ref}`", + ] + step = os.environ.get("GITHUB_ACTION", "") + if step: + lines.append(f"- **Step:** `{step}`") + return "\n".join(lines) + + +def _api_base() -> str: + """Gitea API root. Env, then ``stack.toml``'s compose URL when it resolves.""" + env = os.environ.get("GITEA_API_BASE") or os.environ.get("GITEA_API_URL") or "" + if env: + return env.rstrip("/") + toml_path = Path(__file__).resolve().parents[3] / "stack.toml" + if not toml_path.is_file(): + return "https://git.fhirworx.io/api/v1" + data = tomllib.loads(toml_path.read_text()) + internal = str(data["services"]["git"]).rstrip("/") + "/api/v1" + host = urllib.parse.urlsplit(internal).hostname or "" + try: + socket.getaddrinfo(host, None) + except OSError: + domain = data["platform"]["domain"] + return f"https://git.{domain}/api/v1" + return internal + + +class _UrllibGitea: + """Stdlib Gitea issues client. Used when httpx and conf are not installed.""" + + def __init__(self, token: str, base: str) -> None: + self._token = token + self._base = base.rstrip("/") + + def close(self) -> None: + return None + + def _req( + self, + method: str, + path: str, + body: dict | None = None, + params: dict | None = None, + ) -> object: + import json + + url = self._base + path + if params: + url += "?" + urllib.parse.urlencode({k: str(v) for k, v in params.items()}) + data = json.dumps(body).encode() if body is not None else None + req = urllib.request.Request( + url, + data=data, + method=method, + headers={ + "Authorization": f"token {self._token}", + "Content-Type": "application/json", + "Accept": "application/json", + }, + ) + try: + with urllib.request.urlopen(req, timeout=30) as resp: + raw = resp.read().decode() + except urllib.error.HTTPError as exc: + detail = exc.read().decode(errors="replace")[:300] + raise RuntimeError( + f"Gitea {method} {path} -> {exc.code}: {detail}" + ) from exc + return json.loads(raw) if raw else None + + def list_issues(self, owner: str, repo: str, **params: object) -> list[dict]: + result = self._req("GET", f"/repos/{owner}/{repo}/issues", params=dict(params)) + if not isinstance(result, list): + raise RuntimeError(f"Gitea issue list was not a list: {result!r}") + return result + + def create_issue(self, owner: str, repo: str, body: dict) -> dict: + result = self._req("POST", f"/repos/{owner}/{repo}/issues", body) + if not isinstance(result, dict): + raise RuntimeError(f"Gitea create issue failed: {result!r}") + return result + + def comment_issue(self, owner: str, repo: str, index: int, body: str) -> dict: + result = self._req( + "POST", + f"/repos/{owner}/{repo}/issues/{index}/comments", + {"body": body}, + ) + if not isinstance(result, dict): + raise RuntimeError(f"Gitea comment failed: {result!r}") + return result + + def update_issue(self, owner: str, repo: str, index: int, body: dict) -> dict: + result = self._req("PATCH", f"/repos/{owner}/{repo}/issues/{index}", body) + if not isinstance(result, dict): + raise RuntimeError(f"Gitea update issue failed: {result!r}") + return result + + def resolve_labels(self, owner: str, repo: str, names: list[str]) -> list[int]: + labels = self._req("GET", f"/repos/{owner}/{repo}/labels", params={"limit": 50}) + if not isinstance(labels, list): + return [] + ids = {lb.get("name"): lb.get("id") for lb in labels if isinstance(lb, dict)} + return [ids[n] for n in names if n in ids] + + +def _gitea_client(token: str) -> object: + from api.clients.gitea import GiteaClient + + return GiteaClient(token) + + +def _connect(token: str) -> object: + try: + return _gitea_client(token) + except Exception: + log.debug("Project client unavailable; using urllib") + return _UrllibGitea(token, _api_base()) + + +def _open_matches( + client: object, owner: str, repo: str, workflow: str, job: str +) -> list[dict]: + found: list[dict] = [] + for page in range(1, _MAX_PAGES + 1): + batch = client.list_issues( # type: ignore[attr-defined] + owner, + repo, + state="open", + type="issues", + limit=_PAGE_LIMIT, + page=page, + ) + # An unconfigured test double returns a non-list. Treat that as + # "nothing open" so the create path stays the default. + if not isinstance(batch, list) or not batch: + break + for issue in batch: + if not isinstance(issue, dict) or issue.get("pull_request"): + continue + if _is_match(issue, workflow, job): + found.append(issue) + if len(batch) < _PAGE_LIMIT: + break + if page == _MAX_PAGES: + log.warning("Stopped after %s pages of open issues", _MAX_PAGES) + found.sort(key=lambda issue: int(issue.get("number") or 0)) + return found + + +def _close_numbers( + client: object, owner: str, repo: str, issues: list[dict], note: str +) -> list[int]: + closed: list[int] = [] + for issue in issues: + number = int(issue.get("number") or 0) + if not number: + continue + client.comment_issue(owner, repo, number, note) # type: ignore[attr-defined] + client.update_issue(owner, repo, number, {"state": "closed"}) # type: ignore[attr-defined] + closed.append(number) + return closed + + +def _build_body(workflow: str, job: str, run: str, sha: str, ref: str) -> str: + body_parts = [ + f"**Workflow:** `{workflow}`", + f"**Job:** `{job}`", + f"**Run:** `#{run}`", + f"**Commit:** `{_sha_short(sha)}`", + f"**Ref:** `{ref}`", + "", + "A step in this CI job failed. Check the workflow run logs for details.", + ] + step_name = os.environ.get("GITHUB_ACTION", "") + if step_name: + body_parts.insert(5, f"**Step:** `{step_name}`") + body_parts.append("") + body_parts.append( + f"`{_marker(workflow, job)}` — do not edit; dedup and auto-close key on it." + ) + return "\n".join(body_parts) + + def file_ci_failure( *, workflow: str, @@ -49,81 +314,203 @@ def file_ci_failure( owner: str = "homelab", repo: str = "stack", ) -> dict | None: - """File a Gitea issue for a CI workflow failure.""" + """Comment on the open issue for this job, or file one if none is open. + + Older open duplicates of the same job are closed. + """ token = _get_token() if not token: log.error("No GITEA_TOKEN — cannot file issue") return None - sha_short = sha[:8] if sha else "unknown" - - title = f"ci/{workflow}/{job} failed (#{run} @ {sha_short})" - title = _truncate(title, 120) - - body_parts = [ - f"**Workflow:** `{workflow}`", - f"**Job:** `{job}`", - f"**Run:** `#{run}`", - f"**Commit:** `{sha_short}`", - f"**Ref:** `{ref}`", - "", - "A step in this CI job failed. Check the workflow run logs for details.", - ] - - # Try to detect which step failed from env (Gitea/GitHub set these) - step_name = os.environ.get("GITHUB_ACTION", "") - if step_name: - body_parts.insert(5, f"**Step:** `{step_name}`") - - body = "\n".join(body_parts) - - # Resolve labels - label_names = ["ci"] - wf_lower = workflow.lower() - for keyword in ("deploy", "harden", "ci", "infra", "release", "rebuild"): - if keyword in wf_lower: - label_names.append(f"pipeline:{keyword}") - break - + title = _title(workflow, job, run, sha) try: - from api.clients.gitea import GiteaClient + client = _connect(token) + except Exception: + log.exception("Failed to file Gitea issue") + return None + try: + matches = _open_matches(client, owner, repo, workflow, job) + if matches: + primary = matches[-1] + number = int(primary["number"]) + client.comment_issue( # type: ignore[attr-defined] + owner, repo, number, _failure_note(run, sha, ref) + ) + client.update_issue( # type: ignore[attr-defined] + owner, + repo, + number, + { + "title": title, + "body": _with_marker(primary.get("body") or "", workflow, job), + }, + ) + _close_numbers( + client, + owner, + repo, + matches[:-1], + f"Duplicate of #{number}. Auto-closing.", + ) + log.info("Updated issue #%s: %s", number, title) + return primary - client = GiteaClient(token) - label_ids = client.resolve_labels(owner, repo, label_names) - issue_body: dict = {"title": title, "body": body} + label_ids = client.resolve_labels(owner, repo, _label_names(workflow)) # type: ignore[attr-defined] + issue_body: dict = { + "title": title, + "body": _build_body(workflow, job, run, sha, ref), + } if label_ids: issue_body["labels"] = label_ids - result = client.create_issue(owner, repo, issue_body) + result = client.create_issue(owner, repo, issue_body) # type: ignore[attr-defined] log.info("Filed issue #%s: %s", result.get("number"), title) - client.close() return result except Exception: log.exception("Failed to file Gitea issue") return None + finally: + client.close() # type: ignore[attr-defined] + + +def close_ci_failure( + *, + workflow: str, + job: str, + run: str, + sha: str, + ref: str, + owner: str = "homelab", + repo: str = "stack", +) -> dict | None: + """Close every open issue for this workflow and job. + + Returns ``{"closed": [...]}`` when the lookup worked, including when + nothing was open. Returns None when the token is missing or the API fails. + """ + token = _get_token() + if not token: + log.error("No GITEA_TOKEN — cannot close issue") + return None + note = f"Resolved at `{_sha_short(sha)}` (run #{run}, `{ref}`). Auto-closing." + try: + client = _connect(token) + except Exception: + log.exception("Failed to close Gitea issue") + return None + try: + matches = _open_matches(client, owner, repo, workflow, job) + closed = _close_numbers(client, owner, repo, matches, note) + if len(closed) != len(matches): + log.error("Closed %s of %s issues", len(closed), len(matches)) + return None + log.info("Closed %s issue(s) for %s/%s", len(closed), workflow, job) + return {"closed": closed} + except Exception: + log.exception("Failed to close Gitea issue") + return None + finally: + client.close() # type: ignore[attr-defined] + + +def dedupe_ci_failure( + *, + workflow: str, + job: str, + owner: str = "homelab", + repo: str = "stack", +) -> dict | None: + """Close older open issues for this job and leave the newest one open.""" + token = _get_token() + if not token: + log.error("No GITEA_TOKEN — cannot dedupe issues") + return None + try: + client = _connect(token) + except Exception: + log.exception("Failed to dedupe Gitea issues") + return None + try: + matches = _open_matches(client, owner, repo, workflow, job) + if not matches: + return {"closed": []} + primary = matches[-1] + number = int(primary["number"]) + client.update_issue( # type: ignore[attr-defined] + owner, + repo, + number, + {"body": _with_marker(primary.get("body") or "", workflow, job)}, + ) + closed = _close_numbers( + client, + owner, + repo, + matches[:-1], + f"Duplicate of #{number}. Auto-closing.", + ) + log.info("Dedupe %s/%s closed %s", workflow, job, closed) + return {"closed": closed} + except Exception: + log.exception("Failed to dedupe Gitea issues") + return None + finally: + client.close() # type: ignore[attr-defined] def main() -> int: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") - parser = argparse.ArgumentParser(description="File a Gitea issue for CI failure") + parser = argparse.ArgumentParser(description="File or close a Gitea issue for CI") parser.add_argument("--workflow", required=True, help="Workflow name") parser.add_argument("--job", required=True, help="Job name") - parser.add_argument("--run", required=True, help="Run number") - parser.add_argument("--sha", required=True, help="Commit SHA") + parser.add_argument("--run", default="", help="Run number") + parser.add_argument("--sha", default="", help="Commit SHA") parser.add_argument("--ref", default="", help="Git ref (branch/tag)") parser.add_argument("--owner", default="homelab", help="Repo owner") parser.add_argument("--repo", default="stack", help="Repo name") - args = parser.parse_args() - - result = file_ci_failure( - workflow=args.workflow, - job=args.job, - run=args.run, - sha=args.sha, - ref=args.ref, - owner=args.owner, - repo=args.repo, + mode = parser.add_mutually_exclusive_group() + mode.add_argument( + "--close", + action="store_true", + help="Close the open issue for this workflow and job", ) + mode.add_argument( + "--dedupe", + action="store_true", + help="Close older open duplicates and leave the newest", + ) + args = parser.parse_args() + if not args.dedupe and (not args.run or not args.sha): + parser.error("--run and --sha are required") + + if args.dedupe: + result = dedupe_ci_failure( + workflow=args.workflow, + job=args.job, + owner=args.owner, + repo=args.repo, + ) + elif args.close: + result = close_ci_failure( + workflow=args.workflow, + job=args.job, + run=args.run, + sha=args.sha, + ref=args.ref, + owner=args.owner, + repo=args.repo, + ) + else: + result = file_ci_failure( + workflow=args.workflow, + job=args.job, + run=args.run, + sha=args.sha, + ref=args.ref, + owner=args.owner, + repo=args.repo, + ) return 0 if result else 1 diff --git a/tests/api/test_diag_ci.py b/tests/api/test_diag_ci.py index d28728f..b5146f7 100644 --- a/tests/api/test_diag_ci.py +++ b/tests/api/test_diag_ci.py @@ -150,6 +150,8 @@ class TestWorkflowGeneration: continue content = yml.read_text() assert "File failure issue" in content, f"{yml.name} missing failure step" + assert "Close resolved issue" in content, f"{yml.name} missing close step" + assert "src/api/diag/ci.py" in content, f"{yml.name} missing filer" class TestGetToken: @@ -275,3 +277,294 @@ class TestCLIFailure: sys.argv = old_argv assert code == 1 + + +def _issue( + number: int, workflow: str = "CI", job: str = "test", body: str = "old" +) -> dict: + return { + "number": number, + "title": f"ci/{workflow}/{job} failed (#{number} @ aaaaaaaa)", + "body": body, + } + + +class TestMatch: + def test_title_prefix_matches_legacy_issues(self): + from api.diag.ci import _is_match + + issue = _issue(4) + assert _is_match(issue, "CI", "test") + assert not _is_match(issue, "CI", "testing") + assert not _is_match(issue, "Deploy", "test") + + def test_marker_does_not_match_a_longer_job(self): + from api.diag.ci import _is_match + + body = "`ci-key:CI/testing` — do not edit" + issue = {"number": 1, "title": "something else", "body": body} + assert _is_match(issue, "CI", "testing") + assert not _is_match(issue, "CI", "test") + + def test_spaced_workflow_name(self): + from api.diag.ci import _is_match + + issue = _issue(8, "Notebooks Integration", "notebooks-integration") + assert _is_match(issue, "Notebooks Integration", "notebooks-integration") + assert not _is_match(issue, "Notebooks", "Integration") + + +class TestDedupeAndClose: + @patch("api.diag.ci._get_token", return_value="fake-token") + @patch("api.clients.gitea.GiteaClient") + def test_comments_instead_of_filing_a_duplicate(self, mock_cls, mock_token): + mock_client = MagicMock() + mock_client.list_issues.return_value = [_issue(10), _issue(12)] + mock_cls.return_value = mock_client + + from api.diag.ci import file_ci_failure + + result = file_ci_failure( + workflow="CI", + job="test", + run="40", + sha="deadbeefcafebabe", + ref="refs/heads/main", + ) + + assert result is not None + assert result["number"] == 12 + mock_client.create_issue.assert_not_called() + note = mock_client.comment_issue.call_args_list[0].args[3] + assert "#40" in note + assert "deadbeef" in note + assert "refs/heads/main" in note + updated = mock_client.update_issue.call_args_list[0].args + assert updated[2] == 12 + assert "#40" in updated[3]["title"] + assert "ci-key:CI/test" in updated[3]["body"] + # The older issue is closed as a duplicate, not left open. + closed = mock_client.update_issue.call_args_list[1].args + assert closed[2] == 10 + assert closed[3] == {"state": "closed"} + + @patch("api.diag.ci._get_token", return_value="fake-token") + @patch("api.clients.gitea.GiteaClient") + def test_scans_every_page(self, mock_cls, mock_token): + page1 = [_issue(n, "Other", "job") for n in range(1, 51)] + page2 = [_issue(77)] + mock_client = MagicMock() + mock_client.list_issues.side_effect = [page1, page2] + mock_cls.return_value = mock_client + + from api.diag.ci import file_ci_failure + + result = file_ci_failure( + workflow="CI", + job="test", + run="8", + sha="abc12345ffff", + ref="refs/heads/main", + ) + assert result is not None and result["number"] == 77 + mock_client.create_issue.assert_not_called() + assert mock_client.list_issues.call_count == 2 + + @patch("api.diag.ci._get_token", return_value="fake-token") + @patch("api.clients.gitea.GiteaClient") + def test_close_closes_every_open_match(self, mock_cls, mock_token): + mock_client = MagicMock() + mock_client.list_issues.return_value = [_issue(3), _issue(9)] + mock_cls.return_value = mock_client + + from api.diag.ci import close_ci_failure + + result = close_ci_failure( + workflow="CI", + job="test", + run="41", + sha="abc12345ffff", + ref="refs/heads/main", + ) + assert result == {"closed": [3, 9]} + mock_client.create_issue.assert_not_called() + notes = [c.args[3] for c in mock_client.comment_issue.call_args_list] + assert all("abc12345" in note for note in notes) + states = [c.args[3] for c in mock_client.update_issue.call_args_list] + assert states == [{"state": "closed"}, {"state": "closed"}] + + @patch("api.diag.ci._get_token", return_value="fake-token") + @patch("api.clients.gitea.GiteaClient") + def test_close_with_nothing_open(self, mock_cls, mock_token): + mock_client = MagicMock() + mock_client.list_issues.return_value = [] + mock_cls.return_value = mock_client + + from api.diag.ci import close_ci_failure + + result = close_ci_failure(workflow="CI", job="test", run="1", sha="abc", ref="") + assert result == {"closed": []} + mock_client.comment_issue.assert_not_called() + + @patch("api.diag.ci._get_token", return_value="") + def test_close_without_token(self, mock_token): + from api.diag.ci import close_ci_failure + + assert ( + close_ci_failure(workflow="CI", job="test", run="1", sha="abc", ref="") + is None + ) + + @patch("api.diag.ci._get_token", return_value="fake-token") + @patch("api.clients.gitea.GiteaClient") + def test_dedupe_leaves_the_newest(self, mock_cls, mock_token): + mock_client = MagicMock() + mock_client.list_issues.return_value = [_issue(2), _issue(5), _issue(8)] + mock_cls.return_value = mock_client + + from api.diag.ci import dedupe_ci_failure + + result = dedupe_ci_failure(workflow="CI", job="test") + assert result == {"closed": [2, 5]} + kept = mock_client.update_issue.call_args_list[0].args + assert kept[2] == 8 + assert "ci-key:CI/test" in kept[3]["body"] + assert "state" not in kept[3] + closed_ids = [c.args[2] for c in mock_client.comment_issue.call_args_list] + assert closed_ids == [2, 5] + + +class TestApiBase: + def test_env_wins(self, monkeypatch): + monkeypatch.setenv("GITEA_API_BASE", "http://127.0.0.1:3000/api/v1/") + from api.diag.ci import _api_base + + assert _api_base() == "http://127.0.0.1:3000/api/v1" + + def test_compose_url_when_git_resolves(self, monkeypatch): + monkeypatch.delenv("GITEA_API_BASE", raising=False) + monkeypatch.delenv("GITEA_API_URL", raising=False) + monkeypatch.setattr("api.diag.ci.socket.getaddrinfo", lambda *a, **k: [()]) + import tomllib + from pathlib import Path + + from api.diag.ci import _api_base + + data = tomllib.loads(Path("stack.toml").read_text()) + assert _api_base() == data["services"]["git"].rstrip("/") + "/api/v1" + + def test_public_url_when_git_does_not_resolve(self, monkeypatch): + monkeypatch.delenv("GITEA_API_BASE", raising=False) + monkeypatch.delenv("GITEA_API_URL", raising=False) + + def _down(*_a, **_k): + raise OSError("no such host") + + monkeypatch.setattr("api.diag.ci.socket.getaddrinfo", _down) + import tomllib + from pathlib import Path + + from api.diag.ci import _api_base + + data = tomllib.loads(Path("stack.toml").read_text()) + assert _api_base() == f"https://git.{data['platform']['domain']}/api/v1" + + +class TestUrllibClient: + def test_list_issues_sends_the_token_and_query(self, monkeypatch): + captured: dict = {} + + class _Resp: + def read(self): + return b'[{"number": 1}]' + + def __enter__(self): + return self + + def __exit__(self, *_a): + return False + + def _urlopen(req, timeout=30): + captured["url"] = req.full_url + captured["auth"] = req.get_header("Authorization") + captured["method"] = req.get_method() + return _Resp() + + monkeypatch.setattr("api.diag.ci.urllib.request.urlopen", _urlopen) + from api.diag.ci import _UrllibGitea + + client = _UrllibGitea("sekret", "http://git:3000/api/v1") + assert client.list_issues( + "homelab", "stack", state="open", type="issues", limit=50, page=2 + ) == [{"number": 1}] + assert captured["method"] == "GET" + assert captured["auth"] == "token sekret" + assert captured["url"].startswith( + "http://git:3000/api/v1/repos/homelab/stack/issues?" + ) + assert "state=open" in captured["url"] + assert "page=2" in captured["url"] + + def test_http_error_raises(self, monkeypatch): + import urllib.error + + def _urlopen(req, timeout=30): + raise urllib.error.HTTPError(req.full_url, 500, "nope", hdrs=None, fp=None) + + monkeypatch.setattr("api.diag.ci.urllib.request.urlopen", _urlopen) + from api.diag.ci import _UrllibGitea + + client = _UrllibGitea("sekret", "http://git:3000/api/v1") + try: + client.create_issue("homelab", "stack", {"title": "x"}) + except RuntimeError as exc: + assert "500" in str(exc) + else: + raise AssertionError("expected RuntimeError") + + +class TestCloseCLI: + @patch("api.diag.ci.close_ci_failure") + @patch("api.diag.ci.file_ci_failure") + def test_close_flag(self, mock_file, mock_close): + mock_close.return_value = {"closed": [4]} + import sys + + from api.diag.ci import main + + old = sys.argv + sys.argv = [ + "ci", + "--close", + "--workflow", + "CI", + "--job", + "test", + "--run", + "9", + "--sha", + "abc12345", + ] + try: + code = main() + finally: + sys.argv = old + assert code == 0 + mock_file.assert_not_called() + assert mock_close.call_args[1]["run"] == "9" + + @patch("api.diag.ci.dedupe_ci_failure") + def test_dedupe_flag_does_not_require_a_run(self, mock_dedupe): + mock_dedupe.return_value = {"closed": []} + import sys + + from api.diag.ci import main + + old = sys.argv + sys.argv = ["ci", "--dedupe", "--workflow", "CI", "--job", "test"] + try: + code = main() + finally: + sys.argv = old + assert code == 0 + mock_dedupe.assert_called_once()