This commit is contained in:
Mark Wylde
2025-05-30 20:02:39 +01:00
parent 180a1b6680
commit fb6df649ed
33 changed files with 4709 additions and 901 deletions

View File

@@ -1,12 +1,4 @@
import fs from "fs/promises";
import path from "path";
import type { Octokits } from "../api/client";
import { GITHUB_SERVER_URL } from "../api/config";
const IMAGE_REGEX = new RegExp(
`!\\[[^\\]]*\\]\\((${GITHUB_SERVER_URL.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\/user-attachments\\/assets\\/[^)]+)\\)`,
"g",
);
import type { GitHubClient } from "../api/client";
type IssueComment = {
type: "issue_comment";
@@ -47,186 +39,15 @@ export type CommentWithImages =
| PullRequestBody;
export async function downloadCommentImages(
octokits: Octokits,
owner: string,
repo: string,
comments: CommentWithImages[],
_client: GitHubClient,
_owner: string,
_repo: string,
_comments: CommentWithImages[],
): Promise<Map<string, string>> {
const urlToPathMap = new Map<string, string>();
const downloadsDir = "/tmp/github-images";
await fs.mkdir(downloadsDir, { recursive: true });
const commentsWithImages: Array<{
comment: CommentWithImages;
urls: string[];
}> = [];
for (const comment of comments) {
const imageMatches = [...comment.body.matchAll(IMAGE_REGEX)];
const urls = imageMatches.map((match) => match[1] as string);
if (urls.length > 0) {
commentsWithImages.push({ comment, urls });
const id =
comment.type === "issue_body"
? comment.issueNumber
: comment.type === "pr_body"
? comment.pullNumber
: comment.id;
console.log(`Found ${urls.length} image(s) in ${comment.type} ${id}`);
}
}
// Process each comment with images
for (const { comment, urls } of commentsWithImages) {
try {
let bodyHtml: string | undefined;
// Get the HTML version based on comment type
switch (comment.type) {
case "issue_comment": {
const response = await octokits.rest.issues.getComment({
owner,
repo,
comment_id: parseInt(comment.id),
mediaType: {
format: "full+json",
},
});
bodyHtml = response.data.body_html;
break;
}
case "review_comment": {
const response = await octokits.rest.pulls.getReviewComment({
owner,
repo,
comment_id: parseInt(comment.id),
mediaType: {
format: "full+json",
},
});
bodyHtml = response.data.body_html;
break;
}
case "review_body": {
const response = await octokits.rest.pulls.getReview({
owner,
repo,
pull_number: parseInt(comment.pullNumber),
review_id: parseInt(comment.id),
mediaType: {
format: "full+json",
},
});
bodyHtml = response.data.body_html;
break;
}
case "issue_body": {
const response = await octokits.rest.issues.get({
owner,
repo,
issue_number: parseInt(comment.issueNumber),
mediaType: {
format: "full+json",
},
});
bodyHtml = response.data.body_html;
break;
}
case "pr_body": {
const response = await octokits.rest.pulls.get({
owner,
repo,
pull_number: parseInt(comment.pullNumber),
mediaType: {
format: "full+json",
},
});
// Type here seems to be wrong
bodyHtml = (response.data as any).body_html;
break;
}
}
if (!bodyHtml) {
const id =
comment.type === "issue_body"
? comment.issueNumber
: comment.type === "pr_body"
? comment.pullNumber
: comment.id;
console.warn(`No HTML body found for ${comment.type} ${id}`);
continue;
}
// Extract signed URLs from HTML
const signedUrlRegex =
/https:\/\/private-user-images\.githubusercontent\.com\/[^"]+\?jwt=[^"]+/g;
const signedUrls = bodyHtml.match(signedUrlRegex) || [];
// Download each image
for (let i = 0; i < Math.min(signedUrls.length, urls.length); i++) {
const signedUrl = signedUrls[i];
const originalUrl = urls[i];
if (!signedUrl || !originalUrl) {
continue;
}
// Check if we've already downloaded this URL
if (urlToPathMap.has(originalUrl)) {
continue;
}
const fileExtension = getImageExtension(originalUrl);
const filename = `image-${Date.now()}-${i}${fileExtension}`;
const localPath = path.join(downloadsDir, filename);
try {
console.log(`Downloading ${originalUrl}...`);
const imageResponse = await fetch(signedUrl);
if (!imageResponse.ok) {
throw new Error(
`HTTP ${imageResponse.status}: ${imageResponse.statusText}`,
);
}
const arrayBuffer = await imageResponse.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await fs.writeFile(localPath, buffer);
console.log(`✓ Saved: ${localPath}`);
urlToPathMap.set(originalUrl, localPath);
} catch (error) {
console.error(`✗ Failed to download ${originalUrl}:`, error);
}
}
} catch (error) {
const id =
comment.type === "issue_body"
? comment.issueNumber
: comment.type === "pr_body"
? comment.pullNumber
: comment.id;
console.error(
`Failed to process images for ${comment.type} ${id}:`,
error,
);
}
}
return urlToPathMap;
}
function getImageExtension(url: string): string {
const urlParts = url.split("/");
const filename = urlParts[urlParts.length - 1];
if (!filename) {
throw new Error("Invalid URL: No filename found");
}
const match = filename.match(/\.(png|jpg|jpeg|gif|webp|svg)$/i);
return match ? match[0] : ".png";
// Temporarily simplified - return empty map to avoid Octokit dependencies
// TODO: Implement image downloading with direct Gitea API calls if needed
console.log(
"Image downloading temporarily disabled during Octokit migration",
);
return new Map<string, string>();
}

View File

@@ -0,0 +1,96 @@
#!/usr/bin/env bun
import { $ } from "bun";
/**
* Check if a branch exists locally using git commands
*/
export async function branchExists(branchName: string): Promise<boolean> {
try {
await $`git show-ref --verify --quiet refs/heads/${branchName}`;
return true;
} catch {
return false;
}
}
/**
* Check if a remote branch exists using git commands
*/
export async function remoteBranchExists(branchName: string): Promise<boolean> {
try {
await $`git show-ref --verify --quiet refs/remotes/origin/${branchName}`;
return true;
} catch {
return false;
}
}
/**
* Get the SHA of a branch using git commands
*/
export async function getBranchSha(branchName: string): Promise<string | null> {
try {
// Try local branch first
if (await branchExists(branchName)) {
const result = await $`git rev-parse refs/heads/${branchName}`;
return result.text().trim();
}
// Try remote branch if local doesn't exist
if (await remoteBranchExists(branchName)) {
const result = await $`git rev-parse refs/remotes/origin/${branchName}`;
return result.text().trim();
}
return null;
} catch (error) {
console.error(`Error getting SHA for branch ${branchName}:`, error);
return null;
}
}
/**
* Check if a branch has commits different from base branch
*/
export async function branchHasChanges(
branchName: string,
baseBranch: string,
): Promise<{
hasChanges: boolean;
branchSha: string | null;
baseSha: string | null;
}> {
try {
const branchSha = await getBranchSha(branchName);
const baseSha = await getBranchSha(baseBranch);
if (!branchSha || !baseSha) {
return { hasChanges: false, branchSha, baseSha };
}
const hasChanges = branchSha !== baseSha;
return { hasChanges, branchSha, baseSha };
} catch (error) {
console.error(
`Error comparing branches ${branchName} and ${baseBranch}:`,
error,
);
return { hasChanges: false, branchSha: null, baseSha: null };
}
}
/**
* Fetch latest changes from remote to ensure we have up-to-date branch info
*/
export async function fetchBranch(branchName: string): Promise<boolean> {
try {
await $`git fetch origin ${branchName}`;
return true;
} catch (error) {
console.log(
`Could not fetch branch ${branchName} from remote (may not exist yet)`,
);
return false;
}
}