Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/convert-pdfs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: Convert PDFs to Markdown

on:
push:
branches:
- "**"
Comment thread
patrickschiller marked this conversation as resolved.
Resolved
paths:
- "**/assets/*/source.pdf"
workflow_dispatch:
Expand All @@ -16,6 +18,7 @@ concurrency:

jobs:
convert:
if: github.ref_type == 'branch'
runs-on: ubuntu-latest
timeout-minutes: 45

Expand Down
5 changes: 5 additions & 0 deletions README.de.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: Convert PDFs to Markdown

on:
push:
branches:
- "**"
paths:
- "**/assets/*/source.pdf"
workflow_dispatch:
Expand All @@ -16,6 +18,7 @@ concurrency:

jobs:
convert:
if: github.ref_type == 'branch'
runs-on: ubuntu-latest
timeout-minutes: 45

Expand Down
20 changes: 19 additions & 1 deletion chrome-extension/sourcebraid/scripts/convert_pdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 19 additions & 1 deletion scripts/convert_pdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
95 changes: 95 additions & 0 deletions tests/test_convert_pdfs.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = (
Expand Down
Loading