1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-06-14 00:29:19 +00:00

fix: change status depending on input

This commit is contained in:
2025-05-04 00:08:53 +02:00
parent c9021ab8fa
commit 2e4e3c25ba

View File

@ -19,6 +19,7 @@ type CardResult = {
isAsian?: boolean; isAsian?: boolean;
usedLanguage?: string; usedLanguage?: string;
hasImage?: boolean; hasImage?: boolean;
status?: "added" | "removed" | "modified";
}; };
type CardFetchResult = { type CardFetchResult = {
@ -124,7 +125,7 @@ async function tryFetchCardWithFallback(
async function getChangedFiles( async function getChangedFiles(
context: typeof github.context, context: typeof github.context,
octokit: ReturnType<typeof github.getOctokit>, octokit: ReturnType<typeof github.getOctokit>,
): Promise<string[]> { ): Promise<{ filename: string; status: string }[]> {
if (context.payload.pull_request) { if (context.payload.pull_request) {
const { owner, repo } = context.repo; const { owner, repo } = context.repo;
const prNumber = context.payload.pull_request.number; const prNumber = context.payload.pull_request.number;
@ -133,63 +134,95 @@ async function getChangedFiles(
repo, repo,
pull_number: prNumber, pull_number: prNumber,
}); });
return response.data.map((file) => file.filename); return response.data.map((file) => ({ filename: file.filename, status: file.status }));
} else if (context.payload.commits) { } else if (context.payload.commits) {
const filesSet = new Set<string>(); const files: { filename: string; status: string }[] = [];
for (const commit of context.payload.commits) { for (const commit of context.payload.commits) {
["added", "modified", "removed"].forEach((type) => { if (commit.added) {
if (commit[type]) commit[type].forEach((file: string) => filesSet.add(file)); commit.added.forEach((file: string) => files.push({ filename: file, status: "added" }));
}); }
if (commit.modified) {
commit.modified.forEach((file: string) => files.push({ filename: file, status: "modified" }));
}
if (commit.removed) {
commit.removed.forEach((file: string) => files.push({ filename: file, status: "removed" }));
}
} }
return Array.from(filesSet); return files;
} }
return []; return [];
} }
// Process a single card file // Process a single card file
async function processCardFile(file: string): Promise<CardResult | null> { async function processCardFile(file: { filename: string; status: string }): Promise<CardResult | null> {
console.log(` - ${file}`); console.log(` - ${file.filename} (${file.status})`);
let match = file.match(DATA_REGEX); let match = file.filename.match(DATA_REGEX);
const isCardFile = !!(match || file.filename.match(DATA_ASIA_REGEX));
if (!isCardFile) {
return null;
}
// For added files, just return the file info without fetching
if (file.status === "added") {
return {
file: file.filename,
status: "added",
};
}
// For removed files, just return the file info without fetching
if (file.status === "removed") {
return {
file: file.filename,
status: "removed",
};
}
// Only process modified files normally
if (match) { if (match) {
const [_, , setName, cardLocalId] = match; const [_, , setName, cardLocalId] = match;
const result = await tryFetchCardWithFallback(setName!, cardLocalId!, "en", INTERNATIONAL_LANGUAGES, false); const result = await tryFetchCardWithFallback(setName!, cardLocalId!, "en", INTERNATIONAL_LANGUAGES, false);
if (result) { if (result) {
return { return {
file, file: file.filename,
card: sanitizeCardData(result.card), card: sanitizeCardData(result.card),
isAsian: false, isAsian: false,
usedLanguage: result.usedLanguage, usedLanguage: result.usedLanguage,
hasImage: result.hasImage, hasImage: result.hasImage,
status: "modified",
}; };
} else { } else {
return { return {
file, file: file.filename,
error: "Failed to fetch card information in all available languages", error: "Failed to fetch card information in all available languages",
isAsian: false, isAsian: false,
status: "modified",
}; };
} }
} }
match = file.match(DATA_ASIA_REGEX); match = file.filename.match(DATA_ASIA_REGEX);
if (match) { if (match) {
const [_, , setId, cardLocalId] = match; const [_, , setId, cardLocalId] = match;
const result = await tryFetchCardWithFallback(setId!, cardLocalId!, "ja", ASIAN_LANGUAGES, true); const result = await tryFetchCardWithFallback(setId!, cardLocalId!, "ja", ASIAN_LANGUAGES, true);
if (result) { if (result) {
return { return {
file, file: file.filename,
card: sanitizeCardData(result.card), card: sanitizeCardData(result.card),
isAsian: true, isAsian: true,
usedLanguage: result.usedLanguage, usedLanguage: result.usedLanguage,
hasImage: result.hasImage, hasImage: result.hasImage,
status: "modified",
}; };
} else { } else {
return { return {
file, file: file.filename,
error: "Failed to fetch card information in all available languages", error: "Failed to fetch card information in all available languages",
isAsian: true, isAsian: true,
status: "modified",
}; };
} }
} }
@ -200,15 +233,17 @@ async function processCardFile(file: string): Promise<CardResult | null> {
// Generate comment body for PR // Generate comment body for PR
function generateCommentBody( function generateCommentBody(
cardResults: CardResult[], cardResults: CardResult[],
changedFiles: string[], changedFiles: { filename: string; status: string }[],
repoFullName: string, repoFullName: string,
contextSha: string, contextSha: string,
): string { ): string {
const successfulCards = cardResults.filter((r) => r.card).length; const newCards = cardResults.filter((r) => r.status === "added").length;
const errorCards = cardResults.filter((r) => r.error).length; const deletedCards = cardResults.filter((r) => r.status === "removed").length;
const modifiedCards = cardResults.filter((r) => r.status === "modified" && r.card).length;
const errorCards = cardResults.filter((r) => r.status === "modified" && r.error).length;
const cardsWithoutImages = cardResults.filter((r) => r.card && !r.hasImage).length; const cardsWithoutImages = cardResults.filter((r) => r.card && !r.hasImage).length;
let commentBody = `## 🃏 ${successfulCards + errorCards} Card${successfulCards + errorCards !== 1 ? "s" : ""} Changed\n\n`; let commentBody = `## 🃏 ${cardResults.length} Card${cardResults.length !== 1 ? "s" : ""} Changed\n\n`;
if (cardResults.length === 0) { if (cardResults.length === 0) {
commentBody += commentBody +=
@ -218,23 +253,42 @@ function generateCommentBody(
return commentBody; return commentBody;
} }
// Add summary if there are errors or cards without images // Add summary
if (errorCards > 0 || cardsWithoutImages > 0) { commentBody += `**Details:** `;
commentBody += `**Details:** ${successfulCards} processed successfully`;
if (cardsWithoutImages > 0) { const details = [];
commentBody += ` (${cardsWithoutImages} without images)`; if (newCards > 0) {
} details.push(`${newCards} new`);
if (errorCards > 0) {
commentBody += `, ${errorCards} with errors`;
}
commentBody += `\n\n`;
} }
if (deletedCards > 0) {
details.push(`${deletedCards} deleted`);
}
if (modifiedCards > 0) {
details.push(`${modifiedCards} modified`);
}
if (errorCards > 0) {
details.push(`${errorCards} with errors`);
}
if (cardsWithoutImages > 0) {
details.push(`${cardsWithoutImages} without images`);
}
commentBody += details.join(", ") + "\n\n";
// Generate detailed card information // Generate detailed card information
for (const item of cardResults) { for (const item of cardResults) {
const fileUrl = `https://github.com/${repoFullName}/blob/${contextSha}/${item.file}`; const fileUrl = `https://github.com/${repoFullName}/blob/${contextSha}/${item.file}`;
const fileName = item.file.split("/").pop();
if (item.card) { if (item.status === "added") {
commentBody += `<details><summary> <strong>New card: ${fileName}</strong></summary>\n\n`;
commentBody += `**File:** [${item.file}](${fileUrl}) \n\n`;
commentBody += "</details>\n\n";
} else if (item.status === "removed") {
commentBody += `<details><summary>🗑️ <strong>Deleted card: ${fileName}</strong></summary>\n\n`;
commentBody += `**File:** [${item.file}](${fileUrl}) \n\n`;
commentBody += "</details>\n\n";
} else if (item.card) {
const langInfo = item.usedLanguage ? ` (found using ${item.usedLanguage})` : ""; const langInfo = item.usedLanguage ? ` (found using ${item.usedLanguage})` : "";
const imageStatus = !item.hasImage ? ` <em>(no images)</em>` : ""; const imageStatus = !item.hasImage ? ` <em>(no images)</em>` : "";
@ -252,7 +306,7 @@ function generateCommentBody(
commentBody += `**Rarity:** ${item.card.rarity || "Unknown"}\n\n`; commentBody += `**Rarity:** ${item.card.rarity || "Unknown"}\n\n`;
commentBody += "</details>\n\n"; commentBody += "</details>\n\n";
} else if (item.error) { } else if (item.error) {
commentBody += `<details><summary>⚠️ <strong>Error processing ${item.file.split("/").pop()}</strong></summary>\n\n`; commentBody += `<details><summary>⚠️ <strong>Error processing ${fileName}</strong></summary>\n\n`;
commentBody += `**File:** [${item.file}](${fileUrl}) \n`; commentBody += `**File:** [${item.file}](${fileUrl}) \n`;
commentBody += `**Error:** ${item.error}\n\n`; commentBody += `**Error:** ${item.error}\n\n`;
commentBody += "</details>\n\n"; commentBody += "</details>\n\n";
@ -357,7 +411,7 @@ async function run() {
// Store the generated comment for the workflow // Store the generated comment for the workflow
core.setOutput("pr_comment", commentBody); core.setOutput("pr_comment", commentBody);
core.setOutput("files", changedFiles.join(",")); core.setOutput("files", changedFiles.map(f => f.filename).join(","));
core.setOutput("cardFiles", JSON.stringify(cardResults)); core.setOutput("cardFiles", JSON.stringify(cardResults));
} catch (error) { } catch (error) {
if (error instanceof Error) { if (error instanceof Error) {