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
13 changes: 10 additions & 3 deletions apps/web/src/functions/github-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ interface CommitBody {
message: string;
content?: string;
sha?: string;
branch?: string;
author?: { name: string; email: string };
committer?: { name: string; email: string };
}

function buildCommitBody(
message: string,
author?: { name: string; email: string },
options?: { content?: string; sha?: string },
options?: { content?: string; sha?: string; branch?: string },
): CommitBody {
const body: CommitBody = {
message,
};
if (options?.content !== undefined) body.content = options.content;
if (options?.sha) body.sha = options.sha;
if (options?.branch) body.branch = options.branch;
if (author) {
body.author = author;
body.committer = author;
Expand Down Expand Up @@ -490,6 +492,7 @@ export async function renameContentFile(

export async function deleteContentFile(
filePath: string,
branchName?: string,
): Promise<{ success: boolean; error?: string }> {
if (isDev()) {
try {
Expand All @@ -512,14 +515,15 @@ export async function deleteContentFile(
return { success: false, error: "GitHub token not configured" };
}
const { token: githubToken, author } = credentials;
const targetBranch = branchName || GITHUB_BRANCH;

const fullPath = filePath.startsWith("apps/web/content")
? filePath
: `${CONTENT_PATH}/${filePath}`;

try {
const getResponse = await fetch(
`https://api.github.com/repos/${GITHUB_REPO}/contents/${fullPath}?ref=${GITHUB_BRANCH}`,
`https://api.github.com/repos/${GITHUB_REPO}/contents/${fullPath}?ref=${targetBranch}`,
{
headers: {
Authorization: `Bearer ${githubToken}`,
Expand Down Expand Up @@ -548,7 +552,10 @@ export async function deleteContentFile(
Accept: "application/vnd.github.v3+json",
},
body: JSON.stringify(
buildCommitBody(`Delete ${filePath} via admin`, author, { sha }),
buildCommitBody(`Delete ${filePath} via admin`, author, {
sha,
branch: targetBranch,
}),
),
},
);
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/routes/admin/collections/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ function CollectionsPage() {
});

const deleteMutation = useMutation({
mutationFn: async (params: { path: string }) =>
mutationFn: async (params: { path: string; branch?: string }) =>
postAdminJson<any>(
"/api/admin/content/delete",
params,
Expand Down Expand Up @@ -620,6 +620,8 @@ function CollectionsPage() {
slug: (path.split("/").pop() || "").replace(/\.mdx$/, ""),
type: "file",
collection: path.split("/")[0] || "articles",
branch:
currentTab?.type === "file" ? currentTab.branch : undefined,
},
collectionName: path.split("/")[0] || "articles",
})
Expand Down Expand Up @@ -658,6 +660,7 @@ function CollectionsPage() {
if (deleteConfirmation) {
deleteMutation.mutate({
path: deleteConfirmation.item.path,
branch: deleteConfirmation.item.branch,
});
}
}}
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/routes/api/admin/content/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deleteContentFile } from "@/functions/github-content";

interface DeleteRequest {
path: string;
branch?: string;
}

export const Route = createFileRoute("/api/admin/content/delete")({
Expand Down Expand Up @@ -32,7 +33,7 @@ export const Route = createFileRoute("/api/admin/content/delete")({
});
}

const { path } = body;
const { path, branch } = body;

if (!path) {
return new Response(
Expand All @@ -41,7 +42,7 @@ export const Route = createFileRoute("/api/admin/content/delete")({
);
}

const result = await deleteContentFile(path);
const result = await deleteContentFile(path, branch);

if (!result.success) {
return new Response(JSON.stringify({ error: result.error }), {
Expand Down