Skip to content

Commit

Permalink
feat: add routes
Browse files Browse the repository at this point in the history
/probot/stats and check repo endpoints
  • Loading branch information
wei committed Nov 16, 2024
1 parent 8705223 commit 3990f7d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import log from "@/src/utils/logger.ts";
import { connectMongoDB, disconnectMongoDB } from "@/src/configs/database.ts";
import { getRandomCronSchedule } from "@/src/utils/helpers.ts";
import { getRedisClient } from "@/src/configs/redis.ts";
import createRouter from "@/src/router/index.ts";

const args = Deno.args;
const skipFullSync = args.includes("--skip-full-sync");
Expand Down Expand Up @@ -46,6 +47,7 @@ server.use(
webhooksPath: "/",
}),
);
server.use("/", createRouter(probot));

server.listen(appConfig.port, () => {
log.info(`[Express] Server is running on port ${appConfig.port}`);
Expand Down
23 changes: 23 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Request, Response } from "express";
import type { Probot } from "probot";
import express from "express";
import getProbotStats from "@/src/router/probot-stats.ts";
import getRepoHandlers from "@/src/router/repo-handler.ts";

const createRouter = (app: Probot) => {
const router = express.Router();

router.get("/ping", (_req: Request, res: Response) => {
res.json({ status: "pong" });
});

router.get("/probot/stats", getProbotStats);

const { checkHandler } = getRepoHandlers(app);
router.get("/check/:owner/:repo", checkHandler);
// TODO Add process route

return router;
};

export default createRouter;
11 changes: 11 additions & 0 deletions src/router/probot-stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Request, Response } from "express";

async function getProbotStats(_req: Request, res: Response) {
const response = await fetch(
"https://raw.githack.com/pull-app/stats/master/stats.json",
);
const data = await response.json();
res.json(data);
}

export default getProbotStats;
65 changes: 65 additions & 0 deletions src/router/repo-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Request, Response } from "express";
import type { Probot } from "probot";
import { appConfig } from "@/src/configs/app-config.ts";
import { getPullConfig } from "@/src/utils/get-pull-config.ts";
import { JobPriority, RepositoryModel } from "@wei/probot-scheduler";

function getRepoHandlers(app: Probot) {
async function checkHandler(req: Request, res: Response) {
const full_name = `${req.params.owner}/${req.params.repo}`;
app.log.info(`[${full_name}] Checking ${appConfig.configFilename}`);

try {
// Get Octokit
const repoRecord = await RepositoryModel.findOne({ full_name });

if (!repoRecord) {
app.log.error({ full_name }, `❌ Repo record not found`);
throw new Error(`❌ Repo record not found`);
}

const {
installation_id,
id: repository_id,
owner: { login: owner },
name: repo,
} = repoRecord;

const octokit = await app.auth(installation_id);
const config = await getPullConfig(octokit, app.log, {
installation_id,
owner,
repo,
repository_id,
metadata: {
cron: "",
job_priority: JobPriority.Normal,
repository_id,
},
});

if (!config) {
return res.status(404).json({
status: "error",
message: `Configuration file '${appConfig.configFilename}' not found`,
});
}

res.json(config);
} catch (error) {
app.log.error(error);
res.status(500).json({
status: "error",
message: error instanceof Error
? error.message
: "Unknown error occurred",
});
}
}

return {
checkHandler,
};
}

export default getRepoHandlers;

0 comments on commit 3990f7d

Please sign in to comment.