whdemo / server.ts
Charlie
update
f234d16
import * as express from "express";
const PORT = 7260;
if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) {
console.error(
"This app needs those env variables to be defined",
"WEBHOOK_SECRET, HF_TOKEN"
);
process.exit();
}
const app = express();
app.use(express.json());
app.post("/", async (req, res) => {
if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
console.error("incorrect secret");
return res.status(400).json({ error: "incorrect secret" });
}
if (!req.body?.repo) return;
const { event, repo } = req.body;
if (event.action === "create" && event.scope === "repo") {
const [orgName, repoName] = repo.name.split("/");
console.log({ event, repo, orgName, repoName });
const payload = {
name: `${repoName}-rg`,
description: `Resource group for repository ${repoName}`,
repos: [{ ...repo, type: "model" }],
autoJoin: {
enabled: true,
role: "admin",
},
};
try {
await fetch(
`https://huggingface.co/api/organizations/${orgName}/resource-groups`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.HF_TOKEN}`,
},
body: JSON.stringify(payload),
}
);
} catch (error) {
throw new Error(error.message);
}
}
res.json({ success: true });
});
app.listen(PORT, () => {
console.debug(`server started at http://localhost:${PORT}`);
});