-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (45 loc) · 1.56 KB
/
server.js
File metadata and controls
54 lines (45 loc) · 1.56 KB
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
import { exec } from "child_process";
import { promisify } from "util";
import http from "http";
const execAsync = promisify(exec);
const GITHUB_REPO = process.env.GITHUB_REPO || "Teamtailor/aboard";
let cachedUser = null;
async function getCurrentUser() {
if (cachedUser) return cachedUser;
const { stdout } = await execAsync("gh api user --jq .login");
cachedUser = stdout.trim();
return cachedUser;
}
const server = http.createServer(async (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Content-Type", "application/json");
const url = new URL(req.url, `http://${req.headers.host}`);
if (url.pathname === "/api/me") {
try {
const login = await getCurrentUser();
res.end(JSON.stringify({ login }));
} catch (error) {
res.statusCode = 500;
res.end(JSON.stringify({ error: error.message }));
}
} else if (url.pathname === "/api/prs") {
try {
const { stdout } = await execAsync(
`gh pr list --repo ${GITHUB_REPO} --state merged --limit 1000 --json number,title,url,mergedAt,additions,deletions,author`,
{ maxBuffer: 50 * 1024 * 1024 }
);
const prs = JSON.parse(stdout);
res.end(JSON.stringify({ prs }));
} catch (error) {
res.statusCode = 500;
res.end(JSON.stringify({ error: error.message }));
}
} else {
res.statusCode = 404;
res.end(JSON.stringify({ error: "Not found" }));
}
});
server.listen(3002, () => {
console.log(`API server running on http://localhost:3002`);
console.log(`Fetching PRs from: ${GITHUB_REPO}`);
});