diff --git a/.github/workflows/convert-pdfs.yml b/.github/workflows/convert-pdfs.yml index dfc839f..6f95b08 100644 --- a/.github/workflows/convert-pdfs.yml +++ b/.github/workflows/convert-pdfs.yml @@ -3,6 +3,8 @@ name: Convert PDFs to Markdown on: push: + branches: + - "**" paths: - "**/assets/*/source.pdf" workflow_dispatch: @@ -16,6 +18,7 @@ concurrency: jobs: convert: + if: github.ref_type == 'branch' runs-on: ubuntu-latest timeout-minutes: 45 diff --git a/README.de.md b/README.de.md index 780bb3f..a5e6a50 100644 --- a/README.de.md +++ b/README.de.md @@ -216,6 +216,11 @@ gespeichert werden, aktualisiert der Workflow seinen Branch vor dem Push erneut und wiederholt einen abgelehnten Push bis zu fünfmal. Dadurch gehen parallele SourceBraid-Uploads nicht durch einen kurzzeitigen Git-Ref-Konflikt verloren. +Die automatische PDF-Konvertierung läuft bei Branch-Pushes, nicht bei +Release-Tags. Entfernt ein Force-Push den vorherigen Commit aus dem Checkout, +prüft der Workflow die aktuellen PDF-Quellen, ohne die entfernte Historie erneut +zu laden. Ein Checkout ohne passende PDFs endet erfolgreich ohne Konvertierung. + ## Wikis und Gists mit Bildern ### Azure DevOps Wiki diff --git a/README.md b/README.md index 7769246..c9ea136 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,11 @@ If another clip is saved to the same branch during conversion, the workflow refreshes its branch and retries a rejected push up to five times. Concurrent SourceBraid uploads are therefore not lost to a temporary Git ref race. +Automatic PDF conversion runs on branch pushes, not release tags. If a +force-push removes the previous commit from the checkout, the workflow scans +the current PDF sources instead of fetching the removed history. A checkout +without matching PDFs finishes successfully without converting anything. + ## Wikis and Gists with images ### Azure DevOps Wiki diff --git a/chrome-extension/sourcebraid/.github/workflows/convert-pdfs.yml b/chrome-extension/sourcebraid/.github/workflows/convert-pdfs.yml index dfc839f..6f95b08 100644 --- a/chrome-extension/sourcebraid/.github/workflows/convert-pdfs.yml +++ b/chrome-extension/sourcebraid/.github/workflows/convert-pdfs.yml @@ -3,6 +3,8 @@ name: Convert PDFs to Markdown on: push: + branches: + - "**" paths: - "**/assets/*/source.pdf" workflow_dispatch: @@ -16,6 +18,7 @@ concurrency: jobs: convert: + if: github.ref_type == 'branch' runs-on: ubuntu-latest timeout-minutes: 45 diff --git a/chrome-extension/sourcebraid/scripts/convert_pdfs.py b/chrome-extension/sourcebraid/scripts/convert_pdfs.py index 6515dfe..f783b33 100644 --- a/chrome-extension/sourcebraid/scripts/convert_pdfs.py +++ b/chrome-extension/sourcebraid/scripts/convert_pdfs.py @@ -39,7 +39,25 @@ def main() -> int: def discover_sources(before: str, after: str, process_all: bool) -> list[Path]: - if process_all or not before or before == ZERO_SHA: + scan_all = process_all or not before or before == ZERO_SHA + if not scan_all: + previous = subprocess.run( + ["git", "cat-file", "-e", f"{before}^{{commit}}"], + check=False, + capture_output=True, + ) + if previous.returncode: + # A force-push can remove the previous history from a fresh checkout. + # Validate the current revision, then scan locally without fetching it back. + subprocess.run( + ["git", "cat-file", "-e", f"{after}^{{commit}}"], + check=True, + capture_output=True, + ) + print("Previous commit is unavailable locally; scanning current source.pdf files.") + scan_all = True + + if scan_all: candidates = Path(".").glob("**/assets/*/source.pdf") else: result = subprocess.run( diff --git a/scripts/convert_pdfs.py b/scripts/convert_pdfs.py index 6515dfe..f783b33 100644 --- a/scripts/convert_pdfs.py +++ b/scripts/convert_pdfs.py @@ -39,7 +39,25 @@ def main() -> int: def discover_sources(before: str, after: str, process_all: bool) -> list[Path]: - if process_all or not before or before == ZERO_SHA: + scan_all = process_all or not before or before == ZERO_SHA + if not scan_all: + previous = subprocess.run( + ["git", "cat-file", "-e", f"{before}^{{commit}}"], + check=False, + capture_output=True, + ) + if previous.returncode: + # A force-push can remove the previous history from a fresh checkout. + # Validate the current revision, then scan locally without fetching it back. + subprocess.run( + ["git", "cat-file", "-e", f"{after}^{{commit}}"], + check=True, + capture_output=True, + ) + print("Previous commit is unavailable locally; scanning current source.pdf files.") + scan_all = True + + if scan_all: candidates = Path(".").glob("**/assets/*/source.pdf") else: result = subprocess.run( diff --git a/tests/test_convert_pdfs.py b/tests/test_convert_pdfs.py index bfa1a17..8e13401 100644 --- a/tests/test_convert_pdfs.py +++ b/tests/test_convert_pdfs.py @@ -1,10 +1,13 @@ import json import os +import subprocess +import sys import tempfile import unittest from pathlib import Path from scripts.convert_pdfs import ( + discover_sources, extract_frontmatter, extract_notes, frontmatter_value, @@ -14,6 +17,98 @@ ) +class DiscoverPdfSourcesTest(unittest.TestCase): + MISSING_BEFORE = "f" * 40 + MISSING_AFTER = "e" * 40 + + def setUp(self): + original_cwd = Path.cwd() + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name) + os.chdir(self.root) + self.addCleanup(os.chdir, original_cwd) + self.git("init", "--quiet") + self.git("config", "user.name", "SourceBraid Test") + self.git("config", "user.email", "fixtures@example.com") + + def git(self, *args): + result = subprocess.run( + ["git", "-c", "core.hooksPath=/dev/null", "-c", "commit.gpgsign=false", *args], + check=True, + capture_output=True, + text=True, + ) + return result.stdout.strip() + + def write_source(self, slug, content=b"%PDF synthetic fixture"): + path = Path("web-clips/2026/11/assets") / slug / "source.pdf" + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(content) + return path + + def commit(self): + self.git("add", "--all") + self.git("commit", "--quiet", "--allow-empty", "-m", "Synthetic fixture update") + return self.git("rev-parse", "HEAD") + + def test_valid_range_discovers_only_added_and_modified_source_pdfs(self): + self.write_source("unchanged") + modified = self.write_source("modified") + removed = self.write_source("removed") + before = self.commit() + + modified.write_bytes(b"%PDF changed synthetic fixture") + removed.unlink() + added = self.write_source("added", b"%PDF newly added synthetic fixture") + ignored = added.parent / "attachment.pdf" + ignored.write_bytes(b"%PDF unrelated attachment") + after = self.commit() + self.write_source("untracked") + + self.assertEqual( + discover_sources(before, after, False), + sorted([added, modified]), + ) + + def test_missing_before_discovers_current_pdfs_including_unchanged_sources(self): + unchanged = self.write_source("unchanged") + removed = self.write_source("removed") + self.commit() + + removed.unlink() + added = self.write_source("added") + after = self.commit() + + self.assertEqual( + discover_sources(self.MISSING_BEFORE, after, False), + sorted([added, unchanged]), + ) + + def test_missing_before_without_pdfs_succeeds_without_docling(self): + after = self.commit() + script = Path(__file__).resolve().parents[1] / "scripts" / "convert_pdfs.py" + # Isolated Python without site packages verifies the no-work path needs no Docling. + result = subprocess.run( + [ + sys.executable, "-I", "-S", str(script), + "--before", self.MISSING_BEFORE, "--after", after, + ], + check=True, + capture_output=True, + text=True, + ) + + self.assertIn("No queued source.pdf files found.", result.stdout) + + def test_missing_before_does_not_hide_invalid_after(self): + self.write_source("current") + self.commit() + + with self.assertRaises(subprocess.CalledProcessError): + discover_sources(self.MISSING_BEFORE, self.MISSING_AFTER, False) + + class ConvertPdfHelpersTest(unittest.TestCase): def test_rewrite_artifact_links_normalizes_absolute_and_relative_paths(self): markdown = (