Skip to content

Commit 3990f7d

Browse files
committed
feat: add routes
/probot/stats and check repo endpoints
1 parent 8705223 commit 3990f7d

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import log from "@/src/utils/logger.ts";
77
import { connectMongoDB, disconnectMongoDB } from "@/src/configs/database.ts";
88
import { getRandomCronSchedule } from "@/src/utils/helpers.ts";
99
import { getRedisClient } from "@/src/configs/redis.ts";
10+
import createRouter from "@/src/router/index.ts";
1011

1112
const args = Deno.args;
1213
const skipFullSync = args.includes("--skip-full-sync");
@@ -46,6 +47,7 @@ server.use(
4647
webhooksPath: "/",
4748
}),
4849
);
50+
server.use("/", createRouter(probot));
4951

5052
server.listen(appConfig.port, () => {
5153
log.info(`[Express] Server is running on port ${appConfig.port}`);

src/router/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Request, Response } from "express";
2+
import type { Probot } from "probot";
3+
import express from "express";
4+
import getProbotStats from "@/src/router/probot-stats.ts";
5+
import getRepoHandlers from "@/src/router/repo-handler.ts";
6+
7+
const createRouter = (app: Probot) => {
8+
const router = express.Router();
9+
10+
router.get("/ping", (_req: Request, res: Response) => {
11+
res.json({ status: "pong" });
12+
});
13+
14+
router.get("/probot/stats", getProbotStats);
15+
16+
const { checkHandler } = getRepoHandlers(app);
17+
router.get("/check/:owner/:repo", checkHandler);
18+
// TODO Add process route
19+
20+
return router;
21+
};
22+
23+
export default createRouter;

src/router/probot-stats.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Request, Response } from "express";
2+
3+
async function getProbotStats(_req: Request, res: Response) {
4+
const response = await fetch(
5+
"https://raw.githack.com/pull-app/stats/master/stats.json",
6+
);
7+
const data = await response.json();
8+
res.json(data);
9+
}
10+
11+
export default getProbotStats;

src/router/repo-handler.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { Request, Response } from "express";
2+
import type { Probot } from "probot";
3+
import { appConfig } from "@/src/configs/app-config.ts";
4+
import { getPullConfig } from "@/src/utils/get-pull-config.ts";
5+
import { JobPriority, RepositoryModel } from "@wei/probot-scheduler";
6+
7+
function getRepoHandlers(app: Probot) {
8+
async function checkHandler(req: Request, res: Response) {
9+
const full_name = `${req.params.owner}/${req.params.repo}`;
10+
app.log.info(`[${full_name}] Checking ${appConfig.configFilename}`);
11+
12+
try {
13+
// Get Octokit
14+
const repoRecord = await RepositoryModel.findOne({ full_name });
15+
16+
if (!repoRecord) {
17+
app.log.error({ full_name }, `❌ Repo record not found`);
18+
throw new Error(`❌ Repo record not found`);
19+
}
20+
21+
const {
22+
installation_id,
23+
id: repository_id,
24+
owner: { login: owner },
25+
name: repo,
26+
} = repoRecord;
27+
28+
const octokit = await app.auth(installation_id);
29+
const config = await getPullConfig(octokit, app.log, {
30+
installation_id,
31+
owner,
32+
repo,
33+
repository_id,
34+
metadata: {
35+
cron: "",
36+
job_priority: JobPriority.Normal,
37+
repository_id,
38+
},
39+
});
40+
41+
if (!config) {
42+
return res.status(404).json({
43+
status: "error",
44+
message: `Configuration file '${appConfig.configFilename}' not found`,
45+
});
46+
}
47+
48+
res.json(config);
49+
} catch (error) {
50+
app.log.error(error);
51+
res.status(500).json({
52+
status: "error",
53+
message: error instanceof Error
54+
? error.message
55+
: "Unknown error occurred",
56+
});
57+
}
58+
}
59+
60+
return {
61+
checkHandler,
62+
};
63+
}
64+
65+
export default getRepoHandlers;

0 commit comments

Comments
 (0)