|
| 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