generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a6bdeb
commit 1d5dfa5
Showing
2 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { NextResponse } from "next/server"; | ||
import { db, vercelTokens } from "@/drizzle/schema"; | ||
import { Vercel } from "@vercel/sdk"; | ||
import { headers } from "next/headers"; | ||
|
||
const CRON_SECRET = process.env.CRON_SECRET; | ||
|
||
export async function GET(request: Request) { | ||
console.log("Redeploy cron job started"); | ||
// Verify the request is from Vercel Cron | ||
const headersList = headers(); | ||
const authHeader = headersList.get('authorization'); | ||
|
||
if (authHeader !== `Bearer ${CRON_SECRET}`) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
try { | ||
// Get all tokens from the database | ||
const tokens = await db | ||
.select() | ||
.from(vercelTokens); | ||
|
||
console.log(`Found ${tokens.length} tokens to process`); | ||
|
||
const results = await Promise.allSettled( | ||
tokens.map(async (tokenRecord) => { | ||
try { | ||
const vercel = new Vercel({ | ||
bearerToken: tokenRecord.token, | ||
}); | ||
|
||
if (!tokenRecord.projectId) { | ||
console.log(`No project ID for user ${tokenRecord.userId}`); | ||
return; | ||
} | ||
|
||
// Create new deployment with correct properties | ||
const deployment = await vercel.deployments.createDeployment({ | ||
requestBody: { | ||
name: `file-organizer-redeploy-${Date.now()}`, | ||
target: "production", | ||
project: tokenRecord.projectId, | ||
gitSource: { | ||
type: "github", | ||
repo: "different-ai/file-organizer-2000", | ||
ref: "master", | ||
org: "different-ai", | ||
}, | ||
projectSettings: { | ||
framework: "nextjs", | ||
buildCommand: "pnpm build:self-host", | ||
installCommand: "pnpm install", | ||
outputDirectory: ".next", | ||
rootDirectory: "web", | ||
}, | ||
}, | ||
}); | ||
|
||
console.log(`Redeployed project ${tokenRecord.projectId} for user ${tokenRecord.userId}`); | ||
return deployment; | ||
} catch (error) { | ||
console.error(`Failed to redeploy for user ${tokenRecord.userId}:`, error); | ||
throw error; | ||
} | ||
}) | ||
); | ||
|
||
const successful = results.filter((r) => r.status === "fulfilled").length; | ||
const failed = results.filter((r) => r.status === "rejected").length; | ||
|
||
return NextResponse.json({ | ||
message: `Processed ${tokens.length} tokens`, | ||
stats: { | ||
total: tokens.length, | ||
successful, | ||
failed, | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error("Error in redeploy cron:", error); | ||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters