Charlie commited on
Commit
f234d16
·
1 Parent(s): 2b6c488
Files changed (1) hide show
  1. server.ts +47 -6
server.ts CHANGED
@@ -1,16 +1,57 @@
1
  import * as express from "express";
2
 
3
- const PORT = 7860;
 
 
 
 
 
 
 
 
4
 
5
  const app = express();
6
  app.use(express.json());
7
 
8
- app.get("/", (req, res) => {
9
- res.json({ hello: "world" });
10
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- app.post("*", async (req, res) => {
13
- console.log("ok");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  res.json({ success: true });
15
  });
16
 
 
1
  import * as express from "express";
2
 
3
+ const PORT = 7260;
4
+
5
+ if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) {
6
+ console.error(
7
+ "This app needs those env variables to be defined",
8
+ "WEBHOOK_SECRET, HF_TOKEN"
9
+ );
10
+ process.exit();
11
+ }
12
 
13
  const app = express();
14
  app.use(express.json());
15
 
16
+ app.post("/", async (req, res) => {
17
+ if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
18
+ console.error("incorrect secret");
19
+ return res.status(400).json({ error: "incorrect secret" });
20
+ }
21
+ if (!req.body?.repo) return;
22
+
23
+ const { event, repo } = req.body;
24
+
25
+ if (event.action === "create" && event.scope === "repo") {
26
+ const [orgName, repoName] = repo.name.split("/");
27
+ console.log({ event, repo, orgName, repoName });
28
+
29
+ const payload = {
30
+ name: `${repoName}-rg`,
31
+ description: `Resource group for repository ${repoName}`,
32
+ repos: [{ ...repo, type: "model" }],
33
+ autoJoin: {
34
+ enabled: true,
35
+ role: "admin",
36
+ },
37
+ };
38
 
39
+ try {
40
+ await fetch(
41
+ `https://huggingface.co/api/organizations/${orgName}/resource-groups`,
42
+ {
43
+ method: "POST",
44
+ headers: {
45
+ "Content-Type": "application/json",
46
+ Authorization: `Bearer ${process.env.HF_TOKEN}`,
47
+ },
48
+ body: JSON.stringify(payload),
49
+ }
50
+ );
51
+ } catch (error) {
52
+ throw new Error(error.message);
53
+ }
54
+ }
55
  res.json({ success: true });
56
  });
57