import { FORGE_PROJECTS } from "@/data/forge"; import { ChangelogEntry, ForgeProject } from "@/types"; import "server-only"; export async function getAllForgeProjects(): Promise { // We use Promise.all to fetch all changelogs in parallel for speed return Promise.all( FORGE_PROJECTS.map(async (metadata) => { const changelog = await getPrivateChangelog(metadata.repoName); const latestVersion = changelog.length > 0 ? changelog[0].version : "0.0.0"; return { ...metadata, currentVersion: `${latestVersion}_${metadata.status}`, changelog: changelog, }; }), ); } export async function getPrivateChangelog( repoName: string, ): Promise { const repoOwner = "georgew"; const filePath = "changelog.md"; const url = `https://git.georgew.dev/api/v1/repos/${repoOwner}/${repoName}/contents/${filePath}`; const headers = { headers: { Authorization: `token ${process.env.FORGEJO_TOKEN}`, Accept: "application/vnd.forgejo.raw", }, next: { revalidate: 3600 }, }; try { const response = await fetch(url, headers); if (!response.ok) { return [ { version: "v0.0.0", date: new Date().toISOString().split("T")[0], changes: ["Documentation currently synchronizing with the Forge..."], }, ]; } const data = await response.json(); // 2. Forgejo returns the content as a base64 string // We need to decode it to UTF-8 const markdownText = Buffer.from(data.content, "base64").toString("utf-8"); return parseMarkdown(markdownText); } catch (error) { console.error("Git Fetch Error:", error); return []; } } // Simple parser for your ## [Version] - YYYY-MM-DD format function parseMarkdown(text: string): ChangelogEntry[] { // Split by "## " at the start of a line to isolate version blocks const sections = text.split(/\n(?=## )/g); return sections .map((section) => { const lines = section .split("\n") .map((l) => l.trim()) .filter(Boolean); if (lines.length === 0) return null; // Match the pattern: ## [Version] - YYYY-MM-DD const headerMatch = lines[0].match(/## \[(.*?)\] - (.*)/); if (!headerMatch) return null; const version = headerMatch[1]; const date = headerMatch[2]; // Collect all lines starting with "- " anywhere in this version block const changes = lines .filter((line) => line.startsWith("- ")) .map((line) => line.replace("- ", "")); return { version, date, changes }; }) .filter((entry): entry is ChangelogEntry => entry !== null) .slice(0, 3); }