File size: 1,533 Bytes
59ff286
 
f234d16
 
 
 
 
 
 
 
 
59ff286
 
 
 
f234d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de3f96c
f234d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de3f96c
59ff286
 
 
de3f96c
59ff286
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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}`);
});