fix: build image URL regexes at call time, not module load time

This commit is contained in:
Vlad Mocanu committed 2026-02-25 19:26:18 +00:00
1 parent 2f7d0a7fb2
commit 3bab2edc41
2 files changed
+34 -13

No files matched your search

+21 -13
View File
@@ -1,18 +1,24 @@
import fs from "fs/promises";
import path from "path";
import type { Octokits } from "../api/client";
import { GITHUB_SERVER_URL } from "../api/config";
const escapedUrl = GITHUB_SERVER_URL.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const IMAGE_REGEX = new RegExp(
`!\\[[^\\]]*\\]\\((${escapedUrl}\\/user-attachments\\/assets\\/[^)]+)\\)`,
"g",
);
const HTML_IMG_REGEX = new RegExp(
`<img[^>]+src=["']([^"']*${escapedUrl}\\/user-attachments\\/assets\\/[^"']+)["'][^>]*>`,
"gi",
);
// Build regexes at call time, reading GITHUB_SERVER_URL from process.env
// directly. The config.ts export freezes the value at module load time,
// which can be wrong when the Forgejo CI runner injects its own
// GITHUB_SERVER_URL that the shell export cannot override.
function buildImageRegexes(): { imageRegex: RegExp; htmlImgRegex: RegExp } {
const serverUrl = process.env.GITHUB_SERVER_URL || "https://github.com";
const escapedUrl = serverUrl.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const imageRegex = new RegExp(
`!\\[[^\\]]*\\]\\((${escapedUrl}\\/user-attachments\\/assets\\/[^)]+)\\)`,
"g",
);
const htmlImgRegex = new RegExp(
`<img[^>]+src=["']([^"']*${escapedUrl}\\/user-attachments\\/assets\\/[^"']+)["'][^>]*>`,
"gi",
);
return { imageRegex, htmlImgRegex };
}
type IssueComment = {
type: "issue_comment";
@@ -63,6 +69,8 @@ export async function downloadCommentImages(
await fs.mkdir(downloadsDir, { recursive: true });
const { imageRegex, htmlImgRegex } = buildImageRegexes();
const commentsWithImages: Array<{
comment: CommentWithImages;
urls: string[];
@@ -70,11 +78,11 @@ export async function downloadCommentImages(
for (const comment of comments) {
// Extract URLs from Markdown format
const markdownMatches = [...comment.body.matchAll(IMAGE_REGEX)];
const markdownMatches = [...comment.body.matchAll(imageRegex)];
const markdownUrls = markdownMatches.map((match) => match[1] as string);
// Extract URLs from HTML format
const htmlMatches = [...comment.body.matchAll(HTML_IMG_REGEX)];
const htmlMatches = [...comment.body.matchAll(htmlImgRegex)];
const htmlUrls = htmlMatches.map((match) => match[1] as string);
// Combine and deduplicate URLs